Skip to content

Commit

Permalink
up: update some for client config and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 17, 2021
1 parent 1d6e05d commit 75801bd
Show file tree
Hide file tree
Showing 21 changed files with 298 additions and 180 deletions.
25 changes: 25 additions & 0 deletions example/coreq.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use PhpComp\Http\Client\Client;
use PhpComp\Http\Client\ClientConst;
use PhpComp\Http\Client\Swoole\CoClient;

require dirname(__DIR__) . '/test/bootstrap.php';

$client = Client::factory([
'driver' => CoClient::driverName(),
]);
$client->setUserAgent(ClientConst::USERAGENT_CURL);

// debug
// $client->setDebug(true);
// $client->setOption('logFile', './client-req.log');

$client->get('https://cht.sh/php/array_shift');

vdump(
$client->getDriverName(),
$client->getResponseHeaders()
);

echo $client->getResponseBody();
22 changes: 22 additions & 0 deletions example/demo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use PhpComp\Http\Client\Client;
use PhpComp\Http\Client\ClientConst;

require dirname(__DIR__) . '/test/bootstrap.php';

$client = Client::factory([]);
$client->setUserAgent(ClientConst::USERAGENT_CURL);

// debug
// $client->setDebug(true);
// $client->setOption('logFile', './client-req.log');

$client->get('https://cht.sh/php/array_shift');

vdump(
$client->getDriverName(),
$client->getResponseHeaders()
);

echo $client->getResponseBody();
4 changes: 2 additions & 2 deletions phpunit.xml.dist → phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="test/boot.php"
colors="false"
bootstrap="test/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
Expand Down
73 changes: 49 additions & 24 deletions src/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ abstract class AbstractClient implements ClientInterface
* for create psr7 ResponseInterface instance
*
* @var Closure function(): ResponseInterface {..}
* @psalm-var Closure(): ResponseInterface
*/
protected $responseCreator;

Expand All @@ -60,10 +61,10 @@ abstract class AbstractClient implements ClientInterface
// enable SSL verify
'sslVerify' => false,
// request headers
'headers' => [// name => value
],
'cookies' => [// name => value
],
// name => value
'headers' => [],
// name => value
'cookies' => [],
'proxy' => [
// 'host' => '',
// 'port' => '',
Expand All @@ -79,14 +80,15 @@ abstract class AbstractClient implements ClientInterface
// send data(todo)
'data' => [],
'json' => [],
// extra
// 一些针对不同驱动的自定义选项
// 'curlOptions' => [],
// 'coOptions' => [],
// 'streamContextOptions' => [],
];

/**
* global options data. init from $defaultOptions
* Global options data. init from $defaultOptions
*
* @see AbstractClient::$defaultOptions
* @var array
Expand All @@ -110,11 +112,13 @@ abstract class AbstractClient implements ClientInterface
* [ 'Content-Type' => 'Content-Type: application/json' ]
*
* @var array
* @psalm-var array<string, string>
*/
protected $headers = [];

/**
* @var array
* @psalm-var array<string, string>
*/
protected $cookies = [];

Expand Down Expand Up @@ -147,6 +151,17 @@ abstract class AbstractClient implements ClientInterface
*/
protected $responseHeaders = [];

/**
* @param array $options
*
* @return static
* @throws RuntimeException
*/
public static function new(array $options = []): ClientInterface
{
return new static($options);
}

/**
* @param array $options
*
Expand All @@ -158,6 +173,17 @@ public static function create(array $options = []): ClientInterface
return new static($options);
}

/**
* @return string
*/
public static function driverName(): string
{
$class = static::class;
$name = substr($class, strrpos($class, '\\') + 1);

return strtolower(str_replace('Client', '', $name));
}

/**
* SimpleCurl constructor.
*
Expand Down Expand Up @@ -257,9 +283,9 @@ public function head(string $url, $params = null, array $headers = [], array $op

/**
* @param string $url
* @param null $params
* @param array $headers
* @param array $options
* @param null $params
* @param array $headers
* @param array $options
*
* @return ClientInterface
*/
Expand Down Expand Up @@ -289,9 +315,9 @@ public function sendRequest(RequestInterface $request): ResponseInterface

/**
* @param string $url
* @param mixed $data
* @param array $headers
* @param array $options
* @param mixed $data
* @param array $headers
* @param array $options
*
* @return ClientInterface
*/
Expand Down Expand Up @@ -368,14 +394,14 @@ public function SSLVerify(bool $enable): self
/**
* Set contents of HTTP Cookie header.
*
* @param string $key The name of the cookie
* @param string $key The name of the cookie
* @param string $value The value for the provided cookie name
*
* @return $this
*/
public function setCookie(string $key, $value): ClientInterface
{
$this->cookies[$key] = $value;
$this->cookies[$key] = (string)$value;
return $this;
}

Expand Down Expand Up @@ -471,7 +497,7 @@ public function setUserAgent(string $userAgent): ClientInterface
*
* @param string $user
* @param string $pwd
* @param int $authType CURLAUTH_BASIC CURLAUTH_DIGEST
* @param int $authType CURLAUTH_BASIC CURLAUTH_DIGEST
*
* @return $this
*/
Expand All @@ -491,7 +517,7 @@ public function setUserAuth(string $user, string $pwd = '', int $authType = self

/**
* @param string $host
* @param int $port
* @param int $port
*
* @return $this
*/
Expand Down Expand Up @@ -533,7 +559,7 @@ public function setHeaders(array $headers): ClientInterface

/**
* @param array $headers
* @param bool $override
* @param bool $override
*
* @return $this
*/
Expand All @@ -549,7 +575,7 @@ public function addHeaders(array $headers, bool $override = true): ClientInterfa
/**
* @param string $name
* @param string $value
* @param bool $override
* @param bool $override
*
* @return $this
*/
Expand Down Expand Up @@ -588,7 +614,7 @@ public function delHeader($names): ClientInterface

/**
* @param string $url
* @param mixed $data
* @param mixed $data
*
* @return string
*/
Expand Down Expand Up @@ -754,7 +780,7 @@ public function getBaseUrl(): string

/**
* @param int|string $name
* @param null $default
* @param null|mixed $default
*
* @return mixed
*/
Expand Down Expand Up @@ -900,7 +926,7 @@ public function getResponseHeaders(): array

/**
* @param string $name
* @param null $default
* @param null $default
*
* @return string
*/
Expand Down Expand Up @@ -936,6 +962,8 @@ public function isRedirect(): bool

/**
* Was an 'error' returned (client error or server error).
*
* @return bool
*/
public function isError(): bool
{
Expand All @@ -955,9 +983,6 @@ public function getStatusCode(): int
*/
public function getDriverName(): string
{
$class = static::class;
$name = substr($class, strrpos($class, '\\') + 1);

return strtolower(str_replace('Client', '', $name));
return self::driverName();
}
}
17 changes: 0 additions & 17 deletions src/CType.php

This file was deleted.

22 changes: 15 additions & 7 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Class Client
*
* @package PhpComp\Http\Client
*
* @method static ClientInterface get(string $url, $params = null, array $headers = [], array $options = [])
* @method static ClientInterface delete(string $url, $params = null, array $headers = [], array $options = [])
* @method static ClientInterface head(string $url, $params = null, array $headers = [], array $options = [])
Expand Down Expand Up @@ -59,28 +60,35 @@ class Client
private static $defaultConfig = [];

/**
* @param array $config
* Quick create an client
*
* [
* 'driver' => 'curl', // curl, stream, fsock, fopen, file, co, co2
* // ...
* 'driver' => 'curl', // curl, stream, fsock, fopen, file, co, co2
* // ...
* ]
*
* @param array $config more see {@see AbstractClient::$options}
* @psalm-param array{driver:string, } $config
*
* @return ClientInterface|AbstractClient
*/
public static function factory(array $config): ClientInterface
{
$name = $config['driver'] ?? '';
$name = $config['driver'] ?? '';
$class = self::$drivers[$name] ?? '';

if (!$class = self::$drivers[$name] ?? '') {
if (!$class) {
// auto select
foreach (self::$drivers as $driverClass) {
if ($driverClass::isAvailable()) {
$class = $driverClass;
break;
}
}
} else {
// remove key: 'driver'
}

// remove key: 'driver'
if ($name) {
unset($config['driver']);
}

Expand Down
21 changes: 21 additions & 0 deletions src/ClientConst.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types=1);

namespace PhpComp\Http\Client;

/**
* Class ClientConst
*
* @package PhpComp\Http\Client
*/
class ClientConst
{
public const HTML = 'text/html';
public const JSON = 'application/json';
public const XML = 'application/xml';

public const MULTIPART = 'multipart/form-data';

public const USERAGENT_CURL = 'CURL/7.64.1';

public const USERAGENT_BROWSER = 'Mozilla/5.0 (Linux; Android 9; MI 6 Build/PKQ1.190118.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/86.0.4240.99 XWEB/3121 MMWEBSDK/201201 Mobile Safari/537.36 MMWEBID/356 MicroMessenger/8.0.1.1841(0x2800015D) Process/appbrand2 WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64 MiniProgramEnv/android';
}
Loading

0 comments on commit 75801bd

Please sign in to comment.