定制 behamin/service-proxy 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

behamin/service-proxy

Composer 安装命令:

composer require behamin/service-proxy

包简介

for proxy or sending requests to other services with useful utilities

README 文档

README

Internal communication between services with useful tools
Make request by laravel http client

Installation

composer require behamin/service-proxy

Publish config

php artisan vendor:publish --provider="Behamin\ServiceProxy\Providers\ProxyServiceProvider" --tag config

Add services

Add your project's base url and global headers in proxy.php config

return [
    /**
     * Headers added to every request
     */
    'global_headers' => [
        'Accept' => 'application/json',
        ...
    ],

    'base_url' => env('PROXY_BASE_URL', env('APP_URL')),
];

Usage

Normal usage

use Behamin\ServiceProxy\Proxy;

// Http Get
Proxy::withToken('Your bearer token')
    ->acceptJson()
    ->retry(3)
    ->withHeaders([
        "Content-Type" => "application\json"
    ])->get('api/articles');
    
Proxy::post('api/articles', [
    "title" => "Test title",
    "body" => "Test body"
]);

Proxy::patch('api/articles/1', [
    "title" => "Test title",
    "body" => "Test body"
]);

Proxy::put('api/articles', [
    "title" => "Test title",
    "body" => "Test body"
]);

Proxy::delete('api/articles/1');

Using http request

use Behamin\ServiceProxy\Proxy;
use Illuminate\Http\Request;

public function index(Request $request) {
    $serviceName = 'test-service';
    Proxy::request($request, $serviceName);
}

Proxy events

On success

use Behamin\ServiceProxy\Proxy;
use Behamin\ServiceProxy\Responses\ProxyResponse;
 
Proxy::get('api/articles/1')->onSuccess(function (ProxyResponse $proxyResponse) {
        $data = $proxyResponse->data();
        $message = $proxyResponse->message();
        $response = $proxyResponse->response();
        $items = $proxyResponse->items();
        $count = $proxyResponse->count();
        ...
    });

On error

use Behamin\ServiceProxy\Proxy;
use Behamin\ServiceProxy\Exceptions\ProxyException;
 
Proxy::get('api/articles/1')->onSuccess(function (ProxyException $proxyException) {
        $proxyResponse = $proxyException->proxyResponse;
        $trace = $proxyException->getTraceAsString();
        ...
    });

On data success

use Behamin\ServiceProxy\Proxy;
 
Proxy::get('api/articles/1')->onDataSuccess(function (array $data) {
        $id = $data['id'];
    });

On data collection success

use Behamin\ServiceProxy\Proxy;
 
Proxy::get('api/articles/1')->onCollectionSuccess(function (array $items, int $count) {
        ...
    });

Proxy response methods

use Behamin\ServiceProxy\Proxy;

$proxyResponse = Proxy::get('api/articles/1');
Method Description
data() given data
items() give items
count() given items count
errors() given errors if there is
message() given message
onSuccess($closure) When http request is successful
onError($closure) When http request is with error
onCollectionSuccess($closure) Get collection when http request is successful
onDataSuccess($closure) Get data when http request is successful
throw() Throw error if http request failed
toException() Get exception if http request failed

Proxy request methods

Method Return Type
fake($callback = null) \Illuminate\Http\Client\Factory
accept(string $contentType) \Behamin\ServiceProxy\Http
acceptJson() \Behamin\ServiceProxy\Http
asForm() \Behamin\ServiceProxy\Http
asJson() \Behamin\ServiceProxy\Http
asMultipart() \Behamin\ServiceProxy\Http
async() \Behamin\ServiceProxy\Http
attach(string array $name, string $contents = '', string null $filename = null, array $headers = []) \Behamin\ServiceProxy\Http
baseUrl(string $url) \Behamin\ServiceProxy\Http
beforeSending(callable $callback) \Behamin\ServiceProxy\Http
bodyFormat(string $format) \Behamin\ServiceProxy\Http
contentType(string $contentType) \Behamin\ServiceProxy\Http
dd() \Behamin\ServiceProxy\Http
dump() \Behamin\ServiceProxy\Http
retry(int $times, int $sleep = 0) \Behamin\ServiceProxy\Http
sink(string resource $to)
stub(callable $callback) \Behamin\ServiceProxy\Http
timeout(int $seconds) \Behamin\ServiceProxy\Http
withBasicAuth(string $username, string $password) \Behamin\ServiceProxy\Http
withBody(resource string $content, string $contentType)
withCookies(array $cookies, string $domain) \Behamin\ServiceProxy\Http
withDigestAuth(string $username, string $password) \Behamin\ServiceProxy\Http
withHeaders(array $headers) \Behamin\ServiceProxy\Http
withMiddleware(callable $middleware) \Behamin\ServiceProxy\Http
withOptions(array $options) \Behamin\ServiceProxy\Http
withToken(string $token, string $type = 'Bearer') \Behamin\ServiceProxy\Http
withUserAgent(string $userAgent) \Behamin\ServiceProxy\Http
withoutRedirecting() \Behamin\ServiceProxy\Http
withoutVerifying() \Behamin\ServiceProxy\Http
pool(callable $callback) array
request(Request $request, string $service) \Behamin\ServiceProxy\Responses\ProxyResponse
get(string $url, array string
delete(string $url, array $data = []) \Behamin\ServiceProxy\Responses\ProxyResponse
head(string $url, array string
patch(string $url, array $data = []) \Behamin\ServiceProxy\Responses\ProxyResponse
post(string $url, array $data = []) \Behamin\ServiceProxy\Responses\ProxyResponse
put(string $url, array $data = []) \Behamin\ServiceProxy\Responses\ProxyResponse
send(string $method, string $url, array $options = []) \Behamin\ServiceProxy\Responses\ProxyResponse
fakeSequence(string $urlPattern = '*') \Illuminate\Http\Client\ResponseSequence
assertSent(callable $callback) void
assertNotSent(callable $callback) void
assertNothingSent() void
assertSentCount(int $count) void
assertSequencesAreEmpty() void

Mocking proxy response

You can use mock() on Proxy class before calling http methods and pass the json path in your 'tests/mock' directory, to mock a json for faking your Proxy response in test mode. Example:

use Behamin\ServiceProxy\Proxy;
Proxy::mock('response.json')->get('address');

behamin/service-proxy 适用场景与选型建议

behamin/service-proxy 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.23k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2020 年 10 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「http」 「Guzzle」 「proxy」 「service」 「request」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 behamin/service-proxy 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 behamin/service-proxy 我们能提供哪些服务?
定制开发 / 二次开发

基于 behamin/service-proxy 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.23k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 10
  • Watchers: 3
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-10-14