private static async Task <HttpResponseMessage> SendRequestAsync <TBody>(HttpMethod method, string uri, TBody body, TokenFunc getToken, bool isRefresh) { var request = new HttpRequestMessage(method, BuildUri(uri)); if (body != null) { if (body is string) { request.Content = new StringContent(body as string, Encoding.UTF8, "application/x-www-form-urlencoded"); } else { request.Content = new ObjectContent <TBody>(body, new JsonMediaTypeFormatter()); } } string token = getToken(isRefresh); if (string.IsNullOrEmpty(token) == false) { request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); } using (HttpClient client = GetHttpClient()) { return(await client.SendAsync(request)); } }
internal static async Task <T> PatchItemAsync <T>(string uri, T item, TokenFunc getToken) { return(await DoHttp <T, T>("PATCH", uri, item, getToken)); }
private static async Task <TResult> DoHttp <TBody, TResult>(HttpMethod method, string uri, TBody body, TokenFunc getToken) { Log.Out(Log.Severity.Info, "DoHttp", string.Format("Uri=[{0}]", uri)); for (bool isRefresh = false; ; isRefresh = true) { HttpResponseMessage response = await SendRequestAsync(method, uri, body, getToken, isRefresh); string jsonResponse = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { return(JsonConvert.DeserializeObject <TResult>(jsonResponse)); } var code = response.StatusCode; if (isRefresh == false && code == System.Net.HttpStatusCode.Unauthorized) { var hasInvalidToken = response.Headers.WwwAuthenticate.FirstOrDefault(x => x.Parameter.Contains("error=\"invalid_token\"")) != null; if (hasInvalidToken) { Log.Out(Log.Severity.Info, "DoHttp", "Found invalid_token!!!"); continue; } } throw new System.Exception(GetErrorMessage(jsonResponse, code)); } }
internal static async Task <T> PostItemDynamicAsync <T>(string uri, dynamic body, TokenFunc getToken) { return(await DoHttp <ExpandoObject, T>(HttpMethod.Post, uri, body, getToken)); }
internal static async Task DeleteItemAsync(string uri, TokenFunc getToken) { await DoHttp <EmptyBody, EmptyBody>(HttpMethod.Delete, uri, null, getToken); }
internal static async Task <T> PostItemAsync <T>(string uri, T item, TokenFunc getToken) { return(await DoHttp <T, T>(HttpMethod.Post, uri, item, getToken)); }
internal static async Task <ODataCollection <T> > GetCollectionAsync <T>(string uri, TokenFunc getToken) { return(await GetItemAsync <ODataCollection <T> >(uri, getToken)); }
internal static async Task <T> GetItemAsync <T>(string uri, TokenFunc getToken) { return(await DoHttp <EmptyBody, T>(HttpMethod.Get, uri, null, getToken)); }
internal static async Task <TResult> DoHttp <TBody, TResult>(string methodName, string uri, TBody body, TokenFunc getToken) { return(await DoHttp <TBody, TResult>(new HttpMethod(methodName), uri, body, getToken)); }
internal static async Task <TResult> PostItemAsync2 <TBody, TResult>(string uri, TBody item, TokenFunc getToken) { return(await DoHttp <TBody, TResult>(HttpMethod.Post, uri, item, getToken)); }