Skip to content

Commit 8e836e8

Browse files
committed
v1.0.0
0 parents  commit 8e836e8

File tree

12 files changed

+614
-0
lines changed

12 files changed

+614
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
build
2+
composer.lock
3+
docs
4+
vendor
5+
coverage

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Changelog
2+
3+
All notable changes will be documented in this file
4+
5+
## 1.0.0 - 2021-09-13
6+
- compatibility test with Laravel 8 ang Guzzle 7

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) BMBC
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
2+
<p><img src="https://eu.ui-avatars.com/api/?name=Najm+Njeim?size=100" width="100"/></p>
3+
4+
## Nnjeim Guzzle Wrapper
5+
6+
A Laravel wrapper for the Guzzle client http library. It provides a fluent syntax to make http requests.
7+
8+
## Installation
9+
10+
You can install the package via composer:
11+
```
12+
composer require nnjeim/fetch
13+
```
14+
15+
## Configuration
16+
```
17+
php artisan vendor:publish --provider="Nnjeim\Fetch\FetchServiceProvider"
18+
```
19+
20+
## Usage
21+
22+
##### Fetch Facade
23+
24+
```
25+
use Nnjeim\Fetch\Fetch;
26+
27+
$countries = Fetch::setBaseUri('https://someapi.com')->get('countries');
28+
```
29+
##### FetchHelper Instantiation
30+
```
31+
use Nnjeim\Fetch\FetchHelper;
32+
33+
private $fetch;
34+
35+
public function __construct(FetchHelper $fetch) {
36+
37+
$this->fetch = $fetch;
38+
}
39+
40+
.
41+
.
42+
.
43+
return $this->fetch
44+
->setBaseUri('https://someapi.com')
45+
->get('countries');
46+
```
47+
48+
## Methods
49+
50+
##### Set the headers
51+
```
52+
Set the http headers
53+
54+
@return $this setHeaders(array $headers)
55+
```
56+
57+
##### Set the base uri
58+
```
59+
Sets the base uri for the composition of the http request url.
60+
61+
@return $this setBaseUri(string 'http://someapi.com/')
62+
```
63+
64+
##### Set the request url
65+
```
66+
Sets the http request url.
67+
68+
@return $this setUrl(string $url)
69+
```
70+
71+
##### Set the request method
72+
```
73+
Sets the http request method.
74+
75+
@return $this setMethod(string $method)
76+
```
77+
78+
##### Set the request body format
79+
```
80+
Sets the request body format. The required format are 'query' | 'form_params' | 'multipart'.
81+
82+
@return $this setBodyFormat(string $format)
83+
```
84+
85+
##### Async request
86+
```
87+
Sets the type of the request to async.
88+
89+
@return $this setAsync()
90+
```
91+
92+
##### Get request
93+
```
94+
Sets the body format to query.
95+
96+
@return array get(?string $url = null, ?array $data = null)
97+
```
98+
99+
##### Post request
100+
```
101+
Sets the body format to form-params.
102+
103+
@return array post(?string $url = null, ?array $data = null)
104+
```
105+
106+
##### Put request
107+
```
108+
Sets the body format to form-params.
109+
110+
@return array put(?string $url = null, ?array $data = null)
111+
```
112+
113+
##### Delete request
114+
```
115+
Sets the body format to query.
116+
117+
@return array delete(?string $url = null, ?array $data = null)
118+
```
119+
120+
##### Upload request
121+
```
122+
Sets the body format to multipart.
123+
124+
@return array upload(?string $url = null, ?array $data = null)
125+
```
126+
127+
## Response
128+
129+
```
130+
@return array
131+
132+
[
133+
'response' => ...,
134+
'status' => ...,
135+
];
136+
```
137+
## Testing
138+
139+
``` bash
140+
composer test
141+
```
142+
143+
## Changelog
144+
145+
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

composer.json

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"name": "nnjeim/fetch",
3+
"description": "Laravel wrapper for Guzzle client",
4+
"keywords": [
5+
"nnjeim",
6+
"http"
7+
],
8+
"homepage": "https://github.com/nnjeim/fetch.git",
9+
"license": "MIT",
10+
"type": "library",
11+
"authors": [
12+
{
13+
"name": "Najm Njeim",
14+
"email": "nnjeim@nnjeim.com",
15+
"role": "Developer"
16+
}
17+
],
18+
"require": {
19+
"php": ">=7.4",
20+
"guzzlehttp/guzzle": ">=7.0.0"
21+
},
22+
"require-dev": {
23+
"orchestra/testbench": ">=v4.0.0",
24+
"phpunit/phpunit": ">=8.5.8"
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"Nnjeim\\Fetch\\": "src/"
29+
}
30+
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"Nnjeim\\Fetch\\Tests\\": "tests/"
34+
}
35+
},
36+
"scripts": {
37+
"test": "vendor/bin/phpunit",
38+
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
39+
40+
},
41+
"config": {
42+
"sort-packages": true
43+
},
44+
"extra": {
45+
"laravel": {
46+
"providers": [
47+
"Nnjeim\\Fetch\\FetchServiceProvider"
48+
],
49+
"aliases": {
50+
"Fetch": "Nnjeim\\Fetch\\Fetch"
51+
}
52+
}
53+
}
54+
}

phpunit.xml.dist

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
<logging>
23+
<log type="tap" target="build/report.tap"/>
24+
<log type="junit" target="build/report.junit.xml"/>
25+
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
26+
<log type="coverage-text" target="build/coverage.txt"/>
27+
<log type="coverage-clover" target="build/logs/clover.xml"/>
28+
</logging>
29+
</phpunit>

src/Config/fetch.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
return [
4+
'headers' => [
5+
'Accept' => 'application/json',
6+
'Accept-Encoding' => 'gzip',
7+
],
8+
/*----------------------------------------------- /
9+
if the json_encode_mode is set to true the response
10+
will be converted from an object into an array
11+
/ ---------------------------------------------- */
12+
'json_encode_mode' => false
13+
];

src/Fetch.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Nnjeim\Fetch;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class Fetch extends Facade {
8+
/**
9+
* Get the registered name of the component.
10+
*
11+
* @return string
12+
*/
13+
protected static function getFacadeAccessor() {
14+
return 'fetch';
15+
}
16+
}

src/FetchFactory.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Nnjeim\Fetch;
4+
5+
use GuzzleHttp\Client;
6+
7+
class FetchFactory {
8+
9+
protected array $attributes = [];
10+
11+
/**
12+
* @param string $key
13+
* @return mixed
14+
*/
15+
public function __get(string $key) {
16+
17+
return $this->attributes[$key] ?? null;
18+
}
19+
20+
/**
21+
* @param $key
22+
* @param $value
23+
*/
24+
public function __set($key, $value) {
25+
26+
$this->attributes[$key] = $value;
27+
}
28+
29+
/**
30+
* @return array
31+
*/
32+
public function fetch() {
33+
34+
$this->send();
35+
36+
return [
37+
'response' => $this->attributes['response'],
38+
'status' => $this->attributes['status'],
39+
];
40+
}
41+
42+
private function send(): void {
43+
44+
$http = new Client([
45+
'base_uri' => $this->baseUri,
46+
]);
47+
48+
$requestMethod = strtolower($this->method) . ($this->async ? 'Async' : '');
49+
50+
$requestArray = [
51+
'headers' => array_merge(config('fetch.headers'), $this->headers ?? []),
52+
];
53+
54+
$requestArray[$this->bodyFormat] = $this->data;
55+
56+
try {
57+
58+
$request = $http->{$requestMethod}($this->url, $requestArray);
59+
60+
$response = $this->async ? $request->wait() : $request;
61+
62+
$this->response = json_decode($response->getBody(), config('fetch.json_encode_mode'));
63+
64+
$this->status = $response->getStatusCode();
65+
66+
} catch (\GuzzleHttp\Exception\ClientException $e) {
67+
68+
$this->response = json_decode($e->getResponse()->getBody()->getContents(), config('fetch.json_encode_mode'));
69+
70+
$this->status = $e->getCode();
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)