Skip to content

Commit

Permalink
fix: request base class bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
onurkanbakirci committed Dec 22, 2023
1 parent 73cfef5 commit 7b766b8
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 28 deletions.
17 changes: 15 additions & 2 deletions src/Integration.Hub/IntegrationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@ namespace Integration.Hub;
public class IntegrationBase
{
protected static readonly HttpClient _httpClient;
protected static JsonSerializerOptions _options = default!;
static IntegrationBase()
{
_httpClient = new HttpClient();
}

protected void IntializeDefaultHeaders(Dictionary<string, string> headers)
{
// Add default headers to the request
foreach (var header in headers)
_httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
}

protected void SetSerializerOptions(JsonSerializerOptions options)
{
_options = options;
}

public async Task<TResponse> InvokeRequestAsync<TResponse>(Func<HttpClient, Task<HttpResponseMessage>> httpRequest) where TResponse : IResponseModel
{
var response = await httpRequest.Invoke(_httpClient);
Expand All @@ -19,7 +32,7 @@ public async Task<TResponse> InvokeRequestAsync<TResponse>(Func<HttpClient, Task
if (!isSuccess)
throw new Exception(responseAsString);

return JsonSerializer.Deserialize<TResponse>(responseAsString)!;
return JsonSerializer.Deserialize<TResponse>(responseAsString, _options)!;
}

public async Task<bool> InvokeRequestAsync(Func<HttpClient, Task<HttpResponseMessage>> httpRequest)
Expand All @@ -43,7 +56,7 @@ public async Task<TResponse> InvokeRequestAsync<TResponse>(Func<HttpClient, Stri
var isSuccess = response.IsSuccessStatusCode;
if (!isSuccess)
throw new Exception(responseAsString);
return JsonSerializer.Deserialize<TResponse>(responseAsString)!;
return JsonSerializer.Deserialize<TResponse>(responseAsString, _options)!;
}

public async Task<bool> InvokeRequestAsync(Func<HttpClient, StringContent?, Task<HttpResponseMessage>> httpRequest, IRequestModel requestModel)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Text;
using System.Text.Json;
using Integration.Hub;

namespace Integration.Marketplaces.Trendyol.Infrastructure;
Expand All @@ -14,6 +16,17 @@ public HepsiburadaIntegrationBase(string username, string password, bool isInPro
_username = username ?? throw new ArgumentNullException(nameof(username));
_password = password ?? throw new ArgumentNullException(nameof(password));
_isInProduction = isInProduction;

IntializeDefaultHeaders(new Dictionary<string, string>
{
{ "Accept", "application/json" },
{ "Authorization", $"Basic {Convert.ToBase64String(Encoding.ASCII.GetBytes($"{_username}:{_password}"))}"}
});

SetSerializerOptions(new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
});
}

public string GetBaseUrl() => _isInProduction ? ProdBaseUrl : StageBaseUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace Integration.Marketplaces.Hepsiburada.Infrastructure.ProductIntegration

public class HepsiburadaProductIntegration : HepsiburadaIntegrationBase, IHepsiburadaProductIntegration, IMarketplaceIntegration
{
private string GetCategoriesUrl() => $"{GetBaseUrl}product/api/categories/get-all-categories";
private string GetCategoryAttributesUrl(int categoryId) => $"{GetBaseUrl}product/api/categories/{categoryId}/attributes";
private string GetCategoryAttributeValueUrl(int categoryId, string attributeId) => $"{GetBaseUrl}product/api/categories/{categoryId}/attribute/{attributeId}/v";
private string GetCategoriesUrl() => $"{GetBaseUrl()}product/api/categories/get-all-categories";
private string GetCategoryAttributesUrl(int categoryId) => $"{GetBaseUrl()}product/api/categories/{categoryId}/attributes";
private string GetCategoryAttributeValueUrl(int categoryId, string attributeId) => $"{GetBaseUrl()}product/api/categories/{categoryId}/attribute/{attributeId}/v";

public HepsiburadaProductIntegration(string username, string password, bool isInProduction = true) : base(username, password, isInProduction)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

public class TrendyolClaimIntegration : TrendyolIntegrationBase, ITrendyolClaimIntegration, IMarketplaceIntegration
{
private string GetClaimsUrl() => $"{GetBaseUrl}suppliers/{_supplierId}/claims";
private string GetCreateClaimUrl() => $"{GetBaseUrl}suppliers/{_supplierId}/claims/create";
private string GetApproveClaimLineItemUrl(string claimId) => $"{GetBaseUrl}claims/{claimId}/items/approve";
private string GetClaimsUrl() => $"{GetBaseUrl()}suppliers/{_supplierId}/claims";
private string GetCreateClaimUrl() => $"{GetBaseUrl()}suppliers/{_supplierId}/claims/create";
private string GetApproveClaimLineItemUrl(string claimId) => $"{GetBaseUrl()}claims/{claimId}/items/approve";
public TrendyolClaimIntegration(string supplierId, string apiKey, string apiSecret, bool isInProduction, string entegratorFirm) : base(supplierId, apiKey, apiSecret, isInProduction, entegratorFirm)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
namespace Integration.Marketplaces.Trendyol.Infrastructure.ProductIntegration;
public class TrendyolPackageIntegration : TrendyolIntegrationBase, ITrendyolPackageIntegration, IMarketplaceIntegration
{
private string GetShipmentPackagesUrl() => $"{GetBaseUrl}suppliers/{_supplierId}/orders";
private string GetUpdateTrackingNumberUrl(long shipmentPackageId) => $"{GetBaseUrl}suppliers/{_supplierId}/{shipmentPackageId}/update-tracking-number";
private string GetUpdatePackageUrl(long shipmentPackageId) => $"{GetBaseUrl}suppliers/{_supplierId}//shipment-packages/{shipmentPackageId}";
private string GetAddInvoiceLinkUrl() => $"{GetBaseUrl}suppliers/{_supplierId}/supplier-invoice-links";
private string GetDeleteInvoiceLinkUrl() => $"{GetBaseUrl}suppliers/{_supplierId}/supplier-invoice-links/delete";
private string GetSplitMultiPackageByQuantityUrl(long _shipmentPackageId) => $"{GetBaseUrl}suppliers/{_supplierId}/shipment-packages/{_shipmentPackageId}/split-packages";
private string GetSplitMultiShipmentPackageUrl(long _shipmentPackageId) => $"{GetBaseUrl}suppliers/{_supplierId}/shipment-packages/{_shipmentPackageId}/split";
private string GetSplitShipmentPackageUrl(long _shipmentPackageId) => $"{GetBaseUrl}suppliers/{_supplierId}/shipment-packages/{_shipmentPackageId}/multi-split";
private string GetSplitShipmentPackageByQuantityUrl(long _shipmentPackageId) => $"{GetBaseUrl}suppliers/{_supplierId}/shipment-packages/{_shipmentPackageId}/quantity-split";
private string GetUpdateBoxInfoUrl(long _shipmentPackageId) => $"{GetBaseUrl}suppliers/{_supplierId}/shipment-packages/{_shipmentPackageId}/box-info";
private string GetShipmentPackagesUrl() => $"{GetBaseUrl()}suppliers/{_supplierId}/orders";
private string GetUpdateTrackingNumberUrl(long shipmentPackageId) => $"{GetBaseUrl()}suppliers/{_supplierId}/{shipmentPackageId}/update-tracking-number";
private string GetUpdatePackageUrl(long shipmentPackageId) => $"{GetBaseUrl()}suppliers/{_supplierId}//shipment-packages/{shipmentPackageId}";
private string GetAddInvoiceLinkUrl() => $"{GetBaseUrl()}suppliers/{_supplierId}/supplier-invoice-links";
private string GetDeleteInvoiceLinkUrl() => $"{GetBaseUrl()}suppliers/{_supplierId}/supplier-invoice-links/delete";
private string GetSplitMultiPackageByQuantityUrl(long _shipmentPackageId) => $"{GetBaseUrl()}suppliers/{_supplierId}/shipment-packages/{_shipmentPackageId}/split-packages";
private string GetSplitMultiShipmentPackageUrl(long _shipmentPackageId) => $"{GetBaseUrl()}suppliers/{_supplierId}/shipment-packages/{_shipmentPackageId}/split";
private string GetSplitShipmentPackageUrl(long _shipmentPackageId) => $"{GetBaseUrl()}suppliers/{_supplierId}/shipment-packages/{_shipmentPackageId}/multi-split";
private string GetSplitShipmentPackageByQuantityUrl(long _shipmentPackageId) => $"{GetBaseUrl()}suppliers/{_supplierId}/shipment-packages/{_shipmentPackageId}/quantity-split";
private string GetUpdateBoxInfoUrl(long _shipmentPackageId) => $"{GetBaseUrl()}suppliers/{_supplierId}/shipment-packages/{_shipmentPackageId}/box-info";
public TrendyolPackageIntegration(string supplierId, string apiKey, string apiSecret, bool isInProduction, string entegratorFirm) : base(supplierId, apiKey, apiSecret, isInProduction, entegratorFirm)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
namespace Integration.Marketplaces.Trendyol.Infrastructure.ProductIntegration;
public class TrendyolProductIntegration : TrendyolIntegrationBase, ITrendyolProductIntegration, IMarketplaceIntegration
{
private string GetCreateProducsUrl() => $"{GetBaseUrl}suppliers/{_supplierId}/v2/products";
private string GetSupplierAddressUrl() => $"{GetBaseUrl}suppliers/{_supplierId}/addresses";
private string GetBrandsUrl() => $"{GetBaseUrl}brands";
private string GetCategoryTreeUrl() => $"{GetBaseUrl}product-categories";
private string GetCategoryAttributesUrl(int categoryId) => $"{GetBaseUrl}product-categories/{categoryId}/attributes";
private string GetUpdateProductUrl() => $"{GetBaseUrl}suppliers/{_supplierId}/v2/products";
private string GetUpdatePriceAndStockUrl() => $"{GetBaseUrl}suppliers/{_supplierId}/products/price-and-inventory";
private string GetDeleteProductUrl() => $"{GetBaseUrl}suppliers/{_supplierId}/v2/products";
private string GetBatchRequestResultUrl(string batchRequestId) => $"{GetBaseUrl}suppliers/{_supplierId}/products/batch-requests/{batchRequestId}";
private string GetFilterProductsUrl() => $"{GetBaseUrl}suppliers/{_supplierId}/products";
private string GetCreateProducsUrl() => $"{GetBaseUrl()}suppliers/{_supplierId}/v2/products";
private string GetSupplierAddressUrl() => $"{GetBaseUrl()}suppliers/{_supplierId}/addresses";
private string GetBrandsUrl() => $"{GetBaseUrl()}brands";
private string GetCategoryTreeUrl() => $"{GetBaseUrl()}product-categories";
private string GetCategoryAttributesUrl(int categoryId) => $"{GetBaseUrl()}product-categories/{categoryId}/attributes";
private string GetUpdateProductUrl() => $"{GetBaseUrl()}suppliers/{_supplierId}/v2/products";
private string GetUpdatePriceAndStockUrl() => $"{GetBaseUrl()}suppliers/{_supplierId}/products/price-and-inventory";
private string GetDeleteProductUrl() => $"{GetBaseUrl()}suppliers/{_supplierId}/v2/products";
private string GetBatchRequestResultUrl(string batchRequestId) => $"{GetBaseUrl()}suppliers/{_supplierId}/products/batch-requests/{batchRequestId}";
private string GetFilterProductsUrl() => $"{GetBaseUrl()}suppliers/{_supplierId}/products";
public TrendyolProductIntegration(string supplierId, string apiKey, string apiSecret, bool isInProduction, string entegratorFirm) : base(supplierId, apiKey, apiSecret, isInProduction, entegratorFirm)
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Text;
using System.Text.Json;
using Integration.Hub;

namespace Integration.Marketplaces.Trendyol.Infrastructure;
Expand All @@ -18,6 +20,18 @@ public TrendyolIntegrationBase(string supplierId, string apiKey, string apiSecre
_apiSecret = apiSecret ?? throw new ArgumentNullException(nameof(apiSecret));
_isInProduction = isInProduction;
_entegratorFirm = entegratorFirm;

IntializeDefaultHeaders(new Dictionary<string, string>
{
{ "Accept", "application/json" },
{ "User-Agent", $"{_supplierId} - {_entegratorFirm}" },
{ "Authorization", $"Basic {Convert.ToBase64String(Encoding.ASCII.GetBytes($"{_apiKey}:{_apiSecret}"))}"}
});

SetSerializerOptions(new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
});
}

public string GetBaseUrl() => _isInProduction ? ProdBaseUrl : StageBaseUrl;
Expand Down

0 comments on commit 7b766b8

Please sign in to comment.