PushDataReportPush.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. }
  24. public PushDataReportPush(string url, string app_key, string app_secret)
  25. {
  26. _url = url;
  27. _app_key = app_key;
  28. _app_secret = app_secret;
  29. }
  30. public async Task<ResponseDTO> PushReportData(PushDataReportDTO item, CancellationToken cancellationToken = default)
  31. {
  32. try
  33. {
  34. var options = new JsonSerializerOptions
  35. {
  36. PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
  37. WriteIndented = true
  38. };
  39. string json = JsonSerializer.Serialize(item, options);
  40. var root = await Request("/cps/data/report", json, cancellationToken);
  41. var response = root.GetRawText().Convert2Object<ResponseDTO>();
  42. return response;
  43. }
  44. catch (Exception ex)
  45. {
  46. return new ResponseDTO
  47. {
  48. code = 500,
  49. message = ex.Message
  50. };
  51. }
  52. }
  53. public async Task<JsonElement> Request(string action, string post, CancellationToken cancellationToken = default)
  54. {
  55. string url = $"{_url}{action}";
  56. long ts = DateTime.Now.Convert2UnixTimestamp(true);
  57. string sign = $"{_app_key}{_app_secret}{ts}".ToSHA1();
  58. string token = $"{_app_key}:{ts}:{sign}".ToBase64();
  59. try
  60. {
  61. WebClientUtility client = new WebClientUtility();
  62. client.SetContentType("application/json");
  63. client.SetHeaders(new Dictionary<string, string> { { "Authorization", $"Bearer {token}" } });
  64. client.Post(post);
  65. var response = await client.RequestAsync(url, "POST", cancellationToken);
  66. if (!response.Successed)
  67. {
  68. throw response.ResponseException;
  69. }
  70. var body = response.Body();
  71. await new LoggerLibrary("push_report")
  72. .Info(action, post)
  73. .Info(body)
  74. .SaveAsync();
  75. var root = body.Convert2JsonElement();
  76. return root;
  77. }
  78. catch (Exception ex)
  79. {
  80. await new LoggerLibrary("push_report", "error")
  81. .Info(action, post)
  82. .Info(ex.Message, ex.StackTrace)
  83. .SaveAsync();
  84. throw;
  85. }
  86. }
  87. }
  88. }