Skip to content

Commit

Permalink
Merge pull request #95 from FromDoppler/refactor-get-api-client
Browse files Browse the repository at this point in the history
Add RestClientExtensions
  • Loading branch information
mmosquera authored Aug 20, 2024
2 parents 81b2fd2 + 52bf715 commit 98ca795
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 34 deletions.
44 changes: 10 additions & 34 deletions doppler-beplic/Services/Classes/BeplicSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,63 +24,39 @@ public BeplicSdk(IOptions<ApiConnectionOptions> options)
_serviceClient = GetServiceClient();
}

public async Task<RestResponse> ExecuteApiResource(string resource, object body, Method metod)
public async Task<RestResponse> ExecuteApiResource(string resource, object body, Method method)
{
await EnsureAuthentication();

var request = new RestRequest(resource, metod);
request.AddJsonBody(body);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Bearer " + AccessToken);

return await _apiClient.ExecuteAsync(request);
return await _apiClient.ExecuteResource(AccessToken ?? string.Empty, resource, body, method);
}

public async Task<RestResponse> ExecuteApiResource(string resource, Parameter[] parameters, Method metod)
public async Task<RestResponse> ExecuteApiResource(string resource, Parameter[] parameters, Method method)
{
await EnsureAuthentication();

var request = new RestRequest(resource, metod);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Bearer " + AccessToken);
request.Parameters.AddParameters(parameters);

return await _apiClient.ExecuteAsync(request);
return await _apiClient.ExecuteResource(AccessToken ?? string.Empty, resource, parameters, method);
}

public async Task<RestResponse> ExecuteServiceResource(string resource, Method metod)
public async Task<RestResponse> ExecuteServiceResource(string resource, Method method)
{
await EnsureAuthentication();

var request = new RestRequest(resource, metod);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Bearer " + AccessToken);

return await _serviceClient.ExecuteAsync(request);
return await _serviceClient.ExecuteResource(AccessToken ?? string.Empty, resource, method);
}

public async Task<RestResponse> ExecuteServiceResource(string resource, object body, Method metod)
public async Task<RestResponse> ExecuteServiceResource(string resource, object body, Method method)
{
await EnsureAuthentication();

var request = new RestRequest(resource, metod);
request.AddJsonBody(body);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Bearer " + AccessToken);

return await _serviceClient.ExecuteAsync(request);
return await _serviceClient.ExecuteResource(AccessToken ?? string.Empty, resource, body, method);
}

public async Task<RestResponse> ExecuteServiceResource(string resource, Parameter[] parameters, Method metod)
public async Task<RestResponse> ExecuteServiceResource(string resource, Parameter[] parameters, Method method)
{
await EnsureAuthentication();

var request = new RestRequest(resource, metod);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Bearer " + AccessToken);
request.Parameters.AddParameters(parameters);

return await _serviceClient.ExecuteAsync(request);
return await _serviceClient.ExecuteResource(AccessToken ?? string.Empty, resource, parameters, method);
}

private RestClient GetApiClient()
Expand Down
36 changes: 36 additions & 0 deletions doppler-beplic/Services/Classes/RestClientExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using RestSharp;

namespace DopplerBeplic.Services.Classes
{
public static class RestClientExtensions
{
public static async Task<RestResponse> ExecuteResource(this IRestClient client, string accessToken, string resource, object body, Method method)
{
var request = new RestRequest(resource, method);
request.AddJsonBody(body);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Bearer " + accessToken);

return await client.ExecuteAsync(request);
}

public static async Task<RestResponse> ExecuteResource(this IRestClient client, string accessToken, string resource, Parameter[] parameters, Method method)
{
var request = new RestRequest(resource, method);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Bearer " + accessToken);
request.Parameters.AddParameters(parameters);

return await client.ExecuteAsync(request);
}

public static async Task<RestResponse> ExecuteResource(this IRestClient client, string accessToken, string resource, Method method)
{
var request = new RestRequest(resource, method);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Bearer " + accessToken);

return await client.ExecuteAsync(request);
}
}
}

0 comments on commit 98ca795

Please sign in to comment.