ProductModel.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using economy.Helpers;
  2. namespace economy.Models.Product
  3. {
  4. public class ProductModel
  5. {
  6. private readonly DataGoKR _dataGoKR;
  7. public ProductModel(DataGoKR dataGoKR)
  8. {
  9. _dataGoKR = dataGoKR;
  10. }
  11. // 품목별 가격 목록 조회
  12. public async Task<List.Response> GetPriceItemList(List.Request request)
  13. {
  14. List.Response parsedData = new();
  15. try
  16. {
  17. var response = await _dataGoKR.httpClient.GetAsync($"/1240000/bpp_openapi/getPriceItemList?ServiceKey={_dataGoKR.APIKey}&pageNo={request.PageNo}&numOfRows={request.NumOfRows}");
  18. if (response.IsSuccessStatusCode)
  19. {
  20. var xmlString = await response.Content.ReadAsStringAsync();
  21. parsedData = await Common.ParseXmlDataAsync<List.Response>(xmlString);
  22. if (parsedData.Body is null)
  23. {
  24. return parsedData;
  25. }
  26. }
  27. response.EnsureSuccessStatusCode(); // 예외 발생시킴
  28. }
  29. catch (HttpRequestException e)
  30. {
  31. Console.WriteLine($"Request error: {e.Message}");
  32. }
  33. return parsedData;
  34. }
  35. // 품목 상세 정보 조회
  36. public async Task<Detail.Response> GetPriceItemInfo(Detail.Request request)
  37. {
  38. Detail.Response parseData = new();
  39. try
  40. {
  41. var uriBuilder = new UriBuilder(_dataGoKR.APIUrl)
  42. {
  43. Path = "/1240000/bpp_openapi/getPriceInfo",
  44. Query = $"ServiceKey={_dataGoKR.APIKey}&pageNo={request.PageNo}&numOfRows={request.NumOfRows}&startDate={request.StartDate:yyyy-MM-dd}&endDate={request.EndDate:yyyy-MM-dd}&itemCode={request.ItemCode}"
  45. };
  46. var response = await _dataGoKR.httpClient.GetAsync(uriBuilder.Uri);
  47. if (response.IsSuccessStatusCode)
  48. {
  49. var xmlString = await response.Content.ReadAsStringAsync();
  50. parseData = await Common.ParseXmlDataAsync<Detail.Response>(xmlString);
  51. if (parseData.Body is null)
  52. {
  53. return new Detail.Response();
  54. }
  55. }
  56. response.EnsureSuccessStatusCode();
  57. }
  58. catch (HttpRequestException e)
  59. {
  60. Console.WriteLine($"Request error: {e.Message}");
  61. }
  62. return parseData;
  63. }
  64. }
  65. }