-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.php
More file actions
executable file
·170 lines (141 loc) · 3.45 KB
/
Client.php
File metadata and controls
executable file
·170 lines (141 loc) · 3.45 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace SixBySix\CodebaseHq;
use GuzzleHttp\Client as GuzzleClient;
/**
* Class Client
* @package SixBySix\CodebaseHq
*/
class Client
{
const DOMAIN = 'https://api3.codebasehq.com';
/** @var GuzzleClient */
protected static $http;
/** @var string */
protected static $accountName;
/** @var string */
protected static $apiKey;
/** @var string */
protected static $username;
/**
* @return mixed
*/
public static function getAccountName()
{
return self::$accountName;
}
/**
* @param mixed $accountName
*/
public static function setAccountName($accountName)
{
self::$accountName = $accountName;
}
/**
* @return mixed
*/
public static function getApiKey()
{
return self::$apiKey;
}
/**
* @param mixed $apiKey
*/
public static function setApiKey($apiKey)
{
self::$apiKey = $apiKey;
}
/**
* @return mixed
*/
public static function getUsername()
{
return self::$username;
}
/**
* @param mixed $username
*/
public static function setUsername($username)
{
self::$username = $username;
}
public static function init($accountName, $username, $apiKey)
{
static::setAccountName($accountName);
static::setUsername($username);
static::setApiKey($apiKey);
}
/**
* GET method
*
* @param $resource
* @param array $opts
* @return \SimpleXMLElement
*/
public static function get($resource, array $opts = [])
{
$opts = ['query' => $opts];
$opts = static::prepareRequestOpts($opts);
/** @var Response $response */
$response = self::http()->get($resource, $opts);
/** @var string $body */
$body = (string) $response->getBody();
/** @var \SimpleXMLElement $xml */
$xml = simplexml_load_string($body);
return $xml;
}
/**
* GET method
*
* @param $resource
* @param array $opts
* @return \SimpleXMLElement
*/
public static function post($resource, $xml, array $opts = [])
{
$opts = [
'query' => $opts,
'body' => (string) $xml,
];
$opts = static::prepareRequestOpts($opts);
/** @var Response $response */
$response = self::http()->post($resource, $opts);
/** @var string $body */
$body = (string) $response->getBody();
/** @var \SimpleXMLElement $xml */
$xml = simplexml_load_string($body);
return $xml;
}
/**
* Set auth + headers for each request
*
* @param array $opts
* @return array
*/
protected static function prepareRequestOpts(array $opts = [])
{
$opts['auth'] = [
static::getAccountName() . "/" . static::getUsername(),
static::getApiKey(),
];
$opts['headers'] = [
'User-Agent' => 'CodebaseHq API for PHP (daniel@sixbysix.co.uk)',
'Content-Type' => 'application/xml',
'Accept' => 'application/xml',
];
return $opts;
}
/**
* Getter for http client
*
* @return GuzzleClient
*/
protected static function http()
{
if (!static::$http) {
static::$http = new GuzzleClient([
'base_uri' => static::DOMAIN,
]);
}
return static::$http;
}
}