-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathClient.php
More file actions
103 lines (88 loc) · 3.26 KB
/
Client.php
File metadata and controls
103 lines (88 loc) · 3.26 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
<?php
namespace SegmentIO;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Collection;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Command\Guzzle\Description;
use SegmentIO\Subscriber\BatchFileSubscriber;
use SegmentIO\Subscriber\BatchRequestSubscriber;
/**
* Web Service Client for Segment.io
*
* @author Keith Kirk <kkirk@undergroundelephant.com>
*
* @method array identify(array $data = [])
* @method array alias(array $data = [])
* @method array group(array $data = [])
* @method array track(array $data = [])
* @method array page(array $data = [])
* @method array screen(array $data = [])
* @method array import(array $data = [])
*/
class Client extends GuzzleClient
{
/**
* PHP Client Version
*/
const VERSION = '1.1.0';
/**
* Constructor
*
* @param array $config
*/
public function __construct(array $config = [])
{
$defaults = [
'write_key' => null,
'version' => 'v1',
'batching' => 'request',
'log_file' => null,
'max_queue_size' => 10000,
'batch_size' => 100
];
// Create Configuration
$config = Collection::fromConfig($config, $defaults, ['write_key', 'version', 'batching']);
// Load versioned Service Description
$description = $this->loadServiceDescription(
__DIR__ . '/Description/segment.io.%s.php', $config->get('version')
);
// Allow the Adapter to be set
$httpConfig = $config->hasKey('adapter') ? ['adapter' => $config->get('adapter')] : [];
// Create the Client
parent::__construct(new HttpClient($httpConfig), $description, $config->toArray());
// Set Basic Auth
$this->getHttpClient()->setDefaultOption('auth', [$config->get('write_key'), null]);
// Set the content type header to use "application/json" for all requests
$this->getHttpClient()->setDefaultOption('headers', array('Content-Type' => 'application/json'));
// Default the Version
$this->setConfig('defaults/version', $this->getDescription()->getApiVersion());
if ($config->get('batching') == 'request') {
$this->getEmitter()->attach(new BatchRequestSubscriber($this->getDescription(), [
'max_queue_size' => $config->get('max_queue_size'),
'batch_size' => $config->get('batch_size')
]));
}
if ($config->get('batching') == 'file') {
$this->getEmitter()->attach(new BatchFileSubscriber($this->getDescription(), [
'filename' => $config->get('log_file')
]));
}
}
/**
* Loads the Service Description from the given file path
*
* @param string $filepath The Service Description filepath
* @param string $version The API Version
*
* @return Description
*/
public function loadServiceDescription($filepath, $version)
{
$filepath = sprintf($filepath, $version);
$description = file_exists($filepath) ? include $filepath : null;
if (!is_array($description)) {
throw new \InvalidArgumentException('Invalid Service Description!');
}
return new Description($description);
}
}