Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] handle timeout #26

Merged
merged 10 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 40 additions & 52 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,51 @@ on:
branches: [main]

jobs:
test:
tests:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.2, 8.1, 8.0]
laravel: ['7.*', '8.*', '9.*', '10.*', '11.*']
stability: [prefer-stable]
os: [ubuntu-latest]
php: [8.3, 8.2, 8.1]
laravel: [10.*, 11.*]
dependency-version: [prefer-lowest, prefer-stable]
include:
- laravel: 10.*
testbench: 8.*
- laravel: 9.*
testbench: 7.*
- laravel: 8.*
testbench: 6.*
- laravel: 7.*
testbench: 5.*
- laravel: 11.*
testbench: 9.*
- laravel: 10.*
testbench: 8.*
- laravel: 11.*
testbench: 9.*
exclude:
- laravel: 10.*
php: 8.0
- laravel: 7.*
php: 8.2
- laravel: 7.*
php: 8.1
- laravel: 11.*
php: 8.1
- laravel: 11.*
php: 8.0
- laravel: 11.*
php: 8.1

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
coverage: none

- name: Setup problem matchers
run: |
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "nesbot/carbon:${{ matrix.carbon }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction

- name: List Installed Dependencies
run: composer show -D

- name: Execute tests
run: vendor/bin/pest --ci
- name: Checkout code
uses: actions/checkout@v4

- name: Install SQLite 3
run: |
sudo apt-get update
sudo apt-get install sqlite3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: curl, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, iconv
coverage: none

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "nesbot/carbon:>=2.62.1" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction

- name: Setup Problem Matches
run: |
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Execute tests
run: vendor/bin/pest
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"require-dev": {
"laravel/pint": "^1.0",
"nunomaduro/collision": "^7.9",
"nunomaduro/collision": "^7.9 | ^8.0",
"nunomaduro/larastan": "^2.0.1",
"orchestra/testbench": "^5.0|^6.0|^7.0|^8.0|^9.0",
"pestphp/pest": "^1.22|^2.34",
Expand Down
10 changes: 10 additions & 0 deletions config/chapaar.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@
*/
'status' => false,

/*
* Timeout if a server does not return a response in seconds.
*/
'timeout' => env('CHAPAAR_TIMEOUT', 30),

/*
* Connect timeout in seconds.
*/
'connect_timeout' => env('CHAPAAR_CONNECT_TIMEOUT', 0),

/*
* Define configurations for different drivers
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Chapaar.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function outbox(int $page_size = 100, int $page_number = 1): object
return $this->driver()->outbox($page_size, $page_number);
}

protected function driver(Drivers $driver = null): DriverConnector
protected function driver(?Drivers $driver = null): DriverConnector
{
$connector = $driver
? Drivers::tryFrom($driver->value)->connector()
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/DriverConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function verify($message);

public function outbox($page_size = 100, $page_number = 1): object;

public function generateResponse(int $status, string $message, string $driver, array $data = null): object;
public function generateResponse(int $status, string $message, string $driver, ?array $data = null): object;

public function generateAccountResponse($credit, $expire_date): object;

Expand Down
7 changes: 5 additions & 2 deletions src/Drivers/Farapayamak/FarapayamakConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;
use TookanTech\Chapaar\Contracts\DriverConnector;
use TookanTech\Chapaar\Exceptions\ApiException;
use TookanTech\Chapaar\Exceptions\HttpException;
Expand All @@ -21,11 +22,13 @@ public function __construct()
{
self::$setting = (object) config('chapaar.drivers.farapayamak');
$this->client = new Client([
'headers' => [
RequestOptions::HEADERS => [
'Content-Type' => 'application/x-www-form-urlencoded',
'charset' => 'utf-8',
],
'http_errors' => false,
RequestOptions::HTTP_ERRORS => false,
RequestOptions::TIMEOUT => config('chapaar.timeout'),
RequestOptions::CONNECT_TIMEOUT => config('chapaar.connect_timeout'),
]);

}
Expand Down
5 changes: 4 additions & 1 deletion src/Drivers/Ghasedak/GhasedakConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;
use Illuminate\Http\Response;
use TookanTech\Chapaar\Contracts\DriverConnector;
use TookanTech\Chapaar\Exceptions\ApiException;
Expand All @@ -22,11 +23,13 @@ public function __construct()
{
self::$setting = (object) config('chapaar.drivers.ghasedak');
$this->client = new Client([
'headers' => [
RequestOptions::HEADERS => [
'apikey' => self::$setting->api_key,
'cache-control' => 'no-cache',
'content-type' => 'application/x-www-form-urlencoded',
],
RequestOptions::TIMEOUT => config('chapaar.timeout'),
RequestOptions::CONNECT_TIMEOUT => config('chapaar.connect_timeout'),
]);

}
Expand Down
9 changes: 6 additions & 3 deletions src/Drivers/Kavenegar/KavenegarConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;
use Symfony\Component\HttpFoundation\Response;
use TookanTech\Chapaar\Contracts\DriverConnector;
use TookanTech\Chapaar\Exceptions\ApiException;
Expand All @@ -19,13 +20,15 @@ class KavenegarConnector implements DriverConnector
public function __construct()
{
$this->client = (new Client([
'headers' => [
RequestOptions::HEADERS => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
'charset' => 'utf-8',
],
'verify' => false,
'http_errors' => false,
RequestOptions::VERIFY => false,
RequestOptions::HTTP_ERRORS => false,
RequestOptions::TIMEOUT => config('chapaar.timeout'),
RequestOptions::CONNECT_TIMEOUT => config('chapaar.connect_timeout'),
]));
self::$setting = (object) config('chapaar.drivers.kavenegar');
}
Expand Down
5 changes: 4 additions & 1 deletion src/Drivers/SmsIr/SmsIrConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;
use TookanTech\Chapaar\Contracts\DriverConnector;
use TookanTech\Chapaar\Exceptions\ApiException;
use TookanTech\Chapaar\Exceptions\HttpException;
Expand All @@ -19,11 +20,13 @@ public function __construct()
{
self::$setting = (object) config('chapaar.drivers.smsir');
$this->client = new Client([
'headers' => [
RequestOptions::HEADERS => [
'x-api-key' => self::$setting->api_key,
'Accept' => 'text/plain',
'Content-Type' => 'application/json',
],
RequestOptions::TIMEOUT => config('chapaar.timeout'),
RequestOptions::CONNECT_TIMEOUT => config('chapaar.connect_timeout'),
]);

}
Expand Down
2 changes: 1 addition & 1 deletion src/Listeners/StoreSmsMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class StoreSmsMessage
{
public function handle(SmsSent $event)
public function handle(SmsSent $event): void
{

$response = $event->response;
Expand Down
5 changes: 5 additions & 0 deletions src/Models/SmsMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
use Illuminate\Database\Eloquent\Model;
use TookanTech\Chapaar\Enums\Drivers;

/**
* @property Drivers $driver
* @property AsCollection $data
* @property-write int $status
*/
class SmsMessage extends Model
{
protected $fillable = [
Expand Down
2 changes: 1 addition & 1 deletion src/SmsChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function send($notifiable, $notification)

}

protected function driver(Drivers $driver = null): DriverConnector
protected function driver(?Drivers $driver = null): DriverConnector
{
$connector = $driver
? Drivers::tryFrom($driver->value)->connector()
Expand Down