Esempio n. 1
0
        public async Task <PartialArray <RemotePackageInfo> > ListPackages(Context ctx,
                                                                           string keyword = "", string category = "", int offset = 0, int limit = 0)
        {
            var url = ApiBaseUrl + "/packages?platform=" + Uri.EscapeDataString(ctx.LocalRegistry.PlatformName);

            if (keyword != "")
            {
                url += "&keyword=" + Uri.EscapeDataString(keyword);
            }
            if (category != "")
            {
                url += "&category=" + Uri.EscapeDataString(category);
            }

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);

            if (offset != 0)
            {
                request.Headers.Range = new System.Net.Http.Headers.RangeHeaderValue(offset, offset + limit);
            }

            ServicePointManager.SecurityProtocol = (SecurityProtocolType)0x3ff0;
            HttpResponseMessage response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);

            if (!response.IsSuccessStatusCode)
            {
                throw new HttpException((int)response.StatusCode + " " + response.ReasonPhrase);
            }
            int    totalLength = (int)response.Content.Headers.ContentRange.Length;
            string data        = await response.Content.ReadAsStringAsync();

            XmlDocument dataDoc = new XmlDocument();

            dataDoc.LoadXml(data);

            var packageNodes      = dataDoc.FirstChild.ChildNodes;
            var packageList       = new List <RemotePackageInfo>();
            var packageSerializer = new ParamXmlSerializer(ctx.LocalRegistry.PlatformName, typeof(Package));

            foreach (var node in packageNodes)
            {
                var wrapper = new System.IO.StringReader(((XmlElement)node).OuterXml);
                packageList.Add((Package)packageSerializer.Deserialize(wrapper));
            }
            return(new PartialArray <RemotePackageInfo>(packageList, totalLength));
        }
Esempio n. 2
0
        public async Task <RemotePackageInfo> QueryPackage(Context ctx, Identifier id)
        {
            var url = ApiBaseUrl + "/packages/";

            if (id.HasGuid)
            {
                url += id.Guid.ToString("N").ToLowerInvariant();
            }
            else if (id.HasName)
            {
                url += Uri.EscapeDataString(id.Name);
            }
            else
            {
                throw new ArgumentNullException();
            }

            ServicePointManager.SecurityProtocol = (SecurityProtocolType)0x3ff0;
            HttpResponseMessage response = await httpClient.GetAsync(url);

            if (!response.IsSuccessStatusCode)
            {
                if (response.StatusCode == HttpStatusCode.NotFound)
                {
                    return(null);
                }
                else
                {
                    throw new HttpException((int)response.StatusCode + " " + response.ReasonPhrase);
                }
            }
            string data = await response.Content.ReadAsStringAsync();

            var packageSerializer = new ParamXmlSerializer(ctx.LocalRegistry.PlatformName, typeof(Package));
            var wrapper           = new System.IO.StringReader(data);

            return((Package)packageSerializer.Deserialize(wrapper));
        }