This repository has been archived by the owner on Nov 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackmash-action.php
154 lines (125 loc) · 2.77 KB
/
stackmash-action.php
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
function stackmash_action($category, $title, $body = [])
{
// Get the options
$options = get_option('stackmash');
$data = [
"public_key" => $options['public_key'],
"private_key" => $options['private_key'],
"category" => $category,
"title" => $title,
"body" => json_encode($body)
];
$response = stackmash_post($data);
if(!stackmash_hasErrors($response))
{
return stackmash_toObject($response);
} else {
try
{
return stackmash_error($response);
} catch(Exception $e) {
return $e;
}
}
}
function stackmash_post($data)
{
$response = [];
$opts = [];
$opts[CURLOPT_URL] = 'https://api.stackmash.com/api/notification/create';
$opts[CURLOPT_RETURNTRANSFER] = true;
$opts[CURLOPT_POST] = true;
$opts[CURLOPT_POSTFIELDS] = $data;
$opts[CURLOPT_HTTPHEADER] = ['Accept: application/json'];
$tries = 0;
while(true)
{
$ch = curl_init();
curl_setopt_array($ch, $opts);
$response['raw'] = curl_exec($ch);
$response['errorNumber'] = curl_errno($ch);
$response['errorMessage'] = curl_error($ch);
$response['responseCode'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if(stackmash_shouldRetry($response, $tries))
{
$tries = $tries + 1;
} else {
break;
}
}
return $response;
}
function stackmash_toObject($response)
{
return json_decode($response['raw']);
}
function stackmash_hasErrors($response)
{
if($response['raw'] === false)
{
return true;
} else {
if($response['responseCode'] != 200 && $response['responseCode'] != 201)
{
return true;
}
}
return false;
}
function stackmash_error($response)
{
switch($response['responseCode'])
{
case 401:
throw new Exception($response['raw']);
break;
case 403:
throw new Exception($response['raw']);
break;
case 404:
throw new Exception($response['raw']);
break;
case 422:
throw new Exception($response['raw']);
break;
case 500:
throw new Exception($response['raw']);
break;
}
switch($response['errorNumber'])
{
case CURLE_COULDNT_CONNECT:
case CURLE_OPERATION_TIMEOUTED:
case CURLE_COULDNT_RESOLVE_HOST:
throw new Exception('{"message":"Connection error: ' . $response['errorMessage'] . '"}');
break;
case CURLE_SSL_CACERT:
case CURLE_SSL_PEER_CERTIFICATE:
throw new Exception('{"message":"SSL error: ' . $response['errorMessage'] . '"}');
break;
default:
throw new Exception('{"message":"Error, please contact support@stackmash.com: ' . $response['errorMessage'] . '"}');
}
}
function stackmash_shouldRetry($response, $tries)
{
if($tries >= 3)
{
return false;
}
if($response['errorNumber'] === CURLE_OPERATION_TIMEOUTED)
{
return true;
}
if($response['errorNumber'] === CURLE_COULDNT_CONNECT)
{
return true;
}
if($response['responseCode'] === 409)
{
return true;
}
return false;
}