Passed
Push — master ( dc7252...bfdea4 )
by Dmitry
15:34 queued 18s
created

Leads::getTestLeads()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Promopult\TikTokMarketingApi\Service;
6
7
use Throwable;
8
use function json_decode;
9
10
final class Leads extends \Promopult\TikTokMarketingApi\AbstractService
11
{
12
    /**
13
     * Create test lead
14
     *
15
     * After the subscription, you can use the endpoints below to perform an end-to-end testing for leads generation,
16
     * and clear the testing lead.
17
     *
18
     * @param int $advertiserId
19
     * @param int $pageId
20
     * @return array
21
     *
22
     * @throws Throwable
23
     *
24
     * @see https://ads.tiktok.com/marketing_api/docs?id=1701890943400962
25
     */
26
    public function createTestLead(
27
        int $advertiserId,
28
        int $pageId
29
    ): array {
30
        return $this->requestApi(
31
            'POST',
32
            '/open_api/v1.2/pages/leads/mock/create/',
33
            [
34
                'advertiser_id' => $advertiserId,
35
                'page_id' => $pageId
36
            ]
37
        );
38
    }
39
40
    /**
41
     * Get test leads
42
     *
43
     * You can use this endpoint to get a test lead.
44
     *
45
     * @param int $advertiserId
46
     * @param int $pageId
47
     * @return array
48
     *
49
     * @throws Throwable
50
     *
51
     * @see https://ads.tiktok.com/marketing_api/docs?id=1709486980307969
52
     */
53
    public function getTestLeads(
54
        int $advertiserId,
55
        int $pageId
56
    ): array {
57
        return $this->requestApi(
58
            'GET',
59
            '/open_api/v1.2/pages/leads/mock/get/',
60
            [
61
                'advertiser_id' => $advertiserId,
62
                'page_id' => $pageId
63
            ]
64
        );
65
    }
66
67
    /**
68
     * Delete a test lead
69
     *
70
     * You can use this endpoint to delete an existing test lead.
71
     *
72
     * @param int $advertiserId
73
     * @param int $leadId
74
     * @return array
75
     *
76
     * @throws Throwable
77
     *
78
     * @see https://ads.tiktok.com/marketing_api/docs?id=1709487026425858
79
     */
80
    public function deleteTestLead(
81
        int $advertiserId,
82
        int $leadId
83
    ): array {
84
        return $this->requestApi(
85
            'POST',
86
            '/open_api/v1.2/pages/leads/mock/delete/',
87
            [
88
                'advertiser_id' => $advertiserId,
89
                'lead_id' => $leadId
90
            ]
91
        );
92
    }
93
94
    /**
95
     * Create lead download task
96
     *
97
     * @param int $advertiserId
98
     * @param int|null $adId
99
     * @param int|null $pageId
100
     * @return array
101
     *
102
     * @throws Throwable
103
     *
104
     * @see https://ads.tiktok.com/marketing_api/docs?id=1701890942344193
105
     */
106
    public function createLeadDownloadTask(
107
        int $advertiserId,
108
        ?int $adId = null,
109
        ?int $pageId = null
110
    ): array {
111
        return $this->requestApi(
112
            'GET',
113
            '/open_api/v1.2/pages/leads/task/',
114
            [
115
                'advertiser_id' => $advertiserId,
116
                'ad_id' => $adId,
117
                'page_id' => $pageId,
118
            ]
119
        );
120
    }
121
122
    /**
123
     * Get lead download task for status check
124
     *
125
     * Please note that the leads file will expire in 10 minutes after it was generated. You will get a file not exist
126
     * error if the file has expired.
127
     *
128
     * @param int $advertiserId
129
     * @param int $taskId
130
     * @return array
131
     *
132
     * @throws Throwable
133
     *
134
     * @see https://ads.tiktok.com/marketing_api/docs?id=1701890942344193
135
     */
136
    public function getLeadDownloadTask(
137
        int $advertiserId,
138
        int $taskId
139
    ): array {
140
        return $this->requestApi(
141
            'GET',
142
            '/open_api/v1.2/pages/leads/task/',
143
            [
144
                'advertiser_id' => $advertiserId,
145
                'task_id' => $taskId
146
            ]
147
        );
148
    }
149
150
    /**
151
     * Download leads
152
     *
153
     * @param int $advertiserId
154
     * @param int $taskId
155
     *
156
     * @return string
157
     *
158
     * @throws Throwable
159
     *
160
     * @see https://ads.tiktok.com/marketing_api/docs?id=1709486183758850
161
     */
162
    public function downloadLeads(
163
        int $advertiserId,
164
        int $taskId
165
    ): string {
166
        $url = $this->credentials->getApiBaseUrl()
167
            . '/open_api/v1.2/pages/leads/task/download/'
168
            . '?' . $this->prepareGetParams([
169
                'advertiser_id' => $advertiserId,
170
                'task_id' => $taskId
171
            ]);
172
173
        $headers = [
174
            'Access-Token' => $this->credentials->getAccessToken(),
175
        ];
176
177
        $request = new \GuzzleHttp\Psr7\Request(
178
            'GET',
179
            $url,
180
            $headers,
181
            null
182
        );
183
184
        $response = $this->httpClient->sendRequest($request);
185
186
        /** @var array $decodedJson */
187
        $decodedJson = json_decode(
188
            (string) $response->getBody(),
189
            true,
190
            512,
191
            JSON_THROW_ON_ERROR
192
        );
193
194
        if (
195
            isset($decodedJson['code'])
196
            && $decodedJson['code'] != 0
197
        ) {
198
            throw new \Promopult\TikTokMarketingApi\Exception\ErrorResponse(
199
                (int) $decodedJson['code'],
200
                (string) $decodedJson['message'],
201
                $request,
202
                $response
203
            );
204
        }
205
206
        return (string) $response->getBody();
207
    }
208
209
    /**
210
     * Subscribe to leads
211
     *
212
     * You can subscribe to a page (instant form) to get leads generated from the page. If you do not want to receive
213
     * leads from a page any more, you can cancel the subscription.
214
     *
215
     * You can also get all subscriptions for an advertiser.
216
     *
217
     * You can subscribe to leads that are generated from a lead ad (instant form). Subscriptions are organized based on
218
     * Subcription IDs (subscription_id), not page IDs (page_id). Subscriptions are independent. A page can be included
219
     * in multiple subscriptions. Subscription status changes of a page in one subscription do not affect the
220
     * subscription status of this page in another subscription.
221
     *
222
     * @param int $appId
223
     * @param string $secret
224
     * @param array $subscriptionDetail
225
     * @param string $url
226
     * @param string $object
227
     * @return array
228
     *
229
     * @throws Throwable
230
     *
231
     * @see https://ads.tiktok.com/marketing_api/docs?id=1701890942854146
232
     */
233
    public function subscribeToLeads(
234
        int $appId,
235
        string $secret,
236
        array $subscriptionDetail,
237
        string $url,
238
        string $object = 'LEAD'
239
    ): array {
240
        return $this->requestApi(
241
            'POST',
242
            '/open_api/v1.2/subscription/subscribe/',
243
            [
244
                'app_id' => $appId,
245
                'secret' => $secret,
246
                'subscription_detail' => $subscriptionDetail,
247
                'object' => $object,
248
                'url' => $url
249
            ]
250
        );
251
    }
252
253
    /**
254
     * Cancel subscription
255
     *
256
     * @param int $appId
257
     * @param string $secret
258
     * @param int $subscriptionId
259
     * @return array
260
     *
261
     * @throws Throwable
262
     *
263
     * @see https://ads.tiktok.com/marketing_api/docs?id=1709486460752897
264
     */
265
    public function cancelSubscription(
266
        int $appId,
267
        string $secret,
268
        int $subscriptionId
269
    ): array {
270
        return $this->requestApi(
271
            'POST',
272
            '/open_api/v1.2/subscription/unsubscribe/',
273
            [
274
                'app_id' => $appId,
275
                'secret' => $secret,
276
                'subscription_id' => $subscriptionId,
277
            ]
278
        );
279
    }
280
281
    /**
282
     * Get subscriptions
283
     *
284
     * @param int $appId            # The APP ID
285
     * @param string $secret        # Secret
286
     * @param string $object        # The object that you want to subscribe to. Use LEAD for this endpoint
287
     * @param int|null $page        # The current page number. Default value: 1. Value range: ≥ 1
288
     * @param int|null $pageSize    # The page size. Default value:10. Value range: 1-1000
289
     *
290
     * @return array
291
     *
292
     * @throws Throwable
293
     */
294
    public function getSubscriptions(
295
        int $appId,
296
        string $secret,
297
        string $object = 'LEAD',
298
        ?int $page = null,
299
        ?int $pageSize = null
300
    ): array {
301
        return $this->requestApi(
302
            'GET',
303
            '/open_api/v1.2/subscription/get/',
304
            [
305
                'app_id' => $appId,
306
                'secret' => $secret,
307
                'object' => $object,
308
                'page' => $page,
309
                'page_size' => $pageSize
310
            ]
311
        );
312
    }
313
}
314