Skip to content

Commit 01352a4

Browse files
authored
Merge pull request #58 from mchojrin/patch-1
Adding a setting to define the API base URI
2 parents 9af2a88 + 7874dd5 commit 01352a4

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

readme.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,24 @@ Expected settings are as follows:
9090
```php
9191
use Noweh\TwitterApi\Client;
9292

93-
$settings['account_id']
94-
$settings['access_token'],
95-
$settings['access_token_secret'],
96-
$settings['consumer_key'],
97-
$settings['consumer_secret'],
98-
$settings['bearer_token'],
99-
$settings['free_mode'] = false; // Optional
93+
$settings = [
94+
'account_id' => 'YOUR_ACCOUNT_ID',
95+
'access_token' => 'YOUR_ACCESS_TOKEN',
96+
'access_token_secret' => 'YOUR_TOKEN_SECRET',
97+
'consumer_key' => 'YOUR_CONSUMER_KEY',
98+
'consumer_secret' => 'YOUR_CONSUMER_SECRET',
99+
'bearer_token' => 'YOUR_BEARER_TOKEN',
100+
'free_mode' => false, // Optional
101+
'api_base_uri' => 'https://api.twitter.com/2/', // Optional
102+
];
100103

101104
$client = new Client($settings);
102105
```
103106

107+
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.
108+
109+
For a quick mock server setup you can use [mockoon](https://mockoon.com/).
110+
104111
### API Functionality
105112
All API calls are triggered when the `performRequest()` method is invoked.
106113
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).

src/AbstractController.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ abstract class AbstractController
6262
/** @var array<string> $post_body */
6363
protected array $post_body = [];
6464

65+
/** @var string> $baseUri */
66+
private string $api_base_uri;
67+
6568
/**
6669
* Creates object. Requires an array of settings.
6770
* @param array<string> $settings
@@ -74,6 +77,12 @@ public function __construct(array $settings)
7477
$this->parseSettings($settings);
7578
}
7679

80+
private function getAPIBaseURI(): string
81+
{
82+
return $this->api_base_uri;
83+
}
84+
85+
7786
/**
7887
* Perform the request to Twitter API
7988
* @param array<string, mixed> $postData
@@ -90,7 +99,7 @@ public function performRequest(array $postData = [], $withHeaders = false)
9099

91100
if ($this->auth_mode === 0) { // Bearer Token
92101
// Inject the Bearer token header
93-
$client = new Client(['base_uri' => self::API_BASE_URI]);
102+
$client = new Client(['base_uri' => $this->getAPIBaseURI()]);
94103
$headers['Authorization'] = 'Bearer ' . $this->bearer_token;
95104
} elseif ($this->auth_mode === 1) { // OAuth 1.0a User Context
96105
// Insert Oauth1 middleware
@@ -103,7 +112,7 @@ public function performRequest(array $postData = [], $withHeaders = false)
103112
]);
104113
$stack->push($middleware);
105114
$client = new Client([
106-
'base_uri' => self::API_BASE_URI,
115+
'base_uri' => $this->getAPIBaseURI(),
107116
'handler' => $stack,
108117
'auth' => 'oauth'
109118
]);
@@ -205,6 +214,7 @@ private function parseSettings(array $settings): void
205214
$this->access_token = $settings['access_token'];
206215
$this->access_token_secret = $settings['access_token_secret'];
207216
$this->free_mode = $settings['free_mode'] ?? false;
217+
$this->api_base_uri = $settings['api_base_uri'] ?? self::API_BASE_URI;
208218
}
209219

210220
/**

0 commit comments

Comments
 (0)