Skip to content

Commit 3960c78

Browse files
committed
Add custom base URL support
1 parent a2ef231 commit 3960c78

35 files changed

+105
-56
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ Create a client with your API key and secret:
4848
$client = new Nexmo\Client(new Nexmo\Client\Credentials\Basic(API_KEY, API_SECRET));
4949
```
5050

51+
For testing purposes you may want to change the URL that `nexmo-php` makes requests to. You can do this by providing an array containing `base_url` as the second parameter when creating a `Nexmo\Client` instance.
52+
53+
```php
54+
$client = new Nexmo\Client(
55+
new Nexmo\Client\Credentials\Basic(API_KEY, API_SECRET),
56+
[
57+
'base_url' => 'https://example.com'
58+
]
59+
);
60+
61+
```
62+
5163
Examples
5264
--------
5365

src/Account/Client.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function makePricingRequest($country, $pricingType)
3838
]);
3939

4040
$request = new Request(
41-
\Nexmo\Client::BASE_REST . '/account/get-pricing/outbound/'.$pricingType.'?'.$queryString,
41+
$this->getClient()->getRestUrl() . '/account/get-pricing/outbound/'.$pricingType.'?'.$queryString,
4242
'GET',
4343
'php://temp'
4444
);
@@ -57,7 +57,7 @@ public function getBalance()
5757
{
5858

5959
$request = new Request(
60-
\Nexmo\Client::BASE_REST . '/account/get-balance',
60+
$this->getClient()->getRestUrl() . '/account/get-balance',
6161
'GET',
6262
'php://temp'
6363
);
@@ -82,7 +82,7 @@ public function topUp($trx)
8282
];
8383

8484
$request = new Request(
85-
\Nexmo\Client::BASE_REST . '/account/top-up'
85+
$this->getClient()->getRestUrl() . '/account/top-up'
8686
,'POST'
8787
, 'php://temp'
8888
, ['content-type' => 'application/x-www-form-urlencoded']
@@ -98,13 +98,13 @@ public function topUp($trx)
9898

9999
public function listSecrets($apiKey)
100100
{
101-
$body = $this->get( \Nexmo\Client::BASE_API . '/accounts/'.$apiKey.'/secrets');
101+
$body = $this->get( $this->getClient()->getApiUrl() . '/accounts/'.$apiKey.'/secrets');
102102
return SecretCollection::fromApi($body);
103103
}
104104

105105
public function getSecret($apiKey, $secretId)
106106
{
107-
$body = $this->get( \Nexmo\Client::BASE_API . '/accounts/'.$apiKey.'/secrets/'. $secretId);
107+
$body = $this->get( $this->getClient()->getApiUrl() . '/accounts/'.$apiKey.'/secrets/'. $secretId);
108108
return Secret::fromApi($body);
109109
}
110110

@@ -115,7 +115,7 @@ public function createSecret($apiKey, $newSecret)
115115
];
116116

117117
$request = new Request(
118-
\Nexmo\Client::BASE_API . '/accounts/'.$apiKey.'/secrets'
118+
$this->getClient()->getApiUrl() . '/accounts/'.$apiKey.'/secrets'
119119
,'POST'
120120
, 'php://temp'
121121
, ['content-type' => 'application/json']
@@ -134,7 +134,7 @@ public function createSecret($apiKey, $newSecret)
134134
public function deleteSecret($apiKey, $secretId)
135135
{
136136
$request = new Request(
137-
\Nexmo\Client::BASE_API . '/accounts/'.$apiKey.'/secrets/'. $secretId
137+
$this->getClient()->getApiUrl() . '/accounts/'.$apiKey.'/secrets/'. $secretId
138138
,'DELETE'
139139
, 'php://temp'
140140
, ['content-type' => 'application/json']

src/Application/Client.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function get($application)
4545
}
4646

4747
$request = new Request(
48-
\Nexmo\Client::BASE_API . $this->getCollectionPath() . '/' . $application->getId()
48+
$this->getClient()->getApiUrl() . $this->getCollectionPath() . '/' . $application->getId()
4949
,'GET'
5050
);
5151

@@ -74,7 +74,7 @@ public function post($application)
7474
$body = $application->getRequestData(false);
7575

7676
$request = new Request(
77-
\Nexmo\Client::BASE_API . $this->getCollectionPath()
77+
$this->getClient()->getApiUrl() . $this->getCollectionPath()
7878
,'POST',
7979
'php://temp',
8080
['content-type' => 'application/json']
@@ -110,7 +110,7 @@ public function put($application, $id = null)
110110
$body = $application->getRequestData(false);
111111

112112
$request = new Request(
113-
\Nexmo\Client::BASE_API . $this->getCollectionPath() . '/' . $id,
113+
$this->getClient()->getApiUrl() . $this->getCollectionPath() . '/' . $id,
114114
'PUT',
115115
'php://temp',
116116
['content-type' => 'application/json']
@@ -137,7 +137,7 @@ public function delete($application)
137137
}
138138

139139
$request = new Request(
140-
\Nexmo\Client::BASE_API . $this->getCollectionPath() . '/' . $id
140+
$this->getClient()->getApiUrl(). $this->getCollectionPath() . '/' . $id
141141
,'DELETE'
142142
);
143143

@@ -204,4 +204,4 @@ protected function createFromArray($array)
204204

205205
return $application;
206206
}
207-
}
207+
}

src/Call/Call.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function __construct($id = null)
6969
public function get()
7070
{
7171
$request = new Request(
72-
\Nexmo\Client::BASE_API . Collection::getCollectionPath() . '/' . $this->getId()
72+
$this->getClient()->getApiUrl() . Collection::getCollectionPath() . '/' . $this->getId()
7373
,'GET'
7474
);
7575

@@ -104,7 +104,7 @@ protected function getException(ResponseInterface $response)
104104
public function put($payload)
105105
{
106106
$request = new Request(
107-
\Nexmo\Client::BASE_API . Collection::getCollectionPath() . '/' . $this->getId()
107+
$this->getClient()->getApiUrl() . Collection::getCollectionPath() . '/' . $this->getId()
108108
,'PUT',
109109
'php://temp',
110110
['content-type' => 'application/json']
@@ -298,4 +298,4 @@ public function jsonUnserialize(array $json)
298298
$this->data = $json;
299299
$this->id = $json['uuid'];
300300
}
301-
}
301+
}

src/Call/Collection.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function delete($call = null, $type)
8484
}
8585

8686
$request = new Request(
87-
\Nexmo\Client::BASE_API . $this->getCollectionPath() . '/' . $call->getId() . '/' . $type
87+
$this->getClient()->getApiUrl() . $this->getCollectionPath() . '/' . $call->getId() . '/' . $type
8888
,'DELETE'
8989
);
9090

@@ -106,7 +106,7 @@ public function post($call)
106106
}
107107

108108
$request = new Request(
109-
\Nexmo\Client::BASE_API . $this->getCollectionPath()
109+
$this->getClient()->getApiUrl() . $this->getCollectionPath()
110110
,'POST',
111111
'php://temp',
112112
['content-type' => 'application/json']
@@ -200,4 +200,4 @@ public function offsetUnset($offset)
200200
{
201201
throw new \RuntimeException('can not unset collection properties');
202202
}
203-
}
203+
}

src/Call/Dtmf.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function put($dtmf = null)
6161
}
6262

6363
$request = new Request(
64-
\Nexmo\Client::BASE_API . Collection::getCollectionPath() . '/' . $this->getId() . '/dtmf',
64+
$this->getClient()->getApiUrl() . Collection::getCollectionPath() . '/' . $this->getId() . '/dtmf',
6565
'PUT',
6666
'php://temp',
6767
['content-type' => 'application/json']
@@ -136,4 +136,4 @@ public function offsetUnset($offset)
136136

137137
unset($this->data[$offset]);
138138
}
139-
}
139+
}

src/Call/Stream.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function put($stream = null)
6666
}
6767

6868
$request = new Request(
69-
\Nexmo\Client::BASE_API . Collection::getCollectionPath() . '/' . $this->getId() . '/stream',
69+
$this->getClient()->getApiUrl() . Collection::getCollectionPath() . '/' . $this->getId() . '/stream',
7070
'PUT',
7171
'php://temp',
7272
['content-type' => 'application/json']
@@ -80,7 +80,7 @@ public function put($stream = null)
8080
public function delete()
8181
{
8282
$request = new Request(
83-
\Nexmo\Client::BASE_API . Collection::getCollectionPath() . '/' . $this->getId() . '/stream',
83+
$this->getClient()->getApiUrl() . Collection::getCollectionPath() . '/' . $this->getId() . '/stream',
8484
'DELETE'
8585
);
8686

@@ -124,4 +124,4 @@ function jsonSerialize()
124124
{
125125
return $this->data;
126126
}
127-
}
127+
}

src/Call/Talk.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function put($talk = null)
7373
}
7474

7575
$request = new Request(
76-
\Nexmo\Client::BASE_API . Collection::getCollectionPath() . '/' . $this->getId() . '/talk',
76+
$this->getClient()->getApiUrl() . Collection::getCollectionPath() . '/' . $this->getId() . '/talk',
7777
'PUT',
7878
'php://temp',
7979
['content-type' => 'application/json']
@@ -87,7 +87,7 @@ public function put($talk = null)
8787
public function delete()
8888
{
8989
$request = new Request(
90-
\Nexmo\Client::BASE_API . Collection::getCollectionPath() . '/' . $this->getId() . '/talk',
90+
$this->getClient()->getApiUrl() . Collection::getCollectionPath() . '/' . $this->getId() . '/talk',
9191
'DELETE'
9292
);
9393

@@ -159,4 +159,4 @@ public function offsetUnset($offset)
159159

160160
unset($this->data[$offset]);
161161
}
162-
}
162+
}

src/Client.php

+21-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ public function __construct(CredentialsInterface $credentials, $options = array(
9191
$this->validateAppOptions($options['app']);
9292
}
9393

94+
// Set the default URLs. Keep the constants for
95+
// backwards compatibility
96+
$this->apiUrl = static::BASE_API;
97+
$this->restUrl = static::BASE_REST;
98+
99+
// If they've provided a base URL, use that instead
100+
// of the defaults
101+
if (isset($options['base_url'])) {
102+
$this->restUrl = $options['base_url'];
103+
$this->apiUrl = $options['base_url'];
104+
}
105+
94106
$this->setFactory(new MapFactory([
95107
'account' => 'Nexmo\Account\Client',
96108
'insights' => 'Nexmo\Insights\Client',
@@ -106,6 +118,14 @@ public function __construct(CredentialsInterface $credentials, $options = array(
106118
], $this));
107119
}
108120

121+
public function getRestUrl() {
122+
return $this->restUrl;
123+
}
124+
125+
public function getApiUrl() {
126+
return $this->apiUrl;
127+
}
128+
109129
/**
110130
* Set the Http Client to used to make API requests.
111131
*
@@ -484,4 +504,4 @@ protected function needsKeypairAuthentication(\Psr\Http\Message\RequestInterface
484504

485505
return $isCallEndpoint || $isRecordingUrl || $isStitchEndpoint || $isUserEndpoint;
486506
}
487-
}
507+
}

src/Conversations/Collection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function post($conversation)
8686
}
8787

8888
$request = new Request(
89-
\Nexmo\Client::BASE_API . $this->getCollectionPath()
89+
$this->getClient()->getApiUrl() . $this->getCollectionPath()
9090
,'POST',
9191
'php://temp',
9292
['content-type' => 'application/json']

src/Conversations/Conversation.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function __toString()
6666
public function get()
6767
{
6868
$request = new Request(
69-
\Nexmo\Client::BASE_API . Collection::getCollectionPath() . '/' . $this->getId()
69+
$this->getClient()->getApiUrl() . Collection::getCollectionPath() . '/' . $this->getId()
7070
,'GET'
7171
);
7272

@@ -95,7 +95,7 @@ public function jsonUnserialize(array $json)
9595

9696
public function members()
9797
{
98-
$response = $this->getClient()->get(\Nexmo\Client::BASE_API . Collection::getCollectionPath() . '/' . $this->getId() .'/members');
98+
$response = $this->getClient()->get($this->getClient()->getApiUrl() . Collection::getCollectionPath() . '/' . $this->getId() .'/members');
9999

100100
if($response->getStatusCode() != '200'){
101101
throw $this->getException($response);
@@ -119,7 +119,7 @@ public function inviteMember(User $user)
119119
public function removeMember(User $user)
120120
{
121121
$response = $this->getClient()->delete(
122-
\Nexmo\Client::BASE_API . Collection::getCollectionPath() . '/' . $this->getId() .'/members/'. $user->getId()
122+
$this->getClient()->getApiUrl() . Collection::getCollectionPath() . '/' . $this->getId() .'/members/'. $user->getId()
123123
);
124124

125125
if($response->getStatusCode() != '200'){
@@ -133,7 +133,7 @@ public function sendPostAction(User $user, $action, $channel = 'app') {
133133
$body['channel'] = ['type' => $channel];
134134

135135
$response = $this->getClient()->post(
136-
\Nexmo\Client::BASE_API . Collection::getCollectionPath() . '/' . $this->getId() .'/members',
136+
$this->getClient()->getApiUrl() . Collection::getCollectionPath() . '/' . $this->getId() .'/members',
137137
$body
138138
);
139139

@@ -179,4 +179,4 @@ protected function getException(ResponseInterface $response)
179179
}
180180

181181

182-
}
182+
}

src/Conversion/Client.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected function sendConversion($type, $message_id, $delivered, $timestamp=nul
3333
}
3434

3535
$response = $this->client->postUrlEncoded(
36-
\Nexmo\Client::BASE_API . '/conversions/'.$type.'?'.http_build_query($params),
36+
$this->getClient()->getApiUrl() . '/conversions/'.$type.'?'.http_build_query($params),
3737
[]
3838
);
3939

@@ -60,4 +60,4 @@ protected function getException(ResponseInterface $response)
6060
return $e;
6161
}
6262

63-
}
63+
}

src/Entity/CollectionTrait.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ protected function fetchPage($absoluteUri)
234234

235235
//
236236
$request = new Request(
237-
\Nexmo\Client::BASE_API . $absoluteUri,
237+
$this->getClient()->getApiUrl() . $absoluteUri,
238238
'GET'
239239
);
240240

@@ -247,4 +247,4 @@ protected function fetchPage($absoluteUri)
247247
$this->response = $response;
248248
$this->page = json_decode($this->response->getBody()->getContents(), true);
249249
}
250-
}
250+
}

src/Insights/Client.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function makeRequest($path, $number, $additionalParams = [])
8080
] + $additionalParams);
8181

8282
$request = new Request(
83-
\Nexmo\Client::BASE_API . $path.'?'.$queryString,
83+
$this->getClient()->getApiUrl(). $path.'?'.$queryString,
8484
'GET',
8585
'php://temp',
8686
[

0 commit comments

Comments
 (0)