thephpleague / oauth1-client

OAuth 1 Client
MIT License
968 stars 73 forks source link

adding support for setting user agent #13

Closed stevenmaguire closed 9 years ago

stevenmaguire commented 9 years ago

In response to https://github.com/thephpleague/oauth1-client/issues/8, this PR proposes a solution that includes a new setUserAgent method which sets a protected property on the base Server class, so all vendor specific implementations can utilize this feature.

When building the Authorization header for each request, the custom header is passed into a buildHttpClientHeaders method, where it is merged with a default set of headers. This default set of headers is empty unless the userAgent is set, in which case it will include the header in the response.

This solution does not provide a method to unset the userAgent property, but the setUserAgent method accepts and sets null.

Additionally, the merge is configured in such a way that each of the requesting methods can set User-Agent independently and it will not be overwritten.

There is a new test that covers these new methods as well as an update to existing test to cover the case where the userAgent is not set, ensuring no User-Agent header is sent.

$header = $this->protocolHeader('GET', $url, $tokenCredentials);
$authorization_header = array('Authorization' => $header);
$headers = $this->buildHttpClientHeaders($authorization_header);

try {
    $response = $client->get($url, $headers)->send();
} catch (BadResponseException $e) {
    $response = $e->getResponse();
    $body = $response->getBody();
    $statusCode = $response->getStatusCode();

    throw new \Exception(
        "Received error [$body] with status code [$statusCode] when retrieving token credentials."
    );
}
bencorlett commented 9 years ago

Looks nice. Let me sit on this for a day or two. Could you update the variable names to camelCase throughout?

I know I'm annoying... :)

stevenmaguire commented 9 years ago

@bencorlett D'oh! Old habits die hard. New commit is in.

bencorlett commented 9 years ago

Merged! Thanks :)

bencorlett commented 9 years ago

Released 1.2.0 to respect Semver with this new functionality.

stevenmaguire commented 9 years ago

w00t! Thanks @bencorlett