Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.json
  • Loading branch information
soltancode committed Nov 24, 2022
2 parents ab11ebc + faec402 commit 315f7c0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ sendRequest()->delete($baseUrl, $service, [
]);
```

### Timeout
The timeout method may be used to specify the maximum number of seconds to wait for a response:
```php
$response = sendRequest()->timeout(3)->post(/* ... */);
```

## Other Packages
[ReturnResponse](https://github.com/soltancode/ReturnResponse) - It's a return helper for showing standard json responses.

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "soltancode/send-request",
"type": "library",
"description": "Simple package for Laravel that makes it easy to send HTTP requests and integrate with web APIs.",
"version": "v1.0.0",
"version": "v2.0.0",
"keywords": [
"soltancode",
"curl",
Expand All @@ -25,7 +25,7 @@
}
],
"require": {
"php": "^7.3|^8.0",
"php": "^8.0",
"illuminate/support": "^9.0",
"illuminate/http": "^9.0"
},
Expand Down
25 changes: 19 additions & 6 deletions src/ConnectApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

class ConnectApi implements SendInterface
{
/**
* @var int
*/
private int $timeoutSeconds = 30;

/**
* @param string $service Required | For example: https://soltancode.com
* @param string $request Required | For example: /user/validation
Expand All @@ -19,16 +24,24 @@ class ConnectApi implements SendInterface
public function send(string $service, string $request, string $method, array $params = [], array $headers = []): mixed
{
try {
$url = $service.$request;
$response = Http::withHeaders($headers)->acceptJson()->$method($url, $params);
$data = $response->object();

return $data;
} catch(\Exception $e) {
$url = $service . $request;
$response = Http::withHeaders($headers)->timeout($this->timeoutSeconds)->acceptJson()->$method($url, $params);
return $response->object();
} catch (\Exception $e) {
return Response::json([$e->getMessage()], 500);
}
}

/**
* @param int $seconds
* @return mixed
*/
public function timeout(int $seconds): mixed
{
$this->timeoutSeconds = $seconds;
return $this;
}

/**
* @param string $service Required | For example: http://soltancode.com
* @param string $request Required | For example: /user/validation
Expand Down
11 changes: 9 additions & 2 deletions src/Interfaces/SendInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Soltancode\SendRequest\Interfaces;

use Illuminate\Http\JsonResponse;

/**
* @property int $timeoutSeconds;
*/
interface SendInterface
{
/**
Expand All @@ -16,6 +17,12 @@ interface SendInterface
*/
public function send(string $service, string $request, string $method, array $params = [], array $headers = []): mixed;

/**
* @param int $seconds
* @return mixed
*/
public function timeout(int $seconds): mixed;

/**
* @param string $service Required | For example: http://soltancode.com
* @param string $request Required | For example: /user/validation
Expand Down

0 comments on commit 315f7c0

Please sign in to comment.