public static async Task <T2> PostForm <T1, T2>(T1 item, ApiEndPoint enpoint, string subpath = null) { if (TokenExpiry != default && TokenExpiry < DateTimeOffset.Now) { Console.WriteLine("**** Re -logged in ****"); TokenExpiry = default; await RefreshAccessTokenWithtoken(); } var myContent = new FormUrlEncodedContent(item.ToKeyValue()); var path = enpoint.ToString(); if (!string.IsNullOrWhiteSpace(subpath)) { path = path + @"/" + subpath; } using (var response = await WebClient.PostAsync(path, myContent)) { if (!response.IsSuccessStatusCode) { var result = await response.Content.ReadAsStringAsync(); throw new Exception(result); } else { var result = await response.Content.ReadAsObjectAsync <Models.BaseApiResponse <T2> >(); if (result.Status == Models.ResponseStatus.Error) { throw new Exception(result.ErrorMessage); } return(result.Result); } } }
public static async Task <T2> PostItem <T1, T2>(T1 item, ApiEndPoint enpoint, string subpath = null) { if (TokenExpiry != default && TokenExpiry < DateTimeOffset.Now) { Console.WriteLine("**** Re -logged in ****"); TokenExpiry = default; await RefreshAccessTokenWithtoken(); } var myContent = JsonConvert.SerializeObject(item); var content = new StringContent(myContent, Encoding.UTF8, "application/json"); var path = enpoint.ToString(); if (!string.IsNullOrWhiteSpace(subpath)) { path = path + @"/" + subpath; } using (var response = await WebClient.PostAsync(path, content)) { if (!response.IsSuccessStatusCode) { var result = await response.Content.ReadAsStringAsync(); throw new Exception(result); } else { var result = await response.Content.ReadAsApiResponseForType <T2>(requestBody : myContent); return(result); } } }
public static async Task <T> PostRequest <T>(ApiEndPoint enpoint, string subpath = null) { if (TokenExpiry != default && TokenExpiry < DateTimeOffset.Now) { TokenExpiry = default; Console.WriteLine("**** Re-logged in ****"); await RefreshAccessTokenWithtoken(); } var path = enpoint.ToString(); if (!string.IsNullOrWhiteSpace(subpath)) { path = path + @"/" + subpath; } using (var response = await WebClient.PostAsync(path, null)) { if (!response.IsSuccessStatusCode) { var result = await response.Content.ReadAsStringAsync(); throw new Exception(result); } else { var result = await response.Content.ReadAsApiResponseForType <T>(requestBody : path); return(result); } } }