I have an ASP.NET Core Web API that get requests a few different vendors for their products and data related to those products. I then return those results for my front end when my shop page does its requests to my backend API. However, each vendor has different property names for their API results so returning them as is to my front end seems like bad practice. This is mainly just a learning exercise doing this web app so I am trying to do best practices as such I have implemented an interface called IProduct and extend this for each vendor. However, for their GetProducts() method I need to bind each of their results to the same model so it conforms to what the front end is expected. An example of one vendor is as follows:
public interface IFulfillmentService
{
Task<string> GetProducts();
}
public class Printful : IFulfillmentService
{
private readonly string PrintfulAPIKey;
private readonly IHttpClientFactory _httpClientFactory;
public Printful(IConfiguration configuration, IHttpClientFactory httpClientFactory)
{
PrintfulAPIKey = configuration.GetValue<string>("PrintfulAPIKey") ?? "";
_httpClientFactory = httpClientFactory;
}
public async Task<string> GetProducts()
{
using (var httpClient = _httpClientFactory.CreateClient())
{
var PrintfulProductsHTTP = await httpClient.GetAsync("https://api.printful.com/products");
var PrintfulProductsJson = await PrintfulProductsHTTP.Content.ReadAsStringAsync();
return PrintfulProductsJson;
}
}
}
My controller returns the results currently exactly as the vendor sends it:
namespace Company_API.Controllers;
[ApiController]
[Route("api/[controller]")]
public class ProductsController : BaseController
{
public ProductsController(IConfiguration configuration, ILogger<BaseController> logger, IEmailService emailService, IHttpClientFactory httpClientFactory, IOptions<ReCaptchaSettings> reCaptchaSettings, IFulfillmentService printful) : base(configuration, logger, emailService, httpClientFactory, reCaptchaSettings, printful)
{
}
[HttpGet(Name = "GetProducts")]
[EnableRateLimiting("api")]
public async Task<IActionResult> Get()
{
return Ok(await _printful.GetProducts());
}
}
As a simple example the model will only consist of a few items such as image, pricing, product name. So I need to figure out a way to bind different HTTP request to the same model because I did not want to have a separate model for each vendor since the front end will need to use the same. One vendor may call image image_product and the second might call it image and one may call it file. So its as if I need to be able to allow multiple JSON properties for the same model. Is this possible?