forked from overtrue/http
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ba68588
Showing
20 changed files
with
1,639 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 4 | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = false | ||
|
||
[*.{vue,js,scss}] | ||
charset = utf-8 | ||
indent_style = space | ||
indent_size = 2 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
* text=auto | ||
|
||
/tests export-ignore | ||
.gitattributes export-ignore | ||
.gitignore export-ignore | ||
.scrutinizer.yml export-ignore | ||
.travis.yml export-ignore | ||
phpunit.php export-ignore | ||
phpunit.xml.dist export-ignore | ||
phpunit.xml export-ignore | ||
.php_cs export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
*.DS_Store | ||
/vendor | ||
/coverage | ||
sftp-config.json | ||
composer.lock | ||
.subsplit | ||
.php_cs.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
$header = <<<EOF | ||
This file is part of the overtrue/http. | ||
(c) overtrue <[email protected]> | ||
This source file is subject to the MIT license that is bundled | ||
with this source code in the file LICENSE. | ||
EOF; | ||
|
||
return PhpCsFixer\Config::create() | ||
->setRiskyAllowed(true) | ||
->setRules(array( | ||
'@Symfony' => true, | ||
'header_comment' => array('header' => $header), | ||
'array_syntax' => array('syntax' => 'short'), | ||
'ordered_imports' => true, | ||
'no_useless_else' => true, | ||
'no_useless_return' => true, | ||
'php_unit_construct' => true, | ||
'php_unit_strict' => true, | ||
)) | ||
->setFinder( | ||
PhpCsFixer\Finder::create() | ||
->exclude('vendor') | ||
->in(__DIR__) | ||
) | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<p> | ||
<h1 align="center">Http</h1> | ||
</p> | ||
|
||
<p align="center">A basic http client wrapper.</p> | ||
|
||
## Installing | ||
|
||
```php | ||
$ composer require overtrue/http -vvv | ||
``` | ||
|
||
## Usage | ||
|
||
```php | ||
$client = Client::create(); | ||
|
||
$response = $client->get('https://httpbin.org/ip'); | ||
//{ | ||
// "ip": "1.2.3.4" | ||
//} | ||
``` | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "overtrue/http", | ||
"description": "A base http client.", | ||
"type": "library", | ||
"require": { | ||
"php": ">=7.0", | ||
"guzzlehttp/guzzle": "^6.3", | ||
"ext-curl": "*", | ||
"monolog/monolog": "^1.23" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^6.5", | ||
"mockery/mockery": "^1.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Overtrue\\Http\\": "src" | ||
} | ||
}, | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "overtrue", | ||
"email": "[email protected]" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false"> | ||
<testsuites> | ||
<testsuite name="Application Test Suite"> | ||
<directory>./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">src/</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the overtrue/http. | ||
* | ||
* (c) overtrue <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Overtrue\Http; | ||
|
||
use GuzzleHttp\Client as GuzzleClient; | ||
use GuzzleHttp\MessageFormatter; | ||
use GuzzleHttp\Middleware; | ||
use Monolog\Logger; | ||
use Overtrue\Http\Responses\Response; | ||
use Overtrue\Http\Traits\HasHttpRequests; | ||
|
||
/** | ||
* Class BaseClient. | ||
* | ||
* @author overtrue <[email protected]> | ||
*/ | ||
class Client | ||
{ | ||
use HasHttpRequests { request as performRequest; } | ||
|
||
/** | ||
* @var \Overtrue\Http\Config | ||
*/ | ||
protected $config; | ||
|
||
/** | ||
* @var | ||
*/ | ||
protected $baseUri; | ||
|
||
/** | ||
* @return static | ||
*/ | ||
public static function create(): self | ||
{ | ||
return new static(...func_get_args()); | ||
} | ||
|
||
/** | ||
* Client constructor. | ||
* | ||
* @param \Overtrue\Http\Config| $config | ||
*/ | ||
public function __construct(Config $config = null) | ||
{ | ||
$this->config = $config ?? new Config(); | ||
} | ||
|
||
/** | ||
* GET request. | ||
* | ||
* @param string $url | ||
* @param array $query | ||
* | ||
* @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string | ||
*/ | ||
public function get(string $url, array $query = []) | ||
{ | ||
return $this->request($url, 'GET', ['query' => $query]); | ||
} | ||
|
||
/** | ||
* POST request. | ||
* | ||
* @param string $url | ||
* @param array $data | ||
* | ||
* @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string | ||
*/ | ||
public function post(string $url, array $data = []) | ||
{ | ||
return $this->request($url, 'POST', ['form_params' => $data]); | ||
} | ||
|
||
/** | ||
* JSON request. | ||
* | ||
* @param string $url | ||
* @param string|array $data | ||
* @param array $query | ||
* | ||
* @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string | ||
*/ | ||
public function postJson(string $url, array $data = [], array $query = []) | ||
{ | ||
return $this->request($url, 'POST', ['query' => $query, 'json' => $data]); | ||
} | ||
|
||
/** | ||
* Upload file. | ||
* | ||
* @param string $url | ||
* @param array $files | ||
* @param array $form | ||
* @param array $query | ||
* | ||
* @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string | ||
*/ | ||
public function upload(string $url, array $files = [], array $form = [], array $query = []) | ||
{ | ||
$multipart = []; | ||
|
||
foreach ($files as $name => $path) { | ||
$multipart[] = [ | ||
'name' => $name, | ||
'contents' => fopen($path, 'r'), | ||
]; | ||
} | ||
|
||
foreach ($form as $name => $contents) { | ||
$multipart[] = compact('name', 'contents'); | ||
} | ||
|
||
return $this->request($url, 'POST', ['query' => $query, 'multipart' => $multipart]); | ||
} | ||
|
||
/** | ||
* @param string $url | ||
* @param string $method | ||
* @param array $options | ||
* @param bool $returnRaw | ||
* | ||
* @return \Psr\Http\Message\ResponseInterface|\Overtrue\Http\Support\Collection|array|object|string | ||
*/ | ||
public function request(string $url, string $method = 'GET', array $options = [], $returnRaw = false) | ||
{ | ||
if (empty($this->middlewares)) { | ||
$this->pushMiddleware($this->logMiddleware(), 'log'); | ||
} | ||
|
||
$response = $this->performRequest($url, $method, $options); | ||
|
||
return $returnRaw ? $response : $this->castResponseToType($response, $this->app->config->get('response_type')); | ||
} | ||
|
||
/** | ||
* @param string $url | ||
* @param string $method | ||
* @param array $options | ||
* | ||
* @return \Overtrue\Http\Responses\Response | ||
*/ | ||
public function requestRaw(string $url, string $method = 'GET', array $options = []) | ||
{ | ||
return Response::buildFromPsrResponse($this->request($url, $method, $options, true)); | ||
} | ||
|
||
/** | ||
* @param \GuzzleHttp\Client $client | ||
* | ||
* @return \Overtrue\Http\Client | ||
*/ | ||
public function setHttpClient(GuzzleClient $client): self | ||
{ | ||
$this->httpClient = $client; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Return GuzzleHttp\Client instance. | ||
* | ||
* @return \GuzzleHttp\Client | ||
*/ | ||
public function getHttpClient(): GuzzleClient | ||
{ | ||
if (!($this->httpClient instanceof GuzzleClient)) { | ||
$this->httpClient = new GuzzleClient(); | ||
} | ||
|
||
return $this->httpClient; | ||
} | ||
|
||
/** | ||
* Log the request. | ||
* | ||
* @return \Closure | ||
*/ | ||
protected function logMiddleware() | ||
{ | ||
$formatter = new MessageFormatter($this->config->getOption('http.log_template', MessageFormatter::DEBUG)); | ||
|
||
return Middleware::log($this->app['logger'], $formatter); | ||
} | ||
} |
Oops, something went wrong.