Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to pass your own client instance to the service #20

Closed
fyfey opened this issue Oct 2, 2015 · 3 comments
Closed

Ability to pass your own client instance to the service #20

fyfey opened this issue Oct 2, 2015 · 3 comments

Comments

@fyfey
Copy link
Contributor

fyfey commented Oct 2, 2015

I would like the ability to add my own instance of Client to the service which get's passed to all created API classes.

I would like to set up a events on the client to handle refreshing the OAuth token without having to handle each response separately.

Ideally I would like to be able to do the folloing:

    /**
     * @param string|null $apiKey
     * @param bool $oauth
     * @param HttpClient $client
     * @throws HubSpotException
     */
    protected function __construct($apiKey = null, $oauth = false, HttpClient $client = null)
    {
        $this->oauth = $oauth;
        $this->apiKey = $apiKey ?: getenv('HUBSPOT_API_KEY');
        if (empty($this->apiKey)) {
            throw new HubSpotException("You must provide a HubSpot api key.");
        }
        $this->client = $client;
    }

    /**
     * @param string $access_token HubSpot oauth access token
     * @param HttpClient $client An Http Client
     * @return static
     */
    public static function makeWithToken($access_token, HttpClient $client = null)
    {
        return new static($access_token, true, $client);
    }

    /**
     * @param string $name
     * @param null $arguments
     * @return mixed
     * @throws HubSpotException
     */
    public function __call($name, $arguments = null)
    {
        $apiClass = $this->getApiClassName($name);
        if (! (new \ReflectionClass($apiClass))->isInstantiable()) {
            throw new HubSpotException("Target [$apiClass] is not instantiable.");
        }
        $client = ($this->client) ?: new Client;
        return new $apiClass($this->apiKey, $client, $this->oauth);
    }

What are your thoughts? I'll happily create a pull request if you think this would be useful

@ryanwinchester
Copy link
Collaborator

Yeah I'm not familiar with how the oauth stuff works since I just use an api key.

If you think it would be helpful, we could do that, but maybe just move checking if the client is passed, to the constructor like this?

    /**
     * @param string|null $apiKey
     * @param bool $oauth
     * @param HttpClient $client
     * @throws HubSpotException
     */
    protected function __construct($apiKey = null, $oauth = false, HttpClient $client = null)
    {
        $this->oauth = $oauth;
        $this->apiKey = $apiKey ?: getenv('HUBSPOT_API_KEY');
        if (empty($this->apiKey)) {
            throw new HubSpotException("You must provide a HubSpot api key.");
        }
        $this->client = $client ?: new Client;
    }

    /**
     * @param string $access_token HubSpot oauth access token
     * @param HttpClient $client An Http Client
     * @return static
     */
    public static function makeWithToken($access_token, HttpClient $client = null)
    {
        return new static($access_token, true, $client);
    }

    /**
     * @param string $name
     * @param null $arguments
     * @return mixed
     * @throws HubSpotException
     */
    public function __call($name, $arguments = null)
    {
        $apiClass = $this->getApiClassName($name);
        if (! (new \ReflectionClass($apiClass))->isInstantiable()) {
            throw new HubSpotException("Target [$apiClass] is not instantiable.");
        }

        return new $apiClass($this->apiKey, $this->client, $this->oauth);
    }

@fyfey
Copy link
Contributor Author

fyfey commented Oct 2, 2015

Yeah that makes sense, it means there's only ever one client per HubSpotService too, which is a bit more efficient.

@ryanwinchester
Copy link
Collaborator

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants