Skip to content

Commit

Permalink
Merge pull request #58 from mchojrin/patch-1
Browse files Browse the repository at this point in the history
Adding a setting to define the API base URI
  • Loading branch information
noweh authored Oct 24, 2024
2 parents 9af2a88 + 7874dd5 commit 01352a4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
21 changes: 14 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,24 @@ Expected settings are as follows:
```php
use Noweh\TwitterApi\Client;

$settings['account_id']
$settings['access_token'],
$settings['access_token_secret'],
$settings['consumer_key'],
$settings['consumer_secret'],
$settings['bearer_token'],
$settings['free_mode'] = false; // Optional
$settings = [
'account_id' => 'YOUR_ACCOUNT_ID',
'access_token' => 'YOUR_ACCESS_TOKEN',
'access_token_secret' => 'YOUR_TOKEN_SECRET',
'consumer_key' => 'YOUR_CONSUMER_KEY',
'consumer_secret' => 'YOUR_CONSUMER_SECRET',
'bearer_token' => 'YOUR_BEARER_TOKEN',
'free_mode' => false, // Optional
'api_base_uri' => 'https://api.twitter.com/2/', // Optional
];

$client = new Client($settings);
```

By changing the value of `'api_base_uri'` you can have the requests target a different server, for instance, a simulated one, thus making testing your application in isolation easier.

For a quick mock server setup you can use [mockoon](https://mockoon.com/).

### API Functionality
All API calls are triggered when the `performRequest()` method is invoked.
Depending on the context, this method can either be empty or contain data that will be sent as PostData (refer to examples of each call below).
Expand Down
14 changes: 12 additions & 2 deletions src/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ abstract class AbstractController
/** @var array<string> $post_body */
protected array $post_body = [];

/** @var string> $baseUri */
private string $api_base_uri;

/**
* Creates object. Requires an array of settings.
* @param array<string> $settings
Expand All @@ -74,6 +77,12 @@ public function __construct(array $settings)
$this->parseSettings($settings);
}

private function getAPIBaseURI(): string
{
return $this->api_base_uri;
}


/**
* Perform the request to Twitter API
* @param array<string, mixed> $postData
Expand All @@ -90,7 +99,7 @@ public function performRequest(array $postData = [], $withHeaders = false)

if ($this->auth_mode === 0) { // Bearer Token
// Inject the Bearer token header
$client = new Client(['base_uri' => self::API_BASE_URI]);
$client = new Client(['base_uri' => $this->getAPIBaseURI()]);
$headers['Authorization'] = 'Bearer ' . $this->bearer_token;
} elseif ($this->auth_mode === 1) { // OAuth 1.0a User Context
// Insert Oauth1 middleware
Expand All @@ -103,7 +112,7 @@ public function performRequest(array $postData = [], $withHeaders = false)
]);
$stack->push($middleware);
$client = new Client([
'base_uri' => self::API_BASE_URI,
'base_uri' => $this->getAPIBaseURI(),
'handler' => $stack,
'auth' => 'oauth'
]);
Expand Down Expand Up @@ -205,6 +214,7 @@ private function parseSettings(array $settings): void
$this->access_token = $settings['access_token'];
$this->access_token_secret = $settings['access_token_secret'];
$this->free_mode = $settings['free_mode'] ?? false;

Check failure on line 216 in src/AbstractController.php

View workflow job for this annotation

GitHub Actions / tests (7.4, prefer-stable)

Property Noweh\TwitterApi\AbstractController::$free_mode (bool) does not accept string|false.

Check failure on line 216 in src/AbstractController.php

View workflow job for this annotation

GitHub Actions / tests (7.4, prefer-stable)

Property Noweh\TwitterApi\AbstractController::$free_mode (bool) does not accept string|false.
$this->api_base_uri = $settings['api_base_uri'] ?? self::API_BASE_URI;
}

/**
Expand Down

0 comments on commit 01352a4

Please sign in to comment.