Exemplo n.º 1
0
        public static HttpResponseMessage Invoke(HttpRequestMessage request, string filter, string orderby, bool desc,
                                                 string searchTerm, string targetFramework, bool includePrerelease,
                                                 int skip, int stop)
        {
            Uri uri = request.RequestUri;
            bool filterIsLatestVersion = filter == "IsLatestVersion";

            List<Package> packages = ReadPackages(filterIsLatestVersion, orderby, desc, searchTerm, targetFramework,
                includePrerelease, skip, stop);

            DateTime updated = packages.Any()
                ? packages.OrderByDescending(p => p.DateUpdated).First().DateUpdated
                : DateTime.UtcNow;

            string baseUri = uri.Scheme + "://" + uri.Host + ":" + uri.Port;
            string apiUri = baseUri + "/api/v2/";

            SeachFeed.feed feed = new SeachFeed.feed
            {
                @base = apiUri,
                count = packages.Count,
                updated = updated,
                link = new SeachFeed.feedLink(apiUri + "Packages"),
                entry = packages.Select(p => GetPackageEntry(p, uri)).ToArray()
            };

            XmlSerializer serializer = new XmlSerializer(typeof(SeachFeed.feed));
            MemoryStream ms = new MemoryStream();
            serializer.Serialize(ms, feed);
            ms.Position = 0;
            string result = new StreamReader(ms).ReadToEnd();

            HttpResponseMessage res = request.CreateResponse(HttpStatusCode.OK);
            res.Content = new StringContent(result);

            res.Content.Headers.Remove("Content-Type");
            res.Content.Headers.Add("Content-Type", "application/atom+xml;type=entry;charset=utf-8");
            res.Headers.Add("DataServiceVersion", "2.0;");

            return res;
        }
        private static string GetSearchResult(List<Package> packages)
        {
            Uri uri = new Uri(BaseUrl);
            string baseUri = uri.Scheme + "://" + uri.Host + ":" + uri.Port;
            string apiUri = baseUri + "/api/v2/";

            DateTime updated = packages.Any()
                                   ? packages.OrderByDescending(p => p.DateUpdated).First().DateUpdated
                                   : DateTime.UtcNow;

            SeachFeed.feed feed = new SeachFeed.feed
            {
                @base = apiUri,
                count = packages.Count,
                updated = updated,
                link = new SeachFeed.feedLink(apiUri + "Packages"),
                entry = packages.Select(p => Search.GetPackageEntry(p, uri)).ToArray()
            };

            XmlSerializer serializer = new XmlSerializer(typeof(SeachFeed.feed));
            MemoryStream ms = new MemoryStream();
            serializer.Serialize(ms, feed);
            ms.Position = 0;
            return new StreamReader(ms).ReadToEnd();
        }