-
Notifications
You must be signed in to change notification settings - Fork 10
/
ClassyAPIClient.php
104 lines (88 loc) · 2.94 KB
/
ClassyAPIClient.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
<?php
class ClassyAPIClient
{
const CLASSY_API_BASEURL = 'https://api.classy.org';
const CLASSY_API_VERSION = '2.0';
private static $instance;
private $clientId;
private $clientSecret;
/**
* Private constructor, use factory.
*
* @param $clientId API client ID
* @param $clientSecret API client secret
*/
private function __construct($clientId, $clientSecret)
{
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
}
/**
* Factory method for API singleton.
*
* @param $clientId API client ID
* @param $clientSecret API client secret
* @return ClassyAPIClient
*/
public static function getInstance($clientId, $clientSecret)
{
if ((self::$instance instanceof ClassyAPIClient) === false)
{
self::$instance = new ClassyAPIClient($clientId, $clientSecret);
}
return self::$instance;
}
/**
* Create an access token using key and secret.
*
* @return mixed
*/
public function getAccessToken()
{
$cacheKey = ClassyOrg::CACHE_KEY_PREFIX . '_ACCESS_TOKEN_' . $this->clientId;
$token = get_transient($cacheKey);
if ($token === false)
{
$options = array(
'http' => array(
'header' => 'Content-Type: text/plain',
'method' => 'POST'
)
);
$context = stream_context_create($options);
$content = "client_id={$this->clientId}&client_secret={$this->clientSecret}&grant_type=client_credentials";
$result = file_get_contents('https://api.classy.org/oauth2/auth?' . $content, false, $context);
$token = json_decode($result, true);
set_transient($cacheKey, $token['access_token'], ($token['expires_in'] - 60));
$token = $token['access_token'];
}
return $token;
}
/**
* Make an API request to Classy API.
*
* @param string $url Endpoint URL (e.g. /campaigns/999999)
* @param string $method HTTP Method (e.g. 'GET', 'POST', 'PUT')
* @param array $params URL Parameters to be sent as '?key1=value1&key2=value2'
* @return string Content response
*/
public function request($url, $method = 'GET', $params = array()) // FIXME: more params
{
$url = self::CLASSY_API_BASEURL
. '/' . self::CLASSY_API_VERSION
. '/' . $url
. '?' . http_build_query($params);
$token = $this->getAccessToken();
$options = array(
'http' => array(
'method' => $method,
'header' => "Content-Type: text/plain\r\n"
. "Authorization: Bearer $token\r\n"
)
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
// FIXME: Error handle
return $response;
}
}