Skip to content

Commit

Permalink
fix: media upload: #332 (#336)
Browse files Browse the repository at this point in the history
* fix: media upload: #332
* fix: disable code comments for codecov
* return null for no content

Co-authored-by: atymic <atymicq@gmail.com>
  • Loading branch information
reliq and atymic authored Mar 24, 2021
1 parent f356c13 commit cec305b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 23 deletions.
2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github_checks:
annotations: false
43 changes: 36 additions & 7 deletions src/Traits/MediaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,47 @@ trait MediaTrait
* - media
* - media_data
*
* @param mixed $parameters
* @return mixed
* @throws BadMethodCallException
*/
public function uploadMedia($parameters = [])
public function uploadMedia(array $parameters = [])
{
if (!array_key_exists('media', $parameters) && !array_key_exists('media_data', $parameters)) {
throw new BadMethodCallException('Parameter required missing : media or media_data');
$commandKey = 'command';
$mediaKey = 'media';
$mediaDataKey = 'media_data';

if (isset($parameters[$mediaKey], $parameters[$mediaDataKey])) {
throw new BadMethodCallException('You cannot use `media` and `media_data` at the same time.');
}

if (!(isset($parameters[$mediaKey]) || isset($parameters[$mediaDataKey]) || isset($parameters[$commandKey]))) {
throw new BadMethodCallException('Required parameter: `media`, `media_data` or `command`');
}

if (array_key_exists('media', $parameters) && array_key_exists('media_data', $parameters)) {
throw new BadMethodCallException('You cannot use media and media_data at the same time');
return $this->post('media/upload', $this->normalizeParameters($parameters), true);
}

private function normalizeParameters(array $parameters): array
{
$normalizedParams = [];
$nameKey = 'name';
$contentsKey = 'contents';

foreach ($parameters as $key => $value) {
if (is_array($value) && isset($value[$nameKey], $value[$contentsKey])) {
$normalizedParams[] = $value;

continue;
}

if (!is_array($value)) {
$normalizedParams[] = [
$nameKey => $key,
$contentsKey => $value,
];
}
}

return $this->post('media/upload', $parameters, true);
return $normalizedParams;
}
}
38 changes: 22 additions & 16 deletions src/Twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use GuzzleHttp\RequestOptions;
use GuzzleHttp\Subscriber\Oauth\Oauth1;
use InvalidArgumentException;
use JsonException;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\InvalidArgumentException as InvalidLogArgumentException;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -127,7 +128,7 @@ public function usingConfiguration(Configuration $configuration): self
}

/**
* @return mixed|string
* @return mixed
* @throws TwitterRequestException
*/
public function query(
Expand Down Expand Up @@ -172,8 +173,8 @@ public function directQuery(
}

/**
* @param array $parameters
* @param bool $multipart
* @param array $parameters
* @param bool $multipart
* @param string $extension
*
* @return mixed|string
Expand All @@ -185,7 +186,7 @@ public function get(string $endpoint, $parameters = [], $multipart = false, $ext
}

/**
* @return mixed|string
* @return mixed
* @throws TwitterRequestException
*/
public function post(string $endpoint, array $parameters = [], bool $multipart = false)
Expand Down Expand Up @@ -269,25 +270,30 @@ private function transformClientException(GuzzleException $exception): TwitterRe
/**
* @param Response|ResponseInterface $response
*
* @return mixed|string
* @return mixed
*/
private function formatResponse(Response $response, string $format)
{
$body = (string) $response->getBody();

switch ($format) {
case self::RESPONSE_FORMAT_JSON:
return $body;
case self::RESPONSE_FORMAT_ARRAY:
return json_decode($body, true, 512, JSON_THROW_ON_ERROR);
case self::RESPONSE_FORMAT_OBJECT:
default:
return json_decode($body, false, 512, JSON_THROW_ON_ERROR);
try {
$body = (string) $response->getBody();

switch ($format) {
case self::RESPONSE_FORMAT_JSON:
return $body;
case self::RESPONSE_FORMAT_ARRAY:
return json_decode($body, true, 512, JSON_THROW_ON_ERROR);
case self::RESPONSE_FORMAT_OBJECT:
default:
return json_decode($body, false, 512, JSON_THROW_ON_ERROR);
}
} catch (JsonException $exception) {
return null;
}
}

/**
* @return mixed|string
* @return mixed
* @throws GuzzleException
*/
private function request(string $url, array $parameters, string $method)
{
Expand Down

0 comments on commit cec305b

Please sign in to comment.