public Response Process(MediaRange requestedMediaRange, dynamic model, NancyContext context) { var odataPackage = model as ODataResponse <PackageWithUrls>; if (odataPackage != null) { var response = context.Response; if (response == null) { context.Response = response = new Response(); } response.ContentType = atomXmlContentType; response.StatusCode = HttpStatusCode.OK; response.Contents = netStream => { serializer.Serialize(netStream, odataPackage.Entity.Pkg, odataPackage.ServiceBaseUrl, odataPackage.Entity.ResourceIdUrl, odataPackage.Entity.PackageContentUrl); }; return(response); } var odataPackageCollection = model as ODataResponse <IEnumerable <PackageWithUrls> >; if (odataPackageCollection != null) { var response = context.Response; if (response == null) { context.Response = response = new Response(); } response.ContentType = atomXmlContentType; response.StatusCode = HttpStatusCode.OK; response.Contents = netStream => { serializer.Serialize(netStream, odataPackageCollection.Entity, odataPackageCollection.ServiceBaseUrl); }; return(response); } throw new InvalidOperationException("invalid model type"); }
public PackagesV2Module(IODataPackageSerializer serializer, IPackageService packageService, IPackageStorageService storage, IEdmModel odataModel, ILogger <PackagesV2Module> logger, LiGetCompatibilityOptions compat) { this._compat = compat; this._packageService = packageService; this._storage = storage; this._log = logger; this.GetCompat("/v2/contents/{id}/{version}", async(req, res, routeData) => { string id = routeData.As <string>("id"); string version = routeData.As <string>("version"); if (!NuGetVersion.TryParse(version, out var nugetVersion)) { res.StatusCode = 400; return; } if (!await packageService.IncrementDownloadCountAsync(id, nugetVersion)) { res.StatusCode = 404; return; } var identity = new PackageIdentity(id, nugetVersion); var packageStream = await _storage.GetPackageStreamAsync(identity); await res.FromStream(packageStream, "application/zip"); }); Func <HttpRequest, HttpResponse, RouteData, Task> indexHandler = async(req, res, routeData) => { var serviceUrl = GetServiceUrl(req); string text = $@"<?xml version=""1.0"" encoding=""utf-8""?> <service xml:base=""{serviceUrl}"" xmlns=""http://www.w3.org/2007/app"" xmlns:atom=""http://www.w3.org/2005/Atom""><workspace> <atom:title type=""text"">Default</atom:title><collection href=""Packages""><atom:title type=""text"">Packages</atom:title></collection></workspace> </service>"; res.StatusCode = 200; res.ContentType = "application/xml; charset=utf-8"; await res.WriteAsync(text, new UTF8Encoding(false)); }; this.GetCompat("/v2/", indexHandler); this.GetCompat(@"/v2/FindPackagesById{query}", async(req, res, routeData) => { try { var serviceUrl = GetServiceUrl(req); var uriParser = new ODataUriParser(odataModel, new Uri(serviceUrl), req.GetUri()); var path = uriParser.ParsePath(); if (path.FirstSegment.Identifier == "FindPackagesById") { var idOrNull = uriParser.CustomQueryOptions.FirstOrDefault(o => o.Key.ToLowerInvariant() == "id").Value; string id = idOrNull.TrimStart('\'').TrimEnd('\''); _log.LogDebug("Request to FindPackagesById id={0}", id); var found = await _packageService.FindAsync(id, false, true); var odata = new ODataResponse <IEnumerable <PackageWithUrls> >(serviceUrl, found.Select(f => ToPackageWithUrls(req, f))); using (var ms = new MemoryStream()) { serializer.Serialize(ms, odata.Entity, odata.ServiceBaseUrl); ms.Seek(0, SeekOrigin.Begin); await res.FromStream(ms, atomXmlContentType); } } else { res.StatusCode = 400; } } catch (ODataException odataPathError) { _log.LogError("Bad odata query", odataPathError); res.StatusCode = 400; } }); this.GetCompat(@"/v2/Packages{query}", async(req, res, routeData) => { try { var serviceUrl = GetServiceUrl(req); var uriParser = new ODataUriParser(odataModel, new Uri(serviceUrl), req.GetUri()); var path = uriParser.ParsePath(); if (path.FirstSegment.Identifier == "Packages") { if (path.Count == 2 && path.LastSegment is KeySegment) { KeySegment queryParams = (KeySegment)path.LastSegment; string id = queryParams.Keys.First(k => k.Key == "Id").Value as string; string version = queryParams.Keys.First(k => k.Key == "Version").Value as string; _log.LogDebug("Request to find package by id={0} and version={1}", id, version); var found = await _packageService.FindAsync(id, NuGetVersion.Parse(version), false, true); if (found == null) { res.StatusCode = 404; return; } var odataPackage = new ODataResponse <PackageWithUrls>(serviceUrl, ToPackageWithUrls(req, found)); using (var ms = new MemoryStream()) { serializer.Serialize(ms, odataPackage.Entity.Pkg, odataPackage.ServiceBaseUrl, odataPackage.Entity.ResourceIdUrl, odataPackage.Entity.PackageContentUrl); ms.Seek(0, SeekOrigin.Begin); await res.FromStream(ms, atomXmlContentType); } } else { res.StatusCode = 400; } } else { res.StatusCode = 400; } } catch (ODataException odataPathError) { _log.LogError("Bad odata query", odataPathError); res.StatusCode = 400; } }); }