This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.php
102 lines (78 loc) · 2.3 KB
/
Client.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
<?php
namespace Botamp;
use Botamp\Api\ApiResource;
use Botamp\Api\ApiResponse;
use Botamp\Exceptions;
use Http\Client\Common;
use Http\Client\Common\Plugin;
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\MessageFactory;
use Http\Message\Authentication\BasicAuth;
/**
* Class Client
*
* @package Botamp
*/
class Client
{
private $httpClient;
private $apiKey;
private $apiBase = 'https://app.botamp.com/api';
private $apiVersion = 'v1';
private static $allApiVersions = ['v1'];
public $entities;
public $me;
public $subscriptions;
public function __construct($apiKey, HttpClient $httpClient = null)
{
$this->apiKey = $apiKey;
$this->setHttpClient($httpClient ?: HttpClientDiscovery::find(), MessageFactoryDiscovery::find());
$this->bindResources();
}
public function getHttpClient()
{
return $this->httpClient;
}
public function getApiKey()
{
return $this->apiKey;
}
public function setApiBase($apiBase)
{
$this->apiBase = $apiBase;
$this->bindResources();
}
public function getApiBase()
{
return $this->apiBase;
}
public function setApiVersion($apiVersion)
{
$apiVersion = strtolower($apiVersion);
if(!in_array($apiVersion, self::$allApiVersions))
throw new Exceptions\Base("No valid api version provided.");
else
$this->apiVersion = $apiVersion;
$this->bindResources();
}
public function getApiVersion()
{
return $this->apiVersion;
}
private function setHttpClient(HttpClient $httpClient, MessageFactory $messageFactory)
{
$plugins = [
new Plugin\HeaderDefaultsPlugin(['Content-Type' => 'application/vnd.api+json']),
new Plugin\AuthenticationPlugin(new BasicAuth($this->apiKey, ''))
];
$this->httpClient = new Common\HttpMethodsClient(new Common\PluginClient($httpClient, $plugins), $messageFactory);
}
private function bindResources()
{
$this->entities = new ApiResource('entities', $this);
$this->me = new ApiResource('me', $this);
$this->subscriptions = new ApiResource('subscriptions', $this);
}
}