Skip to content

Commit 0db5037

Browse files
committed
Simplify Client. Optimize error handlers. Update Readme files.
Add code improvements tools.
1 parent 5053045 commit 0db5037

File tree

7 files changed

+112
-47
lines changed

7 files changed

+112
-47
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ $share = $client->post(
221221
Setup custom API request headers
222222

223223
```php
224-
$client->setDefaultApiHeaders([
224+
$client->setApiHeaders([
225225
'Content-Type' => 'application/json',
226226
'x-li-format' => 'json',
227227
'x-li-src' => 'msdk' // set a src header to "msdk" to mimic a mobile SDK

README.ru.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,45 @@ $share = $client->post(
168168
);
169169
```
170170

171+
Поделиться контентом на тестовой странице компаний
172+
173+
```php
174+
// Вы можете увидеть сообщение на этой странице
175+
// https://www.linkedin.com/company/devtestco
176+
$companyId = '2414183'; // идентификатор страницы
177+
178+
$share = $client->post(
179+
'companies/' . $companyId . '/shares',
180+
[
181+
'comment' => 'Checkout this amazing PHP SDK for LinkedIn!',
182+
'content' => [
183+
'title' => 'PHP Client for LinkedIn API',
184+
'description' => 'OAuth 2 flow, composer Package',
185+
'submitted-url' => 'https://github.com/zoonman/linkedin-api-php-client',
186+
'submitted-image-url' => 'https://github.com/fluidicon.png',
187+
],
188+
'visibility' => [
189+
'code' => 'anyone'
190+
]
191+
]
192+
);
193+
```
194+
195+
Установить заголовки по умолчанию
196+
197+
```php
198+
$client->setApiHeaders([
199+
'Content-Type' => 'application/json',
200+
'x-li-format' => 'json',
201+
'x-li-src' => 'msdk' // например отправить "msdk" чтобы симулировать мобильное SDK
202+
]);
203+
```
204+
205+
Изменить корневой адрес для API вызовов
206+
207+
```php
208+
$client->setApiRoot('https://api.linkedin.com/v2/');
209+
```
171210

172211
## Помощь проекту
173212

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
},
4747
"require-dev": {
4848
"vlucas/phpdotenv": "~2.0",
49-
"phpunit/phpunit": "~4.0"
49+
"phpunit/phpunit": "~4.0",
50+
"phpmd/phpmd": "@stable",
51+
"squizlabs/php_codesniffer": "@stable"
5052
},
5153
"archive": {
5254
"exclude": [

examples/index.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
use LinkedIn\Scope;
2323

2424
// import environment variables from the environment file
25+
// you need a .env file in the parent folder
26+
// read this document to learn how to create that file
27+
// https://github.com/zoonman/linkedin-api-php-client/blob/master/examples/README.md
28+
//
2529
$dotenv = new Dotenv\Dotenv(dirname(__DIR__));
2630
$dotenv->load();
2731

@@ -31,6 +35,7 @@
3135
session_start();
3236

3337
// instantiate the Linkedin client
38+
// you can setup keys using
3439
$client = new Client(
3540
getenv('LINKEDIN_CLIENT_ID'),
3641
getenv('LINKEDIN_CLIENT_SECRET')

src/AccessToken.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@ public function setExpiresAt($expiresAt)
135135
return $this;
136136
}
137137

138+
/**
139+
* Convert API response into AccessToken
140+
*
141+
* @param \Psr\Http\Message\ResponseInterface $response
142+
*
143+
* @return self
144+
*/
145+
public static function fromResponse($response)
146+
{
147+
return static::fromResponseArray(
148+
Client::responseToArray($response)
149+
);
150+
}
151+
138152
/**
139153
* Instantiate access token object
140154
*

src/Client.php

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function setUseTokenParam($useTokenParam)
128128
*
129129
* @var array
130130
*/
131-
protected $defaultApiHeaders = [
131+
protected $apiHeaders = [
132132
'Content-Type' => 'application/json',
133133
'x-li-format' => 'json',
134134
];
@@ -138,21 +138,21 @@ public function setUseTokenParam($useTokenParam)
138138
*
139139
* @return array
140140
*/
141-
public function getDefaultApiHeaders()
141+
public function getApiHeaders()
142142
{
143-
return $this->defaultApiHeaders;
143+
return $this->apiHeaders;
144144
}
145145

146146
/**
147147
* Set list of default headers
148148
*
149-
* @param array $defaultApiHeaders
149+
* @param array $apiHeaders
150150
*
151151
* @return Client
152152
*/
153-
public function setDefaultApiHeaders($defaultApiHeaders)
153+
public function setApiHeaders($apiHeaders)
154154
{
155-
$this->defaultApiHeaders = $defaultApiHeaders;
155+
$this->apiHeaders = $apiHeaders;
156156
return $this;
157157
}
158158

@@ -290,20 +290,11 @@ public function getAccessToken($code = '')
290290
]);
291291
try {
292292
$response = $guzzle->get($uri);
293-
} catch (RequestException $requestException) {
294-
$json = self::responseToArray(
295-
$requestException->getResponse()
296-
);
297-
throw new Exception(
298-
$requestException->getMessage(),
299-
$requestException->getCode(),
300-
$requestException,
301-
static::extractErrorDescription($json)
302-
);
293+
} catch (RequestException $exception) {
294+
throw Exception::fromRequestException($exception);
303295
}
304-
$json = self::responseToArray($response);
305296
$this->setAccessToken(
306-
AccessToken::fromResponseArray($json)
297+
AccessToken::fromResponse($response)
307298
);
308299
}
309300
return $this->accessToken;
@@ -316,7 +307,7 @@ public function getAccessToken($code = '')
316307
*
317308
* @return array
318309
*/
319-
protected static function responseToArray($response)
310+
public static function responseToArray($response)
320311
{
321312
return \GuzzleHttp\json_decode(
322313
$response->getBody()->getContents(),
@@ -491,7 +482,7 @@ protected function buildUrl($endpoint, $params)
491482
*/
492483
public function api($endpoint, array $params = [], $method = Method::GET)
493484
{
494-
$headers = $this->getDefaultApiHeaders();
485+
$headers = $this->getApiHeaders();
495486
$uri = $endpoint;
496487
$options = [];
497488
if ($this->isUsingTokenParam()) {
@@ -524,34 +515,12 @@ public function api($endpoint, array $params = [], $method = Method::GET)
524515
try {
525516
$response = $guzzle->request($method, $uri, $options);
526517
} catch (RequestException $requestException) {
527-
$json = self::responseToArray(
528-
$requestException->getResponse()
529-
);
530-
throw new Exception(
531-
$requestException->getMessage(),
532-
$requestException->getCode(),
533-
$requestException,
534-
static::extractErrorDescription($json)
535-
);
518+
throw Exception::fromRequestException($requestException);
536519
}
537520
return self::responseToArray($response);
538521
}
539522

540-
/**
541-
* @param $json
542-
*
543-
* @return null|string
544-
*/
545-
private static function extractErrorDescription($json)
546-
{
547-
if (isset($json['error_description'])) {
548-
return $json['error_description'];
549-
} elseif (isset($json['message'])) {
550-
return $json['message'];
551-
} else {
552-
return null;
553-
}
554-
}
523+
555524

556525
/**
557526
* Make API call to LinkedIn using GET method

src/Exception.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
namespace LinkedIn;
1818

19+
use GuzzleHttp\Exception\RequestException;
20+
1921
/**
2022
* Class Exception
2123
* @package LinkedIn
@@ -40,7 +42,7 @@ public function __construct(
4042
$message = "",
4143
$code = 0,
4244
$previousException = null,
43-
$description
45+
$description = ''
4446
) {
4547
parent::__construct($message, $code, $previousException);
4648
$this->description = $description;
@@ -55,4 +57,38 @@ public function getDescription()
5557
{
5658
return $this->description;
5759
}
60+
61+
/**
62+
* @param RequestException $exception
63+
*
64+
* @return self
65+
*/
66+
public static function fromRequestException($exception)
67+
{
68+
return new static(
69+
$exception->getMessage(),
70+
$exception->getCode(),
71+
$exception,
72+
static::extractErrorDescription($exception)
73+
);
74+
}
75+
76+
/**
77+
* @param RequestException $exception
78+
*
79+
* @return null|string
80+
*/
81+
private static function extractErrorDescription($exception)
82+
{
83+
$json = Client::responseToArray(
84+
$exception->getResponse()
85+
);
86+
if (isset($json['error_description'])) {
87+
return $json['error_description'];
88+
} elseif (isset($json['message'])) {
89+
return $json['message'];
90+
} else {
91+
return null;
92+
}
93+
}
5894
}

0 commit comments

Comments
 (0)