Skip to content

Commit

Permalink
wip (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidigi authored Mar 1, 2021
1 parent 4e3ca5d commit b3b0caf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
8 changes: 7 additions & 1 deletion src/Clients/AwsLambda/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function request(string $method, $uri = '', array $options = [])

protected function getPayload(string $method, string $url, array $options = []) : array
{
return [
$returnOptions = [
'path' => $url,
'httpMethod' => $method,
'headers' => $options['headers'] ?? [],
Expand All @@ -41,5 +41,11 @@ protected function getPayload(string $method, string $url, array $options = [])
'body' => $options['json'] ?? '',
'isBase64Encoded' => $options['isBase64Encoded'] ?? false,
];

if ($options['multiValueHeaders'] ?? false) {
$returnOptions['multiValueHeaders'] = $options['multiValueHeaders'];
}

return $returnOptions;
}
}
15 changes: 15 additions & 0 deletions src/Clients/AwsLambda/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@

class Response extends ClientResponse
{
protected $decodedBody;

public function json($key = null, $default = null)
{
if (! $this->decodedBody) {
$this->decodedBody = json_decode($this->body(), true);
}

if (is_null($key)) {
return $this->decodedBody;
}

return data_get($this->decodedBody, $key, $default);
}

public function getPayload() : array
{
if (! $this->decoded) {
Expand Down
8 changes: 6 additions & 2 deletions src/Mixins/JsonApiRequestMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Sidigi\LaravelRemoteModels\Pagination\PaginationStrategyInterface;
use Sidigi\LaravelRemoteModels\Response;
use Sidigi\LaravelRemoteModels\Services\JsonApiRequest\FilterService;
use Sidigi\LaravelRemoteModels\Services\JsonApiRequest\IncludeService;
use Sidigi\LaravelRemoteModels\Services\JsonApiRequest\PaginateService;
Expand Down Expand Up @@ -107,12 +108,15 @@ public function getPaginationStrategy()
public function perPage()
{
return function ($method = 'get', $sleep = null, ...$arguments) {
/**
* @var PaginationStrategyInterface
*/
$strategy = $this->getPaginationStrategy();

do {
$response = $this->$method(...$arguments);
$response = $this->$method($this->options['path'] ?? '', $arguments);

yield $response;
yield new Response($response, config('laravel-remote-models.defaults.response_key', 'data'));

$strategy->prepareForNextRequest();

Expand Down
16 changes: 1 addition & 15 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,11 @@ public function __construct(HttpClientResponse $response, string $responseKey)
$this->responseKey = $responseKey;
}

public function get(string $index = '')
{
if (! $index) {
return $this->json();
}

return data_get($this->json(), $index, []);
}

public function errors(string $key = 'errors')
{
return $this->get($key);
}

public function mapModel(string $model, Closure $callback = null, string $responseKey = null)
{
$responseKey = ! is_null($responseKey) ? $responseKey : $this->responseKey;

$items = $this->get($responseKey) ?? [];
$items = $this->json($responseKey) ?? [];

return (new DataModelConverter($model))->convert($items, $callback);
}
Expand Down

0 comments on commit b3b0caf

Please sign in to comment.