PushDataReportPush.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc.Controllers;
  3. using Microsoft.AspNetCore.Mvc.Filters;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using dodohold.core;
  9. using System.Xml.Linq;
  10. using YunhuiKit;
  11. using System.Text.Json;
  12. using COSXML.Network;
  13. using molilian.core.PushDataReport;
  14. namespace molilian.core
  15. {
  16. public class PushDataReportPush
  17. {
  18. private string _url = "https://adx.ads.heytapmobi.com";
  19. private string _app_key = "mll";
  20. private string _app_secret = "fHTPd7RK7OnpIv7zGvPgL2Wv8P3pDYfW";
  21. public PushDataReportPush()
  22. {
  23. #if DEBUG
  24. _url = "http://adx-ads-test.wanyol.com";
  25. #endif
  26. }
  27. public PushDataReportPush(string url, string app_key, string app_secret)
  28. {
  29. #if DEBUG
  30. _url = "http://adx-ads-test.wanyol.com";
  31. #endif
  32. _url = url;
  33. _app_key = app_key;
  34. _app_secret = app_secret;
  35. }
  36. public async Task<ResponseDTO> PushReportData(PushDataReportDTO item, CancellationToken cancellationToken = default)
  37. {
  38. try
  39. {
  40. var options = new JsonSerializerOptions
  41. {
  42. PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
  43. WriteIndented = true
  44. };
  45. string json = JsonSerializer.Serialize(item, options);
  46. var root = await Request("/cps/data/report", json, cancellationToken);
  47. var response = root.GetRawText().Convert2Object<ResponseDTO>();
  48. return response;
  49. }
  50. catch (Exception ex)
  51. {
  52. return new ResponseDTO
  53. {
  54. code = 500,
  55. message = ex.Message
  56. };
  57. }
  58. }
  59. public async Task<JsonElement> Request(string action, string post, CancellationToken cancellationToken = default)
  60. {
  61. string url = $"{_url}{action}";
  62. long ts = DateTime.Now.Convert2UnixTimestamp(true);
  63. string sign = $"{_app_key}{_app_secret}{ts}".ToSHA1();
  64. string token = $"{_app_key}:{ts}:{sign}".ToBase64();
  65. try
  66. {
  67. WebClientUtility client = new WebClientUtility();
  68. client.SetContentType("application/json");
  69. client.SetHeaders(new Dictionary<string, string> { { "Authorization", $"Bearer {token}" } });
  70. client.Post(post);
  71. var response = await client.RequestAsync(url, "POST", cancellationToken);
  72. if (!response.Successed)
  73. {
  74. throw response.ResponseException;
  75. }
  76. var body = response.Body();
  77. await new LoggerLibrary("push_report")
  78. .Info(action, post)
  79. .Info(body)
  80. .SaveAsync();
  81. var root = body.Convert2JsonElement();
  82. return root;
  83. }
  84. catch (Exception ex)
  85. {
  86. await new LoggerLibrary("push_report", "error")
  87. .Info(action, post)
  88. .Info(ex.Message, ex.StackTrace)
  89. .SaveAsync();
  90. throw;
  91. }
  92. }
  93. }
  94. }