1: <?php
2: namespace Ctct\Services;
3:
4: use Ctct\Exceptions\CtctException;
5: use Ctct\Util\Config;
6: use GuzzleHttp\Client;
7: use GuzzleHttp\Exception\ClientException;
8:
9: 10: 11: 12: 13: 14:
15: abstract class BaseService
16: {
17: 18: 19: 20: 21:
22: private static function getHeaders($accessToken)
23: {
24: return array(
25: 'User-Agent' => 'ConstantContact AppConnect PHP Library v' . Config::get('settings.version'),
26: 'Content-Type' => 'application/json',
27: 'Accept' => 'application/json',
28: 'Authorization' => 'Bearer ' . $accessToken
29: );
30: }
31:
32: 33: 34: 35:
36: private $client;
37:
38: 39: 40: 41:
42: private $apiKey;
43:
44: 45: 46: 47:
48: public function __construct($apiKey)
49: {
50: $this->apiKey = $apiKey;
51: $this->client = new Client();
52: }
53:
54: 55: 56: 57:
58: protected function getClient()
59: {
60: return $this->client;
61: }
62:
63: protected function createBaseRequest($accessToken, $method, $baseUrl) {
64: $request = $this->client->createRequest($method, $baseUrl);
65: $request->getQuery()->set("api_key", $this->apiKey);
66: $request->setHeaders($this->getHeaders($accessToken));
67: return $request;
68: }
69:
70: 71: 72: 73: 74:
75: protected function convertException($exception)
76: {
77: $ctctException = new CtctException($exception->getResponse()->getReasonPhrase(), $exception->getCode());
78: $ctctException->setUrl($exception->getResponse()->getEffectiveUrl());
79: $ctctException->setErrors(json_decode($exception->getResponse()->getBody()->getContents()));
80: return $ctctException;
81: }
82: }
83: