Skip to content

Commit f56f78f

Browse files
author
Khanh Nguyen
committed
Uses protobuf message instead of home grown pojos
1 parent 214366e commit f56f78f

35 files changed

Lines changed: 352 additions & 762 deletions

src/Core/ApacheError.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/Core/ApiConnector.php

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
use BitSensor\Exception\ApiException;
66
use BitSensor\Util\Log;
7+
use Proto\Datapoint;
78

89

910
/**
1011
* Handles the connection with the BitSensor servers.
1112
* @package BitSensor\Core
1213
*/
13-
class ApiConnector {
14+
class ApiConnector
15+
{
1416

1517
/**
1618
* Authorize the connecting user.
@@ -43,7 +45,7 @@ class ApiConnector {
4345
*/
4446
private $uri;
4547
/**
46-
* @var array JSON array
48+
* @var Datapoint datapoint to send
4749
*/
4850
private $data;
4951
/**
@@ -59,26 +61,29 @@ class ApiConnector {
5961
*/
6062
private $action;
6163

62-
private function __construct() {
64+
private function __construct()
65+
{
6366
}
6467

6568
/**
6669
* @param string $user The ID of the user.
6770
* @param string $apiKey The API key used to authenticate with the BitSensor servers.
6871
* @return ApiConnector
6972
*/
70-
public static function from($user, $apiKey) {
73+
public static function from($user, $apiKey)
74+
{
7175
$connector = new self;
7276
$connector->setUser($user);
7377
$connector->setApiKey($apiKey);
7478
return $connector;
7579
}
7680

7781
/**
78-
* @param string $data The data to send as a JSON encoded object.
82+
* @param Datapoint $data The data to send as a JSON encoded object.
7983
* @return ApiConnector
8084
*/
81-
public function with($data) {
85+
public function with($data)
86+
{
8287
$this->setData($data);
8388
return $this;
8489
}
@@ -87,7 +92,8 @@ public function with($data) {
8792
* @param string $uri The server to send the data to.
8893
* @return ApiConnector
8994
*/
90-
public function to($uri) {
95+
public function to($uri)
96+
{
9197
$this->setUri($uri);
9298
return $this;
9399
}
@@ -98,36 +104,41 @@ public function to($uri) {
98104
* {@link ApiConnector::$ACTION_LOG}
99105
* @return ApiConnector
100106
*/
101-
public function post($action) {
107+
public function post($action)
108+
{
102109
$this->setAction($action);
103110
return $this;
104111
}
105112

106113
/**
107114
* @param string $data The data to send as a JSON encoded object.
108115
*/
109-
public function setData($data) {
116+
public function setData($data)
117+
{
110118
$this->data = $data;
111119
}
112120

113121
/**
114122
* @param string $uri The server to send the data to.
115123
*/
116-
public function setUri($uri) {
124+
public function setUri($uri)
125+
{
117126
$this->uri = $uri;
118127
}
119128

120129
/**
121130
* @param string $user The ID of the user.
122131
*/
123-
public function setUser($user) {
132+
public function setUser($user)
133+
{
124134
$this->user = $user;
125135
}
126136

127137
/**
128138
* @param string $apiKey The API key used to authenticate with the BitSensor servers.
129139
*/
130-
public function setApiKey($apiKey) {
140+
public function setApiKey($apiKey)
141+
{
131142
$this->apiKey = $apiKey;
132143
}
133144

@@ -136,7 +147,8 @@ public function setApiKey($apiKey) {
136147
* {@link ApiConnector::$ACTION_AUTHORIZE},
137148
* {@link ApiConnector::$ACTION_LOG}
138149
*/
139-
public function setAction($action) {
150+
public function setAction($action)
151+
{
140152
$this->action = $action;
141153
}
142154

@@ -145,18 +157,22 @@ public function setAction($action) {
145157
*
146158
* @throws ApiException
147159
*/
148-
public function send() {
149-
150-
$this->data[MetaContext::NAME] = array(
160+
public function send()
161+
{
162+
$meta = array(
151163
MetaContext::USER => $this->user,
152164
MetaContext::API_KEY => $this->apiKey,
153165
MetaContext::PROVIDER => MetaContext::PROVIDER_PHP,
154166
MetaContext::PROVIDER_VERSION => BitSensor::VERSION
155167
);
156168

157-
$json = json_encode($this->data);
169+
foreach ($meta as $k => $v) {
170+
$this->data->getMeta()[$k] = $v;
171+
}
172+
173+
$json = $this->data->serializeToJsonString();
158174

159-
Log::d('<pre>' . json_encode($this->data, defined ("JSON_PRETTY_PRINT") ? JSON_PRETTY_PRINT : 0) . '</pre>');
175+
Log::d('<pre>' . json_encode($this->data, defined("JSON_PRETTY_PRINT") ? JSON_PRETTY_PRINT : 0) . '</pre>');
160176

161177
$fp = fopen(dirname(__DIR__) . '/bitbrain.pem', 'r');
162178
$cert = fread($fp, 8192);
@@ -210,7 +226,8 @@ public function send() {
210226
*
211227
* @see ApiConnector::setAction()
212228
*/
213-
private static function getRequestType($action) {
229+
private static function getRequestType($action)
230+
{
214231
switch ($action) {
215232
case ApiConnector::ACTION_AUTHORIZE:
216233
return 'POST';
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
* Information about the user trying to authenticate.
88
* @package BitSensor\Core
99
*/
10-
class AuthenticationContext extends Context {
11-
10+
class AuthenticationConstants extends Constants
11+
{
1212
/**
1313
* Authentication of the connecting user.
1414
*/
@@ -30,10 +30,4 @@ class AuthenticationContext extends Context {
3030
*/
3131
const REMOTE_USER = 'user';
3232

33-
public function __construct($key, $value) {
34-
$this->setName(self::NAME . '.' . $key);
35-
$this->setValue($value);
36-
}
37-
38-
3933
}

0 commit comments

Comments
 (0)