1 | <?php |
2 | |
3 | namespace App\Components\Mintegral; |
4 | |
5 | use App\Entity\MintegralAccount; |
6 | use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; |
7 | use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; |
8 | use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; |
9 | use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
10 | use Symfony\Contracts\HttpClient\HttpClientInterface; |
11 | |
12 | class MintegralClient |
13 | { |
14 | private HttpClientInterface $httpClient; |
15 | |
16 | public const METHOD = 'GET'; |
17 | |
18 | public const ENDPOINT = 'https://ss-api.mintegral.com/api/v2/reports/data'; |
19 | |
20 | public const FORMAT = 'json'; |
21 | |
22 | public $time = '0'; |
23 | |
24 | private int $retry = 0; |
25 | |
26 | public function __construct(HttpClientInterface $httpClient) |
27 | { |
28 | $this->httpClient = $httpClient; |
29 | } |
30 | |
31 | public function acquiringApps(MintegralAccount $account, array $packageNames) |
32 | { |
33 | $time = time(); |
34 | |
35 | $response = $this->httpClient->request( |
36 | 'POST', |
37 | 'https://ss-api.mintegral.com/api/open/v1/target-apps/app-name', |
38 | [ |
39 | 'query' => [ |
40 | 'package_name' => implode(',', $packageNames), |
41 | ], |
42 | 'headers' => [ |
43 | 'Content-Type' => 'application/json', |
44 | 'access-key' => $account->getAccessKey(), |
45 | 'token' => md5($account->getToken() . md5($time)), |
46 | 'timestamp' => $time, |
47 | ] |
48 | ] |
49 | ); |
50 | |
51 | if ($response->getStatusCode() === 200) { |
52 | $data = $response->toArray(); |
53 | |
54 | return $data['data']; |
55 | } |
56 | |
57 | throw new \RuntimeException('Mintegral: ' . $response->getContent()); |
58 | } |
59 | |
60 | public function acquiringCampaigns(MintegralAccount $account, array $campaignIds) |
61 | { |
62 | $time = time(); |
63 | |
64 | $response = $this->httpClient->request( |
65 | 'GET', |
66 | 'https://ss-api.mintegral.com/api/open/v1/campaign', |
67 | [ |
68 | 'query' => [ |
69 | 'campaign_id' => implode(',', $campaignIds), |
70 | 'limit' => 50, |
71 | ], |
72 | 'headers' => [ |
73 | 'Content-Type' => 'application/json', |
74 | 'access-key' => $account->getAccessKey(), |
75 | 'token' => md5($account->getToken() . md5($time)), |
76 | 'timestamp' => $time, |
77 | ] |
78 | ] |
79 | ); |
80 | |
81 | if ($response->getStatusCode() === 200) { |
82 | $data = $response->toArray(); |
83 | |
84 | return $data['data']; |
85 | } |
86 | |
87 | throw new \RuntimeException('Mintegral: ' . $response->getContent()); |
88 | } |
89 | |
90 | public function getExpenses(MintegralAccount $account, \DateTime $day, int $type = 1) |
91 | { |
92 | if ($this->retry >= 20) { |
93 | throw new \RuntimeException('Mintegral: retry limit.'); |
94 | } |
95 | |
96 | try { |
97 | $response = $this->httpClient->request( |
98 | 'GET', |
99 | self::ENDPOINT, |
100 | [ |
101 | 'query' => $this->getParams($account, $day, $type), |
102 | 'headers' => [ |
103 | 'Content-Type' => 'application/json', |
104 | ] |
105 | ] |
106 | ); |
107 | |
108 | if ($type === 1) { |
109 | $result = $response->toArray(); |
110 | |
111 | if ($result['code'] === 10000) { |
112 | throw new \RuntimeException('Mintegral: code 10000'); |
113 | } |
114 | |
115 | if ($result['code'] === 200) { |
116 | $this->retry = 0; |
117 | return $this->getExpenses($account, $day, 2); |
118 | } |
119 | |
120 | sleep(30); |
121 | $this->retry++; |
122 | |
123 | return $this->getExpenses($account, $day, 1); |
124 | } |
125 | |
126 | return $this->parseData($response->getContent()); |
127 | } catch (\Exception | ClientExceptionInterface | RedirectionExceptionInterface | ServerExceptionInterface | TransportExceptionInterface $e) { |
128 | throw new \RuntimeException($e->getMessage()); |
129 | } |
130 | } |
131 | |
132 | private function parseData(string $content): array |
133 | { |
134 | $lines = str_getcsv($content, PHP_EOL); |
135 | |
136 | $data = []; |
137 | |
138 | foreach ($lines as $key => $line) { |
139 | $r = str_getcsv($line, "\t"); |
140 | |
141 | if ($key === 0) { |
142 | $data['headers'] = $r; |
143 | } else { |
144 | $data['rows'][$key] = $r; |
145 | } |
146 | } |
147 | |
148 | return $data; |
149 | } |
150 | |
151 | public function getParams(MintegralAccount $account, \DateTime $day, int $type): array |
152 | { |
153 | $time = time(); |
154 | |
155 | return [ |
156 | 'access-key' => $account->getAccessKey(), |
157 | 'token' => md5($account->getToken() . md5($time)), |
158 | 'timestamp' => $time, |
159 | 'start_time' => $day->format('Y-m-d'), |
160 | 'end_time' => $day->format('Y-m-d'), |
161 | 'timezone' => $this->time, |
162 | 'type' => $type, |
163 | 'dimension_option' => 'Campaign,Offer,Sub,Location', |
164 | ]; |
165 | } |
166 | } |