-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInstasentController.php
More file actions
124 lines (102 loc) · 3.67 KB
/
Copy pathInstasentController.php
File metadata and controls
124 lines (102 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* Instasent Integration
*/
namespace BitApps\Integrations\Actions\Instasent;
use BitApps\Integrations\Core\Util\HttpHelper;
use WP_Error;
/**
* Provide functionality for Instasent integration
*/
class InstasentController
{
private static $_baseUrl = 'https://api.instasent.com';
public function authorize($refreshFieldsRequestParams)
{
if (empty($refreshFieldsRequestParams->auth_token)) {
wp_send_json_error(
__(
'Requested parameter is empty',
'bit-integrations'
),
400
);
}
$endpoint = self::$_baseUrl . '/organization/account';
$header = [
'Authorization' => 'Bearer ' . $refreshFieldsRequestParams->auth_token,
'Content-Type' => 'application/json',
'Accept' => 'application/json',
];
$response = HttpHelper::get($endpoint, null, $header);
if (HttpHelper::$responseCode == 200) {
wp_send_json_success('Authorization Successful', 200);
return;
}
wp_send_json_error(
$response->message ?? $response ?? 'Authorization Failed',
400
);
}
public function refreshDatasources($refreshFieldsRequestParams)
{
if (empty($refreshFieldsRequestParams->auth_token) || empty($refreshFieldsRequestParams->projectId)) {
wp_send_json_error(
__('Requested parameter is empty', 'bit-integrations'),
400
);
}
$project = rawurlencode($refreshFieldsRequestParams->projectId);
$endpoint = self::$_baseUrl . "/v1/project/{$project}/datasource";
$header = [
'Authorization' => 'Bearer ' . $refreshFieldsRequestParams->auth_token,
'Content-Type' => 'application/json',
'Accept' => 'application/json',
];
$response = HttpHelper::get($endpoint, null, $header);
if (HttpHelper::$responseCode == 200) {
$entities = $response->entities ?? [];
$formattedResponse = [];
foreach ($entities as $entity) {
$formattedResponse[] = [
'id' => $entity->id ?? '',
'name' => $entity->name ?? ($entity->id ?? ''),
];
}
wp_send_json_success($formattedResponse, 200);
return;
}
wp_send_json_error(
$response->message ?? $response ?? __('Failed to fetch data sources', 'bit-integrations'),
400
);
}
public function execute($integrationData, $fieldValues)
{
$integrationDetails = $integrationData->flow_details;
$integId = $integrationData->id;
$auth_token = $integrationDetails->auth_token ?? '';
$fieldMap = $integrationDetails->field_map ?? '';
$action = $integrationDetails->action ?? '';
if (
empty($fieldMap)
|| empty($auth_token)
|| empty($action)
) {
// translators: %s: Placeholder value
return new WP_Error('REQ_FIELD_EMPTY', wp_sprintf(__('module, fields are required for %s api', 'bit-integrations'), 'Instasent'));
}
$recordApiHelper = new RecordApiHelper($auth_token, $integrationDetails, $integId);
$instasentApiResponse = $recordApiHelper->execute(
$integrationDetails,
$fieldValues,
$fieldMap,
$auth_token,
$action
);
if (is_wp_error($instasentApiResponse)) {
return $instasentApiResponse;
}
return $instasentApiResponse;
}
}