Skip to content

Commit

Permalink
Merge pull request #11 from TBCBank/feature/AddMediaTypeParametersInP…
Browse files Browse the repository at this point in the history
…ostJsonAsync

Feature/add media type parameters in post json async
  • Loading branch information
lachinchaladze authored Aug 19, 2022
2 parents 28a80db + c65056d commit ac59b77
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
46 changes: 42 additions & 4 deletions src/TBC.OpenAPI.SDK.Core/HttpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ public async Task<ApiResponse<TResponseData>> GetJsonAsync<TResponseData>(string

#region Post



public Task<ApiResponse<TResponseData>> PostJsonAsync<TRequestData, TResponseData>(string path, TRequestData data, CancellationToken cancellationToken = default)
=> PostJsonAsync<TRequestData, TResponseData>(path, data, null, null, cancellationToken);
=> PostJsonAsync<TRequestData, TResponseData>(path:path, data:data, query: null, headers:null, cancellationToken:cancellationToken);

public Task<ApiResponse<TResponseData>> PostJsonAsync<TRequestData, TResponseData>(string path, TRequestData data, QueryParamCollection? query = null, CancellationToken cancellationToken = default)
=> PostJsonAsync<TRequestData, TResponseData>(path, data, query, null, cancellationToken);
=> PostJsonAsync<TRequestData, TResponseData>(path:path, data:data, query:query, headers:null, cancellationToken:cancellationToken);

public async Task<ApiResponse<TResponseData>> PostJsonAsync<TRequestData, TResponseData>(string path, TRequestData data, QueryParamCollection? query = null, HeaderParamCollection? headers = null, CancellationToken cancellationToken = default)
{
Expand All @@ -58,11 +60,29 @@ public async Task<ApiResponse<TResponseData>> PostJsonAsync<TRequestData, TRespo
}



public Task<ApiResponse<TResponseData>> PostJsonAsync<TRequestData, TResponseData>(string path, TRequestData data, string mediaType = "application/json", CancellationToken cancellationToken = default)
=> PostJsonAsync<TRequestData, TResponseData>(path, data, null, null, mediaType, cancellationToken);

public Task<ApiResponse<TResponseData>> PostJsonAsync<TRequestData, TResponseData>(string path, TRequestData data, QueryParamCollection? query = null, string mediaType = "application/json", CancellationToken cancellationToken = default)
=> PostJsonAsync<TRequestData, TResponseData>(path, data, query, null,mediaType, cancellationToken);

public async Task<ApiResponse<TResponseData>> PostJsonAsync<TRequestData, TResponseData>(string path, TRequestData data, QueryParamCollection? query = null, HeaderParamCollection? headers = null, string mediaType = "application/json", CancellationToken cancellationToken = default)
{
var httpClient = GetHttpClient();
var requestMessage = CreateRequestMessage(httpClient, HttpMethod.Post, path, query, headers);
var json = JsonSerializer.Serialize(data, _jsonSerializerOptions);
requestMessage.Content = new StringContent(json, Encoding.UTF8, mediaType);
return await SendRequestMessage<TResponseData>(httpClient, requestMessage, cancellationToken).ConfigureAwait(false);
}



public Task<ApiResponseBase> PostJsonAsync<TRequestData>(string path, TRequestData data, CancellationToken cancellationToken = default)
=> PostJsonAsync(path, data, null, null, cancellationToken);
=> PostJsonAsync(path:path, data:data, query:null,headers:null, cancellationToken:cancellationToken);

public Task<ApiResponseBase> PostJsonAsync<TRequestData>(string path, TRequestData data, QueryParamCollection? query = null, CancellationToken cancellationToken = default)
=> PostJsonAsync(path, data, query, null, cancellationToken);
=> PostJsonAsync(path:path, data:data, query:query, headers:null, cancellationToken:cancellationToken);

public async Task<ApiResponseBase> PostJsonAsync<TRequestData>(string path, TRequestData data, QueryParamCollection? query = null, HeaderParamCollection? headers = null, CancellationToken cancellationToken = default)
{
Expand All @@ -73,6 +93,24 @@ public async Task<ApiResponseBase> PostJsonAsync<TRequestData>(string path, TReq
return await SendRequestMessage(httpClient, requestMessage, cancellationToken).ConfigureAwait(false);
}




public Task<ApiResponseBase> PostJsonAsync<TRequestData>(string path, TRequestData data, string mediaType = "application/json", CancellationToken cancellationToken = default)
=> PostJsonAsync(path, data, null, null,mediaType, cancellationToken);

public Task<ApiResponseBase> PostJsonAsync<TRequestData>(string path, TRequestData data, QueryParamCollection? query = null, string mediaType = "application/json", CancellationToken cancellationToken = default)
=> PostJsonAsync(path, data, query, null, mediaType, cancellationToken);

public async Task<ApiResponseBase> PostJsonAsync<TRequestData>(string path, TRequestData data, QueryParamCollection? query = null, HeaderParamCollection? headers = null, string mediaType = "application/json", CancellationToken cancellationToken = default)
{
var httpClient = GetHttpClient();
var requestMessage = CreateRequestMessage(httpClient, HttpMethod.Post, path, query, headers);
var json = JsonSerializer.Serialize(data, _jsonSerializerOptions);
requestMessage.Content = new StringContent(json, Encoding.UTF8, mediaType);
return await SendRequestMessage(httpClient, requestMessage, cancellationToken).ConfigureAwait(false);
}

#endregion

#region Put
Expand Down
16 changes: 12 additions & 4 deletions src/TBC.OpenAPI.SDK.Core/IHttpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@ public interface IHttpHelper<TClient>

#region Post
public Task<ApiResponse<TResponseData>> PostJsonAsync<TRequestData, TResponseData>(string path, TRequestData data, CancellationToken cancellationToken = default);

public Task<ApiResponse<TResponseData>> PostJsonAsync<TRequestData, TResponseData>(string path, TRequestData data, QueryParamCollection? query = null, CancellationToken cancellationToken = default);

public Task<ApiResponse<TResponseData>> PostJsonAsync<TRequestData, TResponseData>(string path, TRequestData data, QueryParamCollection? query = null, HeaderParamCollection? headers = null, CancellationToken cancellationToken = default);

public Task<ApiResponseBase> PostJsonAsync<TRequestData>(string path, TRequestData data, CancellationToken cancellationToken = default);

public Task<ApiResponseBase> PostJsonAsync<TRequestData>(string path, TRequestData data, QueryParamCollection? query = null, CancellationToken cancellationToken = default);
public Task<ApiResponse<TResponseData>> PostJsonAsync<TRequestData, TResponseData>(string path, TRequestData data, string mediaType = "application/json", CancellationToken cancellationToken = default);
public Task<ApiResponse<TResponseData>> PostJsonAsync<TRequestData, TResponseData>(string path, TRequestData data, QueryParamCollection? query = null, string mediaType = "application/json", CancellationToken cancellationToken = default);
public Task<ApiResponse<TResponseData>> PostJsonAsync<TRequestData, TResponseData>(string path, TRequestData data, QueryParamCollection? query = null, HeaderParamCollection? headers = null, string mediaType = "application/json", CancellationToken cancellationToken = default);



public Task<ApiResponseBase> PostJsonAsync<TRequestData>(string path, TRequestData data, CancellationToken cancellationToken = default);
public Task<ApiResponseBase> PostJsonAsync<TRequestData>(string path, TRequestData data, QueryParamCollection? query = null, CancellationToken cancellationToken = default);
public Task<ApiResponseBase> PostJsonAsync<TRequestData>(string path, TRequestData data, QueryParamCollection? query = null, HeaderParamCollection? headers = null, CancellationToken cancellationToken = default);


public Task<ApiResponseBase> PostJsonAsync<TRequestData>(string path, TRequestData data, string mediaType = "application/json", CancellationToken cancellationToken = default);
public Task<ApiResponseBase> PostJsonAsync<TRequestData>(string path, TRequestData data, QueryParamCollection? query = null, string mediaType = "application/json", CancellationToken cancellationToken = default);
public Task<ApiResponseBase> PostJsonAsync<TRequestData>(string path, TRequestData data, QueryParamCollection? query = null, HeaderParamCollection? headers = null, string mediaType = "application/json", CancellationToken cancellationToken = default);
#endregion

#region Put
Expand Down

0 comments on commit ac59b77

Please sign in to comment.