public async Task <TEntity> SendAsync <TEntity>(HttpMethod method, string endpoint, object body = null) { var request = new HttpRequestMessage(); request.Method = method; request.RequestUri = new Uri(_apiWebSettings.Address + endpoint); Authorize(); if (body != null) { var json = JsonSerializeService.Serialize(body); request.Content = new StringContent(json, Encoding.UTF8, "application/json"); } var httpResponseMessage = await _httpClient.SendAsync(request); var responseString = await httpResponseMessage.Content.ReadAsStringAsync(); if (!httpResponseMessage.IsSuccessStatusCode) { HandleFailMessage(responseString); } return(JsonSerializeService.Deserialize <TEntity>(responseString)); }
public Task HandleExceptionAsync(HttpContext context, Exception exception) { var statusCode = HttpStatusCode.BadRequest; var errorCode = "internal_server_error"; var details = exception.Message as object; var stack = exception.StackTrace; switch (exception) { case ServerException serverException: errorCode = serverException.Code; break; default: var errorMessage = $"Unexpected exception {exception} with stack: {exception.StackTrace} with source: {exception.Source}"; _logger.Fatal(errorMessage); break; } var response = new ErrorDto { ErrorCode = errorCode, Details = details, Stack = stack }; var payload = JsonSerializeService.Serialize(response); context.Response.ContentType = "application/json"; context.Response.StatusCode = (int)statusCode; return(context.Response.WriteAsync(payload)); }
public async Task <TResponse> SendAsync <TResponse>(HttpMethod method, string url, object body = null) { var request = new HttpRequestMessage(); request.Method = method; request.RequestUri = new Uri(Client.BaseAddress, url); var hasToken = Client.DefaultRequestHeaders.Contains("Authorization"); if (!_token.IsEmpty()) { Client.DefaultRequestHeaders.Remove("Authorization"); Client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _token); } if (body != null) { var json = JsonSerializeService.Serialize(body); request.Content = new StringContent(json, Encoding.UTF8, "application/json"); } var httpResponseMessage = await Client.SendAsync(request); var responseString = await httpResponseMessage.Content.ReadAsStringAsync(); httpResponseMessage.IsSuccessStatusCode.Should().BeTrue(responseString); return(JsonSerializeService.Deserialize <TResponse>(responseString)); }
private void HandleFailMessage(string responseString) { var errorDto = JsonSerializeService.Deserialize <ErrorDto>(responseString); switch (errorDto.ErrorCode) { case ErrorCodes.InvalidCredentials: throw new UnauthorizedAccessException(); } }
public static StringContent GetPayload(this object data, IContractResolver contractResolver) { var serializeSettings = new JsonSerializerSettings { Formatting = Formatting.Indented, ContractResolver = contractResolver }; var json = JsonSerializeService.Serialize(data); return(new StringContent(json, Encoding.UTF8, "application/json")); }
public void SerializeObject_ValidObject_ReturnsValidString() { //Arrange var simpleObj = new { Name = "TestName", Array = new[] { "One", "Two" } }; var jsonSerializeService = new JsonSerializeService(); var expectedString = "{\"Name\":\"TestName\",\"Array\":[\"One\",\"Two\"]}"; //Act var objString = jsonSerializeService.SerializeObject(simpleObj); //Assert Assert.AreEqual(expectedString, objString); }
private static TModel DeserializeObject <TModel>(string data) => JsonSerializeService.Deserialize <TModel>(data);