示例#1
0
        private async Task <AcmeRespone <T> > ReadResponse <T>(HttpResponseMessage response, string resourceType = null)
        {
            var data = new AcmeRespone <T>();

            if (IsJsonMedia(response.Content?.Headers.ContentType.MediaType))
            {
                var json = await response.Content.ReadAsStringAsync();

                data.Json = json;
                if (response.IsSuccessStatusCode)
                {
                    data.Data = JsonConvert.DeserializeObject <T>(json, jsonSettings);
                    var entity = data.Data as EntityBase;
                    if (resourceType != null && entity != null && string.IsNullOrWhiteSpace(entity.Resource))
                    {
                        entity.Resource = resourceType;
                    }
                }
                else
                {
                    data.Error = JsonConvert.DeserializeObject <AcmeError>(json, jsonSettings);
                }
            }
            else if (response.Content?.Headers.ContentLength > 0)
            {
                data.Raw = await response.Content.ReadAsByteArrayAsync();
            }

            ParseHeaders(data, response);
            this.nonce = data.ReplayNonce;
            return(data);
        }
示例#2
0
        private static void ParseHeaders <T>(AcmeRespone <T> data, HttpResponseMessage response)
        {
            data.Location = response.Headers.Location;

            if (response.Headers.Contains("Replay-Nonce"))
            {
                data.ReplayNonce = response.Headers.GetValues("Replay-Nonce").Single();
            }

            // TODO: Support Retry-After header

            if (response.Headers.Contains("Link"))
            {
                data.Links = response.Headers.GetValues("Link")?
                             .Select(h =>
                {
                    var segments = h.Split(';');
                    var url      = segments[0].Substring(1, segments[0].Length - 2);
                    var rel      = segments.Skip(1)
                                   .Select(s => s.Trim())
                                   .Where(s => s.StartsWith("rel=", StringComparison.OrdinalIgnoreCase))
                                   .Select(r =>
                    {
                        var relType = r.Split('=')[1];
                        return(relType.Substring(1, relType.Length - 2));
                    })
                                   .First();

                    return(new RelLink
                    {
                        Rel = rel,
                        Uri = new Uri(url)
                    });
                })
                             .ToArray();
            }

            data.ContentType = response.Content?.Headers.ContentType.MediaType;
        }