Skip to content

Commit

Permalink
feat: custom route possibility
Browse files Browse the repository at this point in the history
Add the `custom` method on the Tourware Client
to allow fetching the object relation from tourware.
  • Loading branch information
nicoorfi committed Mar 28, 2022
1 parent 7717a06 commit a225541
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ $travels = $client->travel()->query()->offset(5)->limit(20)->get();

```

#### Custom endpoint
In a lot of cases, you may want to send **custom** HTTP request to Tourware. You can do this by using the `custom` method on the `Client` class.
```php
$relations = $client->custom("/relations/getRelations/travels/bba0b42e4699", 'GET')->call();
```

## Change log

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],
"require": {
"php": "^7.3|^8.0",
"adbario/php-dot-notation": "^2.2",
"adbario/php-dot-notation": "^3",
"guzzlehttp/guzzle": "^7.3"
},
"require-dev": {
Expand Down
6 changes: 6 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Tourware\Contracts\Entity;
use Tourware\Contracts\ReadClient;
use Tourware\Contracts\WriteClient;
use Tourware\Entities\Custom;
use Tourware\Entities\Raw;
use Tourware\Shared\Clients;

Expand Down Expand Up @@ -40,6 +41,11 @@ public static function create(string $xApiKey, bool $staging = true)
return new static(new Http($config));
}

public function custom(string $endpoint, string $method, ?array $body = null): Custom
{
return new Custom($this->http, $endpoint, $method, $body);
}

public function raw(string $endpoint): WriteClient
{
return (new Raw($endpoint))->client($this->http);
Expand Down
43 changes: 43 additions & 0 deletions src/Entities/Custom.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Tourware\Entities;

use Adbar\Dot;
use GuzzleHttp\Client as Http;
use Tourware\Clients\WriteClient as WriteClient;
use Tourware\Contracts\WriteClient as WriteClientInterface;
use Tourware\Requests\ApiRequest;
use Tourware\Shared\SendRequest;
use GuzzleHttp\Psr7\Request;

class Custom
{
use SendRequest;

protected string $endpoint;

protected string $method;

protected ?array $body;

public function __construct(Http $http, string $endpoint, string $method, ?array $body = null)
{
$this->http = $http;
$this->body = $body;
$this->endpoint = $endpoint;
$this->method = $method;
}

public function call(): Dot
{
$request = new Request(
$this->method,
$this->endpoint,
$this->body
);

return $this->sendRequest($request);
}
}

0 comments on commit a225541

Please sign in to comment.