using AvaskTest2.Shared.Models; using AvaskTest2.Shared.Models.Shopify; using AvaskTest2.Shared.Services; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Net.Http.Json; using System.Threading.Tasks; using System.Web; namespace AvaskTest2.Client.Services { public class ShopifyService : IShopifyService { private readonly HttpClient _httpClient; public ShopifyService(HttpClient httpClient) => _httpClient = httpClient; public async Task ShopifyAuthUrlAsync(string userShop) { // Azure Function call "api/shopifyauthurl", via controller for test-bed try { var request = new ShopifyAuthUrlRequest(new User { CompanyId = "AVASK-SOFTWARE" }, "infiniti-system-client", userShop); //string sTemp = JsonConvert.SerializeObject(request); //### test only var response = await _httpClient.PostAsJsonAsync("api/shopify/shopifyauthurl", request); var content = await response.Content.ReadAsStringAsync(); var authResp = JsonConvert.DeserializeObject(content); return authResp?.AuthURL ?? "no response"; } catch (Exception ex) { return $"Exception: {ex.Message}"; } } public async Task ShopifyAuthCallBack(ShopifyAuthCallbackGet authCallbackGet) { // Azure Function call "api/shopifyauthcallback", via controller for test-bed try { var request = new ShopifyAuthCallbackRequest(new User { CompanyId = "AVASK-SOFTWARE" }, "infiniti-system-client", authCallbackGet); var response = await _httpClient.PostAsJsonAsync("api/shopify/shopifyauthcallback", request); var content = await response.Content.ReadAsStringAsync(); var shopifyResp = JsonConvert.DeserializeObject(content); return shopifyResp?.ShopifyAuthReturn; } catch (Exception ex) { return new ShopifyAuthReturn { token_type = "Exception", access_token = ex.Message }; } } public async Task ShopifyFetchSalesAsync(int year, int month, int day) { // Azure Function call "api/shopifyfetchsales", via controller for test-bed try { var datespan = new SalesDatespan { Year = year, Month = month, Day = day }; var request = new ShopifyFetchSalesRequest(new User { CompanyId = "AVASK-SOFTWARE" }, "infiniti-system-client", datespan); var response = await _httpClient.PostAsJsonAsync("api/shopify/shopifyfetchsales", request); var content = await response.Content.ReadAsStringAsync(); var salesReturn = JsonConvert.DeserializeObject(content); return salesReturn?.ShopifySalesResponse; } catch (Exception ex) { return new ShopifySalesResponse { count = -1, next_page_info = $"Exception: {ex.Message}" }; } } public async Task ShopifyGetSalesAsync(int year, int month, int day) { // Azure Function call "api/shopifygetsales", via controller for test-bed try { var datespan = new SalesDatespan { Year = year, Month = month, Day = day }; var request = new ShopifyGetSalesRequest(new User { CompanyId = "AVASK-SOFTWARE" }, "infiniti-system-client", datespan); var response = await _httpClient.PostAsJsonAsync("api/shopify/shopifygetsales", request); var content = await response.Content.ReadAsStringAsync(); return JsonConvert.DeserializeObject(content); } catch (Exception ex) { return new ShopifyGetSalesResponse(-1, null, $"Exception: {ex.Message}"); } } } }