Skip to content

Commit 77e8905

Browse files
authored
Merge pull request #62 from ddebin/master
Bump guzzlehttp/oauth-subscriber to 0.8.*
2 parents 60be5fe + d3b2209 commit 77e8905

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"ext-json": "*",
2020
"php": ">=7.4",
2121
"guzzlehttp/guzzle": "^7.5.0",
22-
"guzzlehttp/oauth-subscriber": "^0.6.0"
22+
"guzzlehttp/oauth-subscriber": "^0.8.0"
2323
},
2424
"require-dev": {
2525
"phpunit/phpunit": "^9.6.3",

src/AbstractController.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
namespace Noweh\TwitterApi;
33

44
use GuzzleHttp\Client;
5+
use GuzzleHttp\Exception\GuzzleException;
6+
use GuzzleHttp\Exception\RequestException;
57
use GuzzleHttp\Exception\ServerException;
68
use GuzzleHttp\Subscriber\Oauth\Oauth1;
79
use GuzzleHttp\HandlerStack;
@@ -86,10 +88,10 @@ private function getAPIBaseURI(): string
8688
/**
8789
* Perform the request to Twitter API
8890
* @param array<string, mixed> $postData
89-
* @return mixed
90-
* @throws \GuzzleHttp\Exception\GuzzleException|\RuntimeException|\JsonException
91+
* @return \stdClass|null
92+
* @throws GuzzleException|\RuntimeException|\JsonException
9193
*/
92-
public function performRequest(array $postData = [], $withHeaders = false)
94+
public function performRequest(array $postData = [], bool $withHeaders = false): ?\stdClass
9395
{
9496
try {
9597
$headers = [
@@ -128,9 +130,10 @@ public function performRequest(array $postData = [], $withHeaders = false)
128130
'json' => count($postData) ? $postData : null,
129131
]);
130132

133+
/** @var \stdClass|null $body */
131134
$body = json_decode($response->getBody()->getContents(), false, 512, JSON_THROW_ON_ERROR);
132135

133-
if ($withHeaders) {
136+
if ($withHeaders && $body) {
134137
$body->headers = $response->getHeaders();
135138
}
136139

@@ -148,12 +151,11 @@ public function performRequest(array $postData = [], $withHeaders = false)
148151
return $body;
149152

150153
} catch (ServerException $e) {
151-
/** @var \stdClass $payload */
152-
$payload = json_decode(str_replace("\n", "", $e->getResponse()->getBody()->getContents()), false, 512,
153-
JSON_THROW_ON_ERROR);
154-
throw new \RuntimeException($payload->detail, $payload->status);
155-
} catch (\GuzzleHttp\Exception\RequestException $e) {
156-
throw new \RuntimeException($e->getResponse()->getBody()->getContents(), $e->getCode());
154+
/** @var \stdClass|null $payload */
155+
$payload = json_decode($e->getResponse()->getBody()->getContents(), false, 512, JSON_THROW_ON_ERROR);
156+
throw new \RuntimeException($payload->detail ?? $e->getMessage(), $payload->status ?? $e->getCode());
157+
} catch (RequestException $e) {
158+
throw new \RuntimeException($e->getMessage(), $e->getCode());
157159
}
158160
}
159161

@@ -213,7 +215,7 @@ private function parseSettings(array $settings): void
213215
$this->bearer_token = $settings['bearer_token'];
214216
$this->access_token = $settings['access_token'];
215217
$this->access_token_secret = $settings['access_token_secret'];
216-
$this->free_mode = $settings['free_mode'] ?? false;
218+
$this->free_mode = (bool) ($settings['free_mode'] ?? false);
217219
$this->api_base_uri = $settings['api_base_uri'] ?? self::API_BASE_URI;
218220
}
219221

src/Media.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use Exception;
66
use GuzzleHttp\Client;
7+
use GuzzleHttp\Exception\GuzzleException;
8+
use GuzzleHttp\Exception\RequestException;
9+
use GuzzleHttp\Exception\ServerException;
710
use GuzzleHttp\HandlerStack;
811
use GuzzleHttp\Subscriber\Oauth\Oauth1;
912
use JsonException;
@@ -34,7 +37,7 @@ public function __construct(array $settings)
3437

3538
/**
3639
* Prepare request to upload images to Twitter
37-
* @param array $settings
40+
* @param array<string, mixed> $settings
3841
* @return void
3942
*/
4043
private function prepareRequest(array $settings = []): void
@@ -58,9 +61,8 @@ private function prepareRequest(array $settings = []): void
5861
/**
5962
* Upload media to Twitter
6063
* @param string $filedata Base64 encoded binary file
61-
* @return void
62-
* @throws JsonException
63-
* @throws Exception
64+
* @return array<string, mixed>|null
65+
* @throws GuzzleException|\RuntimeException|\JsonException
6466
*/
6567
public function upload(string $filedata = ""): ?array
6668
{
@@ -81,10 +83,16 @@ public function upload(string $filedata = ""): ?array
8183
]);
8284

8385
if ($response->getStatusCode() === 200) {
84-
return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
86+
/** @var array<string, mixed>|null $payload */
87+
$payload = json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
88+
return $payload;
8589
}
86-
} catch (Exception $e) {
87-
throw new \RuntimeException($e->getResponse()->getBody()->getContents(), $e->getCode());
90+
} catch (ServerException $e) {
91+
/** @var \stdClass|null $payload */
92+
$payload = json_decode($e->getResponse()->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
93+
throw new \RuntimeException($payload->detail ?? $e->getMessage(), $payload->status ?? $e->getCode());
94+
} catch (RequestException $e) {
95+
throw new \RuntimeException($e->getMessage(), $e->getCode());
8896
}
8997

9098
return null;

0 commit comments

Comments
 (0)