Skip to content

Commit

Permalink
Merge 394f16e into df176a0
Browse files Browse the repository at this point in the history
  • Loading branch information
magnus-tretton37 committed Mar 27, 2019
2 parents df176a0 + 394f16e commit a30c415
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
50 changes: 28 additions & 22 deletions src/Castle.Sdk/Infrastructure/HttpMessageSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,54 +42,60 @@ internal class HttpMessageSender : IMessageSender
public async Task<TResponse> Post<TResponse>(string endpoint, object payload)
where TResponse : class, new()
{
var jsonContent = payload.ToHttpContent();
var message = new HttpRequestMessage(HttpMethod.Post, endpoint)
{
Content = jsonContent
};

return await SendRequest<TResponse>(message);
return await SendRequest<TResponse>(HttpMethod.Post, endpoint, payload);
}

public async Task<TResponse> Get<TResponse>(string endpoint)
where TResponse : class, new()
{
var message = new HttpRequestMessage(HttpMethod.Get, endpoint);
return await SendRequest<TResponse>(message);
return await SendRequest<TResponse>(HttpMethod.Get, endpoint);
}

public async Task<TResponse> Put<TResponse>(string endpoint)
where TResponse : class, new()
{
var message = new HttpRequestMessage(HttpMethod.Put, endpoint);
return await SendRequest<TResponse>(message);
return await SendRequest<TResponse>(HttpMethod.Put, endpoint);
}

public async Task<TResponse> Delete<TResponse>(string endpoint, object payload)
where TResponse : class, new()
{
var jsonContent = payload.ToHttpContent();
var message = new HttpRequestMessage(HttpMethod.Delete, endpoint)
{
Content = jsonContent
};
return await SendRequest<TResponse>(message);
return await SendRequest<TResponse>(HttpMethod.Delete, endpoint, payload);
}

private async Task<TResponse> SendRequest<TResponse>(HttpRequestMessage requestMessage)
private async Task<TResponse> SendRequest<TResponse>(HttpMethod method, string endpoint, object payload = null)
where TResponse : class, new()
{
try
var jsonContent = payload != null ? payload.ToHttpContent() : null;
var requestMessage = new HttpRequestMessage(method, endpoint)
{
_logger.Info(() => "Sending, " + requestMessage);
Content = jsonContent
};

_logger.Info(() => string.Concat(
"Request",
Environment.NewLine,
requestMessage,
Environment.NewLine,
payload != null ? JsonForCastle.SerializeObject(payload) : ""
));

try
{
var response = await _httpClient.SendAsync(requestMessage);

_logger.Info(() => "Receiving, " + response);
var content = response.Content != null ? await response.Content.ReadAsStringAsync() : "";

_logger.Info(() => string.Concat(
"Response",
Environment.NewLine,
response,
Environment.NewLine,
content
));

if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
return JsonForCastle.DeserializeObject<TResponse>(content);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Tests/Sending/When_sending_requests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class When_sending_requests
logger
.Received()
.Info(Arg.Is<Func<string>>(func => func()
.StartsWith("Sending")));
.StartsWith("Request")));
}
}

Expand All @@ -46,7 +46,7 @@ public class When_sending_requests
logger
.Received()
.Info(Arg.Is<Func<string>>(func => func()
.StartsWith("Receiving")));
.StartsWith("Response")));
}
}

Expand Down

0 comments on commit a30c415

Please sign in to comment.