Пример #1
0
        static private async Task <HttpResponseMessage> PutInternal(string uri, UCWAModelBase body, string version = "")
        {
            if (string.IsNullOrEmpty(uri))
            {
                return(new HttpResponseMessage());
            }

            if (!uri.StartsWith("http"))
            {
                uri = Settings.Host + uri;
            }

            using (HttpClient client = await GetClient(uri, version))
            {
                JsonSerializer serializer = new JsonSerializer()
                {
                    DefaultValueHandling = DefaultValueHandling.Ignore
                };
                serializer.Converters.Add(new StringEnumConverter());
                JObject jobject = JObject.FromObject(body, serializer);
                jobject["_links"]?.Parent?.Remove();
                jobject["_embedded"]?.Parent?.Remove();
                jobject.Add(body.PGuid, "please pass this in a PUT request");
                client.DefaultRequestHeaders.IfMatch.Add(new EntityTagHeaderValue("\"" + jobject["etag"].Value <string>() + "\""));

                return(await client.PutAsync(uri,
                                             new StringContent(JsonConvert.SerializeObject(jobject, new StringEnumConverter()), Encoding.UTF8, "application/json")));
            }
        }
Пример #2
0
        static private async Task <HttpResponseMessage> PutInternal(string uri, UCWAModelBase body, CancellationToken cancellationToken, string version = defaultVersion, bool anonymous = false)
        {
            if (string.IsNullOrEmpty(uri))
            {
                return(new HttpResponseMessage());
            }

            uri = EnsureUriContainsHttp(uri);

            var client = await GetClient(uri, cancellationToken, version, anonymous);

            JsonSerializer serializer = new JsonSerializer()
            {
                DefaultValueHandling = DefaultValueHandling.Ignore
            };

            serializer.Converters.Add(new StringEnumConverter());
            JObject jobject = JObject.FromObject(body, serializer);

            jobject["_links"]?.Parent?.Remove();
            jobject["_embedded"]?.Parent?.Remove();
            jobject.Add(body.PGuid, "please pass this in a PUT request");

            var request = new HttpRequestMessage(HttpMethod.Put, uri)
            {
                Content = new StringContent(JsonConvert.SerializeObject(jobject, new StringEnumConverter()), Encoding.UTF8, "application/json")
            };

            request.Headers.Add("If-Match", "\"" + body.ETag + "\"");
            return(await client.SendAsync(request, cancellationToken));
        }
Пример #3
0
 static public async Task <T> Put <T>(string uri, UCWAModelBase body, CancellationToken cancellationToken, string version = defaultVersion, bool anonymous = false)
 {
     return(await ExecuteHttpCallAndRetry((token) => PutInternal(uri, body, token, version, anonymous), async (response) =>
     {
         var jObject = JObject.Parse(await response.Content.ReadAsStringAsync());
         GetPGuid(jObject as JToken);
         return JsonConvert.DeserializeObject <T>(jObject.ToString());
     }, cancellationToken));
 }
Пример #4
0
        static public async Task <T> Put <T>(UCWAHref href, UCWAModelBase body, string version = "")
        {
            if (href == null || string.IsNullOrEmpty(href.Href))
            {
                return(default(T));
            }

            return(await Put <T>(href.Href, body, version));
        }
Пример #5
0
        static public async Task Put(UCWAHref href, UCWAModelBase body, string version = "")
        {
            if (href == null || string.IsNullOrEmpty(href.Href))
            {
                return;
            }

            await Put(href.Href, body, version);
        }
Пример #6
0
        static public async Task Put(string uri, UCWAModelBase body, string version = "")
        {
            var response = await PutInternal(uri, body, version);

            if (response.IsSuccessStatusCode)
            {
                return;
            }
            else
            {
                await HandleError(response);
            }
        }
Пример #7
0
        static public async Task <T> Put <T>(string uri, UCWAModelBase body, string version = "")
        {
            var response = await PutInternal(uri, body, version);

            if (response.IsSuccessStatusCode)
            {
                var jObject = JObject.Parse(await response.Content.ReadAsStringAsync());
                GetPGuid(jObject as JToken);
                return(JsonConvert.DeserializeObject <T>(jObject.ToString()));
            }
            else
            {
                await HandleError(response);
            }

            return(default(T));
        }
Пример #8
0
 static public Task <T> Put <T>(string uri, UCWAModelBase body, string version = defaultVersion, bool anonymous = false)
 {
     return(Put <T>(uri, body, GetNewCancellationToken(), version, anonymous));
 }
Пример #9
0
 static public async Task Put(string uri, UCWAModelBase body, CancellationToken cancellationToken, string version = defaultVersion, bool anonymous = false)
 {
     await ExecuteHttpCallAndRetry((token) => PutInternal(uri, body, token, version, anonymous), cancellationToken);
 }