Client.php 1.5 KB
<?php

namespace Lackoxygen\TiktokOpen\Base\Client;

use Lackoxygen\TiktokOpen\Application;
use Lackoxygen\TiktokOpen\Base\Event\Fail;
use Lackoxygen\TiktokOpen\Base\Event\Request;
use Lackoxygen\TiktokOpen\Base\Event\Response;
use Lackoxygen\TiktokOpen\Base\ServiceManager;
use Lackoxygen\TiktokOpen\Base\Traits\BaseClient;
use Lackoxygen\TiktokOpen\Wap\Listener;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Facades\Event;

class Client extends ServiceManager
{
    use BaseClient;

    private string $requestOption = '';

    private bool $withSession = false;

    public function __construct(Application $app)
    {
        parent::__construct($app);

        $this->listen();
    }

    protected function listen()
    {
        Event::listen(Request::class, [Listener::class, 'request']);
        Event::listen(Response::class, [Listener::class, 'response']);
        Event::listen(Fail::class, [Listener::class, 'fail']);
    }


    public function asForm(): Client
    {
        $this->requestOption = RequestOptions::FORM_PARAMS;

        return $this;
    }

    public function asJson(): Client
    {
        $this->requestOption = RequestOptions::JSON;

        return $this;
    }

    public function asMultipart(): Client
    {
        $this->requestOption = RequestOptions::MULTIPART;

        return $this;
    }

    public function withSession(): Client
    {
        $this->withSession = true;

        return $this;
    }

    public function refresh()
    {
        $this->requestOption = '';
        $this->withSession = false;
    }
}