定制 mindwingx/service-call-adapter 二次开发

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

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

mindwingx/service-call-adapter

最新稳定版本:v1.0.6

Composer 安装命令:

composer require mindwingx/service-call-adapter

包简介

Laravel package designed to simplify the integration and communication with various microservices and third-party services.

README 文档

README

The Service Call Adapter is a Laravel package designed to simplify the integration and communication with various microservices and third-party services.

Features

  • Generate new service calls with a simple command
  • Easily set up and configure your services
  • Allocate multiple services by customized condition
  • Generated service calls are easy to access in the app/ServiceCalls directory

Installation

  • You can install the Service Call Adapter package via Composer:
composer require mindwingx/service-call-adapter

Usage

  • Generate a new service call using the following command:
php artisan sc:new <your-service-name>
  • To call the microservices or third-party endpoints, you can use the generated service calls. For example:
use Mindwingx\ServiceCallAdapter\ServiceCall;

try {
    $response = ServiceCall::handle()->yourServiceName(["dummy-payload"]);
} catch (\Exception $exception) {
    return $exception->getMessage();
}

return $response;

** Note: You have to strictly use the service name of creating after the handle() method.

** Customize and configure your services according to your specific needs.

Documentation

To prepare the needed data/payload for requests, pass them as an array in the yourServiceName method. The payload will be passed to the related service via the ServiceAdapter and ServiceCall classes.

Service Adapter Class example
  • You can create multiple instances of the ServiceCall class in the related ServiceAdapter class if you have different services with the same purpose. Handle them in the constructor of the ServiceAdapter class based on your conditions.
class DummyServiceAdapter implements ServiceCallAdapterInterface
{
    /**
     * @var ServiceCallInterface
     */
    private ServiceCallInterface $service;

    public function __construct()
    {
        /*
         * Note: you make multiple Service Call Classes and handle them here to
         * access by the related condition, etc.
         */

        $this->service = new FirstServiceCall();
    }

    /**
     * @throws GuzzleException
     */
    public function call(array $payload = []): ResponseInterface|array
    {
        return $this->service
            ->preparePayload($payload)
            ->getResult();
    }
}
Service Call Class example
  • The GuzzleHttp is used as the HTTP driver. In the getResult() method, it returns the request-response as an array. You can replace getArrayResponse() with getResponse() to get the default Guzzle response.
class FirstServiceCall extends ServiceCallHandler
{

    public function preparePayload(array $payload = []): self
    {
        //todo: set service call details
        $this->setUrl('https://service-url.io/api')
             ->setMethod() //  default: GET
             ->setQuery([])
             ->setHeaders([])
             ->setBody($payload);

        return $this;
    }

    /**
     * @throws GuzzleException
     */
    public function getResult(): ResponseInterface|array
    {
        return $this->sendRequest()
                    ->getArrayResponse();
    }
}
  • The generated service call will be available in the app/ServiceCalls directory.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvement, please submit an issue or a pull request on the GitHub repository.

License

The Service Call Adapter package is open-source software licensed under the MIT license.

Credits

The Service Call Adapter package is developed and maintained by Milad Roudgarian.

统计信息

  • 总下载量: 17
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-05