dimanche 16 juin 2019

What is the best design pattern to consume REST API

I want to consume a Rest API in Laravel (an MVC framework) but I resort to use __call and was wonder if there is a better design pattern for this. Here is my Repository class:

namespace App\Repositories;

use App\Models\OnlinePayment;
use App\Models\Order;
use App\Models\Transaction;
use App\Models\User;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use SoapClient;

class Tavizkala
{
    protected $http;
    protected $user;

    public function __construct()
    {
        $this->http = new Client;
    }

    protected function index()
    {
        $url = config('tavizkala.url') . '/v2/quantum/users/' . $this->user->national_id . '/report';

        $data = [
            'user_gender'       => $this->user->gender ?? 1,
            'user_name'         => $this->user->name,
            'user_family'       => $this->user->family ?? 'خالی',
            'user_mobile'       => $this->user->mobile,
            'user_type'         => $this->user->type->name,
        ];

        $options = $this->options($data);
        $res = $this->http->request('GET', $url, $options);

        $response = json_decode($res->getBody(), true);

        return $response;
    }

    protected function indexData($request)
    {
        $url = config('tavizkala.url') . '/v2/quantum/users/' . $this->user->national_id . '/customers';

        $options = $this->options($request->all());
        $res = $this->http->request('GET', $url, $options);
        $response = response()->json(json_decode($res->getBody(), true), $res->getStatusCode());
        return $response;
    }

    protected function show($national_id)
    {
        $url = config('tavizkala.url') . '/v2/quantum/users/' . $this->user->national_id . '/customers/' . $national_id;

        $options = $this->options([]);
        $res = $this->http->request('GET', $url, $options);

        if ($res->getStatusCode() == 404) {
            abort(404);
        }
        $response = json_decode($res->getBody(), true);

        return $response;

    }

    protected function store($request)
    {
        $http = new Client;
        $url = config('tavizkala.url') . '/v2/quantum/users/' . $this->user->national_id . '/customers';
        $this->user = auth()->user();

        $data = array_merge(
            [
                'customer_national_id'  => $request->national_id,
                'customer_gender'       => $request->gender,
                'customer_name'         => $request->name,
                'customer_family'       => $request->family,
                'customer_phone'        => $request->phone,
                'customer_mobile'       => $request->mobile,
                'customer_city_id'      => $request->city_id,
            ], [
                'user_name'         => $this->user->name,
                'user_family'       => $this->user->family ?? 'خالی',
                'user_mobile'       => $this->user->mobile,
                'user_type'         => $this->user->type->name,
                'user_gender'       => $this->user->gender ?? 1,
            ]
        );

        $res = $http->request('POST', $url, [
            'headers' => [
                'Accept'        => 'application/json',
                'Content-Type'  => 'application/json',
                'Authorization' => 'Bearer ' . config('tavizkala.token'),
            ],
            'json' => $data,
            'http_errors' => false
        ]);

        if (! in_array($res->getStatusCode(), [200, 422])) {
            $error = ValidationException::withMessages([
                'name' => 'خطای ' . $res->getStatusCode() . ' در تعویض کالا'
            ]);
            throw $error;
        }

        $response = response()->json(json_decode($res->getBody(), true), $res->getStatusCode());
        return $response;
    }

    protected function options($data)
    {
        $options = [
            'headers' => [
                'Accept'        => 'application/json',
                'Content-Type'  => 'application/json',
                'Authorization' => 'Bearer ' . config('tavizkala.token'),
            ],
            'json' => $data,
            'http_errors' => false
        ];

        return $options;
    }

    public function __call($method, $arguments) {
        if (method_exists($this, $method)) {

            if (! isset($arguments[0]) || ! $arguments[0] instanceof User) {
                $this->user = auth()->user();
            } else {
                $this->user = $arguments[0];
                unset($arguments[0]);
            }

            return call_user_func_array(array($this, $method), $arguments);
        }
    }
}

then create an instance of it in controller constructor:

    public function __construct()
    {

        $this->tavizkala = new Tavizkala();
    }

and use it in controller like this:

$response = $this->tavizkala->indexData($user, $request);

or this:

$response = $this->tavizkala->indexData($request);

Aucun commentaire:

Enregistrer un commentaire