public static async Task<String> PatchAsync(HttpClient client, String apiUrl, List<APIRequest> requestBody) { var responseBody = String.Empty; var jsonRequest = JsonConvert.SerializeObject(requestBody); var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json-patch+json"); using (HttpResponseMessage response = await client.PatchAsync(apiUrl, content)) { response.EnsureSuccessStatusCode(); responseBody = await response.Content.ReadAsStringAsync(); } return responseBody; }
public async Task<HttpStatusCode> Patch() { HttpClient httpClient = new HttpClient(); Uri uri = new Uri(DataSource.host + "goal/" + this.id.ToString() + "?access_token=" + DataSource.accessToken); List<KeyValuePair<string, string>> pairsToSend = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("match_id", this.match_id.ToString()), new KeyValuePair<string, string>("team_id", this.team_id.ToString()), new KeyValuePair<string, string>("player_id", this.player_id.ToString()), new KeyValuePair<string, string>("assistant_id", this.assistant_id.ToString()), new KeyValuePair<string, string>("is_penalty", this.is_penalty.ToString().ToLower()), new KeyValuePair<string, string>("is_autogoal", this.is_autogoal.ToString().ToLower()), new KeyValuePair<string, string>("minute", this.minute.ToString()), new KeyValuePair<string, string>("addition_minute", this.addition_minute.ToString()), }; var content = new FormUrlEncodedContent(pairsToSend); var response = await httpClient.PatchAsync(uri, content); try { response.EnsureSuccessStatusCode(); } catch (Exception ex) { objectStatus = (int)DataSource.status.needUpdate; throw new Exception(ex.Message); } string result = await response.Content.ReadAsStringAsync(); JsonObject jsonObject = JsonObject.Parse(result); objectStatus = (int)DataSource.status.ok; return response.StatusCode; }
public async Task UpdateMatchInfo() { HttpClient httpClient = new HttpClient(); Uri uri = new Uri(DataSource.host + "match/" + this.id.ToString() + "?access_token=" + DataSource.accessToken); List<KeyValuePair<string, string>> pairsToSend = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("goals1", this.goals1.ToString()), new KeyValuePair<string, string>("goals2", this.goals2.ToString()), new KeyValuePair<string, string>("penalty1", this.penalty1.ToString()), new KeyValuePair<string, string>("penalty2", this.penalty2.ToString()), new KeyValuePair<string, string>("start_at", this.start_at), new KeyValuePair<string, string>("place", this.place), }; var content = new FormUrlEncodedContent(pairsToSend); var response = await httpClient.PatchAsync(uri, content); try { response.EnsureSuccessStatusCode(); } catch (Exception ex) { //objectStatus = (int)DataSource.status.needUpdate; throw new Exception(ex.Message); //return false; } string result = await response.Content.ReadAsStringAsync(); //JsonObject jsonObject = JsonObject.Parse(result); //if (jsonObject.Count == 0) //{ // return false; //} //else //{ // this.id = (int)jsonObject["id"].GetNumber(); //} // objectStatus = (int)DataSource.status.ok; // await DataSource.RefreshYellowCardsCollectionAsync(Match_idValue); //return true; }
public override async Task <U> PatchAsync <T, U>(string path, T data) { U dataResult = default(U); var json = Newtonsoft.Json.JsonConvert.SerializeObject(data, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); var content = new System.Net.Http.StringContent(json, Encoding.UTF8, "application/json"); HttpResponseMessage response = await _client.PatchAsync( path, content); if (response.IsSuccessStatusCode) { dataResult = await response.Content.ReadAsAsync <U>(); } return(dataResult); }
public static async Task <HttpResponseMessage> PatchAsync <T>(this HttpClient httpClient, string requestUri, T patch) where T : class { if (httpClient is null) { throw new ArgumentNullException(nameof(httpClient)); } if (string.IsNullOrEmpty(requestUri)) { throw new StringArgumentNullOrEmptyException(nameof(requestUri)); } if (patch is null) { throw new ArgumentNullException(nameof(patch)); } var content = patch.ConvertToJsonStringContent(); return(await httpClient.PatchAsync(requestUri, content)); }
public Task <HttpResponseMessage> PatchAsync(Uri requestUri, HttpContent content) { return(_builtInHttpClient.PatchAsync(requestUri, content)); }
/// <summary> /// Sends a PATCH request as an asynchronous operation, with a specified value serialized using the given formatter and /// media type String. /// </summary> /// <returns> /// A task object representing the asynchronous operation. /// </returns> /// <param name="client">The client used to make the request.</param> /// <param name="requestUri">The URI the request is sent to.</param> /// <param name="value">The value to write into the entity body of the request.</param> /// <param name="formatter">The formatter used to serialize the value.</param> /// <param name="mediaType"> /// The authoritative value of the Content-Type header. Can be null, in which case the default /// content type of the formatter will be used. /// </param> /// <typeparam name="T">The type of object to serialize.</typeparam> public static Task <HttpResponseMessage> PatchAsync <T>(this HttpClient client, string requestUri, T value, MediaTypeFormatter formatter, string mediaType) { return(client.PatchAsync(requestUri, value, formatter, mediaType, CancellationToken.None)); }
/// <summary> /// Sends a PATCH request as an asynchronous operation, with a specified value serialized using the given formatter and /// media type String. Includes a cancellation token to cancel the request. /// </summary> /// <returns> /// A task object representing the asynchronous operation. /// </returns> /// <param name="client">The client used to make the request.</param> /// <param name="requestUri">The URI the request is sent to.</param> /// <param name="value">The value to write into the entity body of the request.</param> /// <param name="formatter">The formatter used to serialize the value.</param> /// <param name="mediaType"> /// The authoritative value of the Content-Type header. Can be null, in which case the default /// content type of the formatter will be used. /// </param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of /// cancellation. /// </param> /// <typeparam name="T">The type of object to serialize.</typeparam> public static Task <HttpResponseMessage> PatchAsync <T>(this HttpClient client, string requestUri, T value, MediaTypeFormatter formatter, string mediaType, CancellationToken cancellationToken) { MediaTypeHeaderValue mediaTypeHeader = mediaType != null ? new MediaTypeHeaderValue(mediaType) : null; return(client.PatchAsync(requestUri, value, formatter, mediaTypeHeader, cancellationToken)); }
/// <summary> /// Sends a PATCH request as an asynchronous operation, with a specified value serialized as XML. Includes a cancellation /// token to cancel the request. /// </summary> /// <returns> /// A task object representing the asynchronous operation. /// </returns> /// <param name="client">The client used to make the request.</param> /// <param name="requestUri">The URI the request is sent to.</param> /// <param name="value">The value to write into the entity body of the request.</param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of /// cancellation. /// </param> /// <typeparam name="T">The type of object to serialize.</typeparam> public static Task <HttpResponseMessage> PatchAsXmlAsync <T>(this HttpClient client, string requestUri, T value, CancellationToken cancellationToken) { return(client.PatchAsync(requestUri, value, new XmlMediaTypeFormatter(), cancellationToken)); }
/// <summary> /// Sends a PATCH request as an asynchronous operation, with a specified value serialized using the given formatter. /// Includes a cancellation token to cancel the request. /// </summary> /// <returns> /// A task object representing the asynchronous operation. /// </returns> /// <param name="client">The client used to make the request.</param> /// <param name="requestUri">The URI the request is sent to.</param> /// <param name="value">The value to write into the entity body of the request.</param> /// <param name="formatter">The formatter used to serialize the value.</param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of /// cancellation. /// </param> /// <typeparam name="T">The type of object to serialize.</typeparam> public static Task <HttpResponseMessage> PatchAsync <T>(this HttpClient client, string requestUri, T value, MediaTypeFormatter formatter, CancellationToken cancellationToken) { CancellationToken cancellationToken1 = cancellationToken; return(client.PatchAsync(requestUri, value, formatter, (MediaTypeHeaderValue)null, cancellationToken1)); }
/// <summary> /// Send a PATCH request with a cancellation token as an asynchronous operation. /// </summary> /// /// <returns> /// Returns <see cref="T:System.Threading.Tasks.Task`1"/>.The task object representing the asynchronous operation. /// </returns> /// <param name="client">The instantiated Http Client <see cref="HttpClient"/></param> /// <param name="requestUri">The Uri the request is sent to.</param> /// <param name="content">The HTTP request content sent to the server.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <exception cref="T:System.ArgumentNullException">The <paramref name="client"/> was null.</exception> /// <exception cref="T:System.ArgumentNullException">The <paramref name="requestUri"/> was null.</exception> public static Task <HttpResponseMessage> PatchAsync(this HttpClient client, string requestUri, HttpContent content, CancellationToken cancellationToken) { return(client.PatchAsync(CreateUri(requestUri), content, cancellationToken)); }
public async Task<bool> Patch(int MatchId) { HttpClient httpClient = new HttpClient(); Uri uri = new Uri(DataSource.host + "match/" + MatchId.ToString() + "/addition?access_token=" + DataSource.accessToken); List<KeyValuePair<string, string>> pairsToSend = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("starthour", this.starthour.ToString()), new KeyValuePair<string, string>("startminute", this.startminute.ToString()), new KeyValuePair<string, string>("endhour", this.endhour.ToString()), new KeyValuePair<string, string>("endminute", this.endminute.ToString()), new KeyValuePair<string, string>("half_time_minutes", this.half_time_minutes.ToString()), new KeyValuePair<string, string>("attendance", this.attendance.ToString()), new KeyValuePair<string, string>("match_number", this.match_number.ToString()), }; var content = new FormUrlEncodedContent(pairsToSend); var response = await httpClient.PatchAsync(uri, content); try { response.EnsureSuccessStatusCode(); } catch (Exception ex) { objectStatus = (int)DataSource.status.needUpdate; return false; } string result = await response.Content.ReadAsStringAsync(); JsonObject jsonObject = JsonObject.Parse(result); if (jsonObject.Count == 0) { return false; } else { // this.id = (int)jsonObject["id"].GetNumber(); } objectStatus = (int)DataSource.status.ok; return true; }
/// <summary> /// Send a PATCH request to the specified Uri as an asynchronous operation. /// </summary> /// /// <returns> /// Returns <see cref="T:System.Threading.Tasks.Task`1"/>.The task object representing the asynchronous operation. /// </returns> /// <param name="client">The instantiated Http Client <see cref="HttpClient"/></param> /// <param name="requestUri">The Uri the request is sent to.</param> /// <param name="content">The HTTP request content sent to the server.</param> /// <exception cref="T:System.ArgumentNullException">The <paramref name="client"/> was null.</exception> /// <exception cref="T:System.ArgumentNullException">The <paramref name="requestUri"/> was null.</exception> public static Task <HttpResponseMessage> PatchAsync(this HttpClient client, Uri requestUri, HttpContent content) { return(client.PatchAsync(requestUri, content, CancellationToken.None)); }
private static async Task<RestResponse> Send(CancellationToken ct, string method, string url, string json, Guid appKey, Guid token, int timeoutInMinutes) { using (HttpClient httpClient = new HttpClient()) { httpClient.Timeout = new TimeSpan(0, timeoutInMinutes, 0); // 1 minute timeout httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); httpClient.DefaultRequestHeaders.Add("user-agent", UserAgent()); if (appKey != Guid.Empty) httpClient.DefaultRequestHeaders.Add("appkey", appKey.ToString()); if (token != Guid.Empty) httpClient.DefaultRequestHeaders.Add("token", token.ToString()); Coordinate location = UserLocation(); if (location != null) { httpClient.DefaultRequestHeaders.Add("latitude", location.Latitude.ToString(CultureInfo.InvariantCulture)); httpClient.DefaultRequestHeaders.Add("longitude", location.Longitude.ToString(CultureInfo.InvariantCulture)); } HttpResponseMessage response = new HttpResponseMessage(); try { if (method == "POST") response = await httpClient.PostAsync(url, new StringContent(json, Encoding.UTF8, "application/json"), ct); else if (method == "PUT") response = await httpClient.PutAsync(url, new StringContent(json, Encoding.UTF8, "application/json"), ct); else if (method == "PATCH") response = await httpClient.PatchAsync(url, new StringContent(json, Encoding.UTF8, "application/json"), ct); if (response.IsSuccessStatusCode) { try { var data = response.Content.ReadAsStringAsync().Result; return new RestResponse(data); } catch (Exception) { throw new Exception(AppResources.ApiErrorPopupMessageUnknownError); } } else { string errorResponseString = response.Content.ReadAsStringAsync().Result; Debug.WriteLine(errorResponseString); if (!String.IsNullOrEmpty(errorResponseString)) { ErrorResponseModel errorResponse = null; try { errorResponse = JsonConvert.DeserializeObject<ErrorResponseModel>(errorResponseString); return new RestResponse(errorResponse); } catch (Exception) { } } if (response.StatusCode == HttpStatusCode.NotFound) { return new RestResponse(new Exception(AppResources.ApiErrorPopupMessageNotFound)); } else if (response.StatusCode == HttpStatusCode.BadRequest) { return new RestResponse(new Exception(AppResources.ApiErrorPopupMessageBadRequest)); } else if (response.StatusCode == HttpStatusCode.InternalServerError) { return new RestResponse(new Exception(AppResources.ApiErrorPopupMessageInternalServerError)); } else return new RestResponse(new Exception(AppResources.ApiErrorPopupMessageUnknownError)); } } catch (TaskCanceledException) { if (ct.IsCancellationRequested) return new RestResponse(new Exception(AppResources.ApiErrorTaskCancelled)); return new RestResponse(new Exception(AppResources.ApiErrorPopupMessageTimeout)); } catch (Exception) { return new RestResponse(new Exception(AppResources.ApiErrorPopupMessageUnknownError)); } } }
public async Task<bool> Patch() { HttpClient httpClient = new HttpClient(); Uri uri = new Uri(DataSource.host + "match_player/" + this.id.ToString() + "?access_token=" + DataSource.accessToken); List<KeyValuePair<string, string>> pairsToSend = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("match_id", this.match_id.ToString()), new KeyValuePair<string, string>("team_id", this.team_id.ToString()), new KeyValuePair<string, string>("player_id", this.player_id.ToString()), new KeyValuePair<string, string>("teamsheet", this.teamsheet.ToString()), new KeyValuePair<string, string>("is_capitan", this.is_capitan.ToString().ToLower()), new KeyValuePair<string, string>("is_goalkeeper", this.is_goalkeeper.ToString().ToLower()), }; var content = new FormUrlEncodedContent(pairsToSend); var response = await httpClient.PatchAsync(uri, content); try { response.EnsureSuccessStatusCode(); } catch (Exception ex) { objectStatus = (int)DataSource.status.needUpdate; throw new Exception(ex.Message); return false; } string result = await response.Content.ReadAsStringAsync(); JsonObject jsonObject = JsonObject.Parse(result); if (jsonObject.Count == 0) { return false; } else { // this.id = (int)jsonObject["id"].GetNumber(); } objectStatus = (int)DataSource.status.ok; return true; }
public async Task<bool> Patch() { HttpClient httpClient = new HttpClient(); Uri uri = new Uri(DataSource.host + "yellow_card/" + this.id.ToString() + "?access_token=" + DataSource.accessToken); List<KeyValuePair<string, string>> pairsToSend = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("match_id", this.match_id.ToString()), new KeyValuePair<string, string>("team_id", this.team_id.ToString()), new KeyValuePair<string, string>("player_id", this.player_id.ToString()), new KeyValuePair<string, string>("minute", this.minute.ToString()), new KeyValuePair<string, string>("addition_minute", this.addition_minute.ToString()), new KeyValuePair<string, string>("note", this.note), }; var content = new FormUrlEncodedContent(pairsToSend); var response = await httpClient.PatchAsync(uri, content); try { response.EnsureSuccessStatusCode(); } catch (Exception ex) { objectStatus = (int)DataSource.status.needUpdate; throw new Exception(ex.Message); return false; } string result = await response.Content.ReadAsStringAsync(); JsonObject jsonObject = JsonObject.Parse(result); if (jsonObject.Count == 0) { return false; } else { // this.id = (int)jsonObject["id"].GetNumber(); } objectStatus = (int)DataSource.status.ok; await DataSource.RefreshYellowCardsCollectionAsync(Match_idValue); return true; }
public static Task <HttpResponseMessage> PatchContentAsJsonAsync <T>(this HttpClient client, Uri requestUri, T requestContent, CancellationToken cancellationToken) { var httpContent = CreateJsonHttpContent(requestContent); return(client.PatchAsync(requestUri, httpContent, cancellationToken)); }
public static Task <HttpResponseMessage> PatchAsync <T>(this HttpClient client, string requestUri, T value, MediaTypeFormatter formatter, CancellationToken cancellationToken) { var content = new ObjectContent <T>(value, formatter); return(client.PatchAsync(requestUri, content, cancellationToken)); }
public static async void Patch(string url, System.Net.Http.HttpContent wiPostDataContent, JSONReturnCallBack callBack) { string responseString = String.Empty; try { using (HttpClient client = new System.Net.Http.HttpClient()) { client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json-patch+json")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(getConnectionDetails()))); using (System.Net.Http.HttpResponseMessage response = client.PatchAsync(SetURL(url), wiPostDataContent).Result) { response.EnsureSuccessStatusCode(); string ResponseContent = await response.Content.ReadAsStringAsync(); responseString = ResponseContent; } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.ReadLine(); } callBack(responseString, String.Empty); }