Skip to content

Commit bd3a60e

Browse files
Pull request #17: Add requests PATCH
Merge in SDK/php_telesign from feature/EOA-2509 to developer * commit 'bc287c5345d815786c2c94aef0a3e518b9d15c74': Change fictitious data Put correct capitilzation Fix testing fix number version Update RELEASE Add requests PATCH
2 parents 36f38af + bc287c5 commit bd3a60e

File tree

5 files changed

+79
-30
lines changed

5 files changed

+79
-30
lines changed

RELEASE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
3.0.7
2+
- Added support for PATCH requests
3+
14
3.0.6
25
- Add tracking of when SDK is used as a dependency
36

src/rest/RestClient.php

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
use telesign\sdk\Config;
99

1010
/**
11-
* The TeleSign RestClient is a generic HTTP REST client that can be extended to make requests against any of
12-
* TeleSign's REST API endpoints.
11+
* The Telesign RestClient is a generic HTTP REST client that can be extended to make requests against any of
12+
* Telesign's REST API endpoints.
1313
*
1414
* RequestEncodingMixin offers the function _encode_params for url encoding the body for use in string_to_sign outside
1515
* of a regular HTTP request.
@@ -25,7 +25,7 @@ class RestClient {
2525
protected $rest_endpoint;
2626

2727
/**
28-
* TeleSign RestClient instantiation function
28+
* Telesign RestClient instantiation function
2929
*
3030
* @param string $customer_id Your customer_id string associated with your account
3131
* @param string $api_key Your api_key string associated with your account
@@ -73,16 +73,16 @@ function setRestEndpoint($rest_endpoint) {
7373
}
7474

7575
/**
76-
* Generates the TeleSign REST API headers used to authenticate requests.
76+
* Generates the Telesign REST API headers used to authenticate requests.
7777
*
7878
* Creates the canonicalized string_to_sign and generates the HMAC signature. This is used to authenticate requests
79-
* against the TeleSign REST API.
79+
* against the Telesign REST API.
8080
*
8181
* See https://developer.telesign.com/docs/authentication-1 for detailed API documentation.
8282
*
8383
* @param string $customer_id Your account customer_id
8484
* @param string $api_key Your account api_key
85-
* @param string $method_name The HTTP method name of the request, should be one of 'POST', 'GET', 'PUT' or
85+
* @param string $method_name The HTTP method name of the request, should be one of 'POST', 'GET', 'PUT', 'PATCH' or
8686
* 'DELETE'
8787
* @param string $resource The partial resource URI to perform the request against
8888
* @param string $url_encoded_fields HTTP body parameters to perform the HTTP request with, must be urlencoded
@@ -92,7 +92,7 @@ function setRestEndpoint($rest_endpoint) {
9292
* @param string $content_type Content-Type to send in header
9393
* @param string $auth_method Authentication method
9494
*
95-
* @return array The TeleSign authentication headers
95+
* @return array The Telesign authentication headers
9696
*/
9797
static function generateTelesignHeaders (
9898
$customer_id,
@@ -162,7 +162,7 @@ static function generateTelesignHeaders (
162162
}
163163

164164
/**
165-
* Generic TeleSign REST API POST handler
165+
* Generic Telesign REST API POST handler
166166
*
167167
* @param string $resource The partial resource URI to perform the request against
168168
* @param array $fields Body params to perform the POST request with
@@ -176,7 +176,7 @@ function post (...$args) {
176176
}
177177

178178
/**
179-
* Generic TeleSign REST API GET handler
179+
* Generic Telesign REST API GET handler
180180
*
181181
* @param string $resource The partial resource URI to perform the request against
182182
* @param array $fields Query params to perform the GET request with
@@ -190,7 +190,7 @@ function get (...$args) {
190190
}
191191

192192
/**
193-
* Generic TeleSign REST API PUT handler
193+
* Generic Telesign REST API PUT handler
194194
*
195195
* @param string $resource The partial resource URI to perform the request against
196196
* @param array $fields Query params to perform the DELETE request with
@@ -204,7 +204,7 @@ function put (...$args) {
204204
}
205205

206206
/**
207-
* Generic TeleSign REST API DELETE handler
207+
* Generic Telesign REST API DELETE handler
208208
*
209209
* @param string $resource The partial resource URI to perform the request against
210210
* @param array $fields Query params to perform the DELETE request with
@@ -218,7 +218,21 @@ function delete (...$args) {
218218
}
219219

220220
/**
221-
* Generic TeleSign REST API request handler
221+
* Generic Telesign REST API PATCH handler
222+
*
223+
* @param string $resource The partial resource URI to perform the request against
224+
* @param array $fields Query params to perform the PATCH request with
225+
* @param string $date The date and time of the request
226+
* @param string $nonce A unique cryptographic nonce for the request
227+
*
228+
* @return \telesign\sdk\rest\Response The RestClient Response object
229+
*/
230+
function patch (...$args) {
231+
return $this->execute("PATCH", ...$args);
232+
}
233+
234+
/**
235+
* Generic Telesign REST API request handler
222236
*
223237
* @param string $resource The partial resource URI to perform the request against
224238
* @param array $fields Body of query params to perform the HTTP request with
@@ -251,7 +265,7 @@ protected function execute ($method_name, $resource, $fields = [], $date = null,
251265
$auth_method
252266
);
253267

254-
$option = in_array($method_name, [ "POST", "PUT" ]) ? "body" : "query";
268+
$option = in_array($method_name, [ "POST", "PUT", "PATCH" ]) ? "body" : "query";
255269

256270
return new Response($this->client->request($method_name, $resource, [
257271
"headers" => $headers,

test/ClientTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,15 @@ abstract function getRequestExamples();
2222
*/
2323
function testRequestFormat ($client, $method, $args, $expected_url, $expected_fields) {
2424
$mock = new MockHandler([ new Response() ]);
25-
$client = new $client(
26-
self::EXAMPLE_CUSTOMER_ID, self::EXAMPLE_API_KEY, self::EXAMPLE_REST_ENDPOINT, 10, null, $mock
27-
);
25+
if (strpos($client, 'telesign\enterprise') !== false) {
26+
$client = new $client(
27+
self::EXAMPLE_CUSTOMER_ID, self::EXAMPLE_API_KEY, self::EXAMPLE_REST_ENDPOINT, 10, null, $mock
28+
);
29+
} else {
30+
$client = new $client(
31+
self::EXAMPLE_CUSTOMER_ID, self::EXAMPLE_API_KEY, self::EXAMPLE_REST_ENDPOINT, "php_telesign", null, null, 10, null, $mock
32+
);
33+
}
2834
$client->$method(...$args);
2935
$request = $mock->getLastRequest();
3036

test/Example.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
class Example {
66

77
const CUSTOMER_ID = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
8-
const API_KEY = "EXAMPLE----TE8sTgg45yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
8+
const API_KEY = "ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
9+
const AUTH_BASIC_STRING = "Basic RkZGRkZGRkYtRUVFRS1ERERELTEyMzQtQUIxMjM0NTY3ODkwOkFCQzEyMzQ1eXVzdW1vTjZCWXNCVmtoK3lSSjVjemdzbkNlaFphT1lsZFBKZG1GaDZOZVg4a3VuWjJ6VTFZV2FVdy8wd1Y2eGZ3PT0=";
910
const REST_ENDPOINT = "https://www.example.com";
1011
const PHONE_NUMBER = "13103409700";
1112
const UCID = "OTHR";

test/rest/RestClientTest.php

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,25 @@ final class RestClientTest extends TestCase {
2626
* @dataProvider getRequestExamples
2727
*/
2828
function testTelesignHeadersMatchExample ($data) {
29+
$auth_method = $data["auth_method"] ?? "HMAC-SHA256";
2930
$actual_headers = RestClient::generateTelesignHeaders(
3031
self::EXAMPLE_CUSTOMER_ID,
3132
self::EXAMPLE_API_KEY,
3233
$data["method_name"],
3334
self::EXAMPLE_RESOURCE,
3435
self::EXAMPLE_URL_ENCODED_FIELDS,
3536
self::EXAMPLE_DATE,
36-
self::EXAMPLE_NONCE
37+
self::EXAMPLE_NONCE,
38+
null,
39+
$data["request"]["headers"]["content-type"],
40+
$auth_method
3741
);
3842

3943
$expected_headers = [
4044
"Authorization" => $data["request"]["headers"]["authorization"],
4145
"Date" => self::EXAMPLE_DATE,
4246
"Content-Type" => $data["request"]["headers"]["content-type"],
43-
"x-ts-auth-method" => "HMAC-SHA256",
47+
"x-ts-auth-method" => $auth_method,
4448
"x-ts-nonce" => self::EXAMPLE_NONCE
4549
];
4650

@@ -55,7 +59,7 @@ function getRequestExamples () {
5559
"uri" => self::EXAMPLE_REST_ENDPOINT . self::EXAMPLE_RESOURCE,
5660
"body" => self::EXAMPLE_URL_ENCODED_FIELDS,
5761
"headers" => [
58-
"authorization" => "TSA FFFFFFFF-EEEE-DDDD-1234-AB1234567890:smUGWEeXtN+WT1s/y1Ssp4Q2Acm/ultPxkgl/GjqSsA=",
62+
"authorization" => "TSA FFFFFFFF-EEEE-DDDD-1234-AB1234567890:dzREwjKg3/o02ABsf8itcNiNzzKWM293qOavWhfkkok=",
5963
"content-type" => "application/x-www-form-urlencoded",
6064
]
6165
]
@@ -66,7 +70,7 @@ function getRequestExamples () {
6670
"uri" => self::EXAMPLE_REST_ENDPOINT . self::EXAMPLE_RESOURCE . "?" . self::EXAMPLE_URL_ENCODED_FIELDS,
6771
"body" => "",
6872
"headers" => [
69-
"authorization" => "TSA FFFFFFFF-EEEE-DDDD-1234-AB1234567890:YgzQt6LcuBDSUeTpE4SASXcSAKAm1eL5TWetbxhXJxg=",
73+
"authorization" => "TSA FFFFFFFF-EEEE-DDDD-1234-AB1234567890:rtUnnJ8wPWEq/pxxT5H+Pj78WHicDzkVYP+dIStuKiQ=",
7074
"content-type" => ""
7175
]
7276
]
@@ -77,7 +81,7 @@ function getRequestExamples () {
7781
"uri" => self::EXAMPLE_REST_ENDPOINT . self::EXAMPLE_RESOURCE,
7882
"body" => self::EXAMPLE_URL_ENCODED_FIELDS,
7983
"headers" => [
80-
"authorization" => "TSA FFFFFFFF-EEEE-DDDD-1234-AB1234567890:ccNQP7Tdwsfqx/Sdz/MmZuhFh+z0z/Bj+OcwDhbTT0s=",
84+
"authorization" => "TSA FFFFFFFF-EEEE-DDDD-1234-AB1234567890:U2s4H/VqFVnb/flIk9ynhUn6no+VBi2Jlc4YUh4H08k=",
8185
"content-type" => "application/x-www-form-urlencoded"
8286
]
8387
]
@@ -88,11 +92,23 @@ function getRequestExamples () {
8892
"uri" => self::EXAMPLE_REST_ENDPOINT . self::EXAMPLE_RESOURCE . "?" . self::EXAMPLE_URL_ENCODED_FIELDS,
8993
"body" => "",
9094
"headers" => [
91-
"authorization" => "TSA FFFFFFFF-EEEE-DDDD-1234-AB1234567890:ODT8s51qSdrS2pKbtrKIu76gQJf2h0hDz8nJ5ho0/6w=",
95+
"authorization" => "TSA FFFFFFFF-EEEE-DDDD-1234-AB1234567890:PjtMTR0t1JzTZEuB7GKlOpdxpsyaX4Zy+4MBWnwii4w=",
9296
"content-type" => ""
9397
]
9498
]
95-
]]
99+
]],
100+
[[
101+
"method_name" => "PATCH",
102+
"auth_method" => "Basic",
103+
"request" => [
104+
"uri" => self::EXAMPLE_REST_ENDPOINT . self::EXAMPLE_RESOURCE,
105+
"body" => self::EXAMPLE_FIELDS,
106+
"headers" => [
107+
"authorization" => Example::AUTH_BASIC_STRING,
108+
"content-type" => "application/json"
109+
]
110+
]
111+
]],
96112
];
97113
}
98114

@@ -120,14 +136,14 @@ function testUserAgentMatchesFormat () {
120136
$mock = new MockHandler([ new Response() ]);
121137

122138
$client = new RestClient(
123-
self::EXAMPLE_CUSTOMER_ID, self::EXAMPLE_API_KEY, self::EXAMPLE_REST_ENDPOINT, 10, null, $mock
139+
self::EXAMPLE_CUSTOMER_ID, self::EXAMPLE_API_KEY, self::EXAMPLE_REST_ENDPOINT, "php_telesign", null, null, 10, null, $mock
124140
);
125141
$client->get(self::EXAMPLE_RESOURCE);
126142

127143
$user_agent = $mock->getLastRequest()->getHeader("user-agent")[0];
128144
$php_version = PHP_VERSION;
129145
$guzzle_version = Client::MAJOR_VERSION;
130-
$pattern = "`^TeleSignSDK/php-v?\d.+ PHP/$php_version Guzzle/$guzzle_version$`";
146+
$pattern = "`^TeleSignSDK/php PHP/$php_version Guzzle/$guzzle_version OriginatingSDK/php_telesign SDKVersion/[a-zA-Z0-9.\-_]+$`";
131147

132148
$this->assertRegExp($pattern, $user_agent);
133149
}
@@ -139,10 +155,12 @@ function testSendsRequest ($data) {
139155
$mock = new MockHandler([ new Response() ]);
140156

141157
$client = new RestClient(
142-
self::EXAMPLE_CUSTOMER_ID, self::EXAMPLE_API_KEY, self::EXAMPLE_REST_ENDPOINT, 10, null, $mock
158+
self::EXAMPLE_CUSTOMER_ID, self::EXAMPLE_API_KEY, self::EXAMPLE_REST_ENDPOINT, "php_telesign", null, null, 10, null, $mock
143159
);
160+
$auth_method = $data["auth_method"] ?? "HMAC-SHA256";
161+
$content_type = $data["request"]["headers"]["content-type"];
144162
$client->{$data["method_name"]}(
145-
self::EXAMPLE_RESOURCE, self::EXAMPLE_FIELDS, self::EXAMPLE_DATE, self::EXAMPLE_NONCE
163+
self::EXAMPLE_RESOURCE, self::EXAMPLE_FIELDS, self::EXAMPLE_DATE, self::EXAMPLE_NONCE, $content_type, $auth_method
146164
);
147165

148166
$request = $mock->getLastRequest();
@@ -151,7 +169,14 @@ function testSendsRequest ($data) {
151169
$this->assertEquals($data["request"]["uri"], $request->getUri());
152170
$this->assertTrue($request->hasHeader("authorization"));
153171
$this->assertEquals($data["request"]["headers"]["authorization"], $request->getHeader("authorization")[0]);
154-
$this->assertEquals($data["request"]["body"], $request->getBody());
172+
173+
$bodyContent = $request->getBody();
174+
if ($content_type === "application/json") {
175+
$parsedBody = json_decode($bodyContent, true);
176+
$this->assertEquals($data["request"]["body"], $parsedBody);
177+
} else {
178+
$this->assertEquals($data["request"]["body"], $bodyContent);
179+
}
155180
}
156181

157182
/**
@@ -161,7 +186,7 @@ function testReturnsResponse ($data) {
161186
$mock = new MockHandler([ new Response() ]);
162187

163188
$client = new RestClient(
164-
self::EXAMPLE_CUSTOMER_ID, self::EXAMPLE_API_KEY, self::EXAMPLE_REST_ENDPOINT, 10, null, $mock
189+
self::EXAMPLE_CUSTOMER_ID, self::EXAMPLE_API_KEY, self::EXAMPLE_REST_ENDPOINT, "php_telesign", null, null, 10, null, $mock
165190
);
166191
$response = $client->{$data["method_name"]}(
167192
self::EXAMPLE_RESOURCE, self::EXAMPLE_FIELDS, self::EXAMPLE_DATE, self::EXAMPLE_NONCE

0 commit comments

Comments
 (0)