public async Task <HttpResponseMessage> SendRequest(string endpoint, RequestVerbs verb, StringContent bodyContent = null, string token = null) { using (var client = new HttpClient()) { client.BaseAddress = new Uri(_uri); client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); if (!string.IsNullOrEmpty(token)) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); } HttpResponseMessage result = null; switch (verb) { case RequestVerbs.PUT: if (bodyContent is null) { throw new ArgumentNullException(); } result = await client.PutAsync(endpoint, bodyContent); break; case RequestVerbs.POST: if (bodyContent is null) { throw new ArgumentNullException(); } result = await client.PostAsync(endpoint, bodyContent); break; case RequestVerbs.GET: result = await client.GetAsync(endpoint); break; case RequestVerbs.DELETE: result = await client.DeleteAsync(endpoint); break; } return(result); } }
private async Task <ResponseModel> SendRequest <T>(RequestVerbs verb, string endpoint, string section) where T : class { var token = _identity.GetTokens().Token; var response = await _userWebApiHelper.SendRequest(endpoint, verb, token : token); var result = await _userWebApiHelper.GetResultFromResponse <T>(response, section); if (!result.IsSuccess && result.Message.Equals(UserWebApiHelper.TokenExpiredHeaderKey, StringComparison.OrdinalIgnoreCase)) { await RefreshToken(); await SendRequest <T>(verb, endpoint); } return(result); }