Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: corrects JSON encoding for various data types #118

Merged
merged 6 commits into from Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 17 additions & 19 deletions lib/EasyPost/Requestor.php
Expand Up @@ -55,11 +55,11 @@ public static function utf8($value)
*/
private static function _encodeObjects($data)
{
if (!$data) {
$data = array();
}

if ($data instanceof EasypostResource) {
if ($data === "") {
return null;
Justintime50 marked this conversation as resolved.
Show resolved Hide resolved
} elseif (is_null($data)) {
return array();
} elseif ($data instanceof EasypostResource) {
return array("id" => self::utf8($data->id));
} elseif ($data === true) {
return 'true';
Expand All @@ -68,12 +68,16 @@ private static function _encodeObjects($data)
} elseif (is_array($data)) {
$resource = array();
foreach ($data as $k => $v) {
$resource[$k] = self::_encodeObjects($v);
if (empty($data)) {
return null;
} elseif (!is_null($v)) {
Justintime50 marked this conversation as resolved.
Show resolved Hide resolved
$resource[$k] = self::_encodeObjects($v);
}
}

return array_filter($resource);
return $resource;
} else {
return self::utf8($data);
return self::utf8(strval($data));
}
}

Expand Down Expand Up @@ -197,24 +201,18 @@ private function _curlRequest($method, $absUrl, $headers, $params, $myApiKey)
// Setup the HTTP method and params to use on the request
if ($method == 'get') {
$curlOptions[CURLOPT_HTTPGET] = 1;
if (count($params) > 0) {
$urlParams = self::_urlEncode($params);
$absUrl = "$absUrl?$urlParams";
}
$urlParams = self::_urlEncode($params);
$absUrl = "$absUrl?$urlParams";
Justintime50 marked this conversation as resolved.
Show resolved Hide resolved
} elseif ($method == 'post') {
$curlOptions[CURLOPT_POST] = 1;
$curlOptions[CURLOPT_POSTFIELDS] = json_encode($params);
} elseif ($method == 'patch' || $method == 'put') {
$curlOptions[CURLOPT_CUSTOMREQUEST] = strtoupper($method);
if (count($params) > 0) {
$curlOptions[CURLOPT_POSTFIELDS] = json_encode($params);
}
$curlOptions[CURLOPT_POSTFIELDS] = json_encode($params);
Justintime50 marked this conversation as resolved.
Show resolved Hide resolved
} elseif ($method == 'delete') {
$curlOptions[CURLOPT_CUSTOMREQUEST] = strtoupper($method);
if (count($params) > 0) {
$urlParams = self::_urlEncode($params);
$absUrl = "$absUrl?$urlParams";
}
$urlParams = self::_urlEncode($params);
$absUrl = "$absUrl?$urlParams";
Justintime50 marked this conversation as resolved.
Show resolved Hide resolved
} else {
throw new Error("Unrecognized method {$method}");
}
Expand Down
28 changes: 28 additions & 0 deletions test/EasyPost/Test/AddressTest.php
Expand Up @@ -74,4 +74,32 @@ public function testRetrieve(Address $address)
$this->assertEquals($retrieved_address->id, $address->id);
$this->assertEquals($retrieved_address, $address);
}

/**
* Test the creation of a verified address
* We purposefully pass in slightly incorrect data to get the corrected address back once verified
*
* @return void
*/
public function testCreateVerify()
{
VCR::insertCassette('addresses/createVerify.yml');

$address = Address::create(array(
"verify" => array(true),
"street1" => "417 montgomery streat",
"street2" => "FL 5",
"city" => "San Francisco",
"state" => "CA",
"zip" => "94104",
"country" => "US",
"company" => "EasyPost",
"phone" => "415-123-4567"
));

$this->assertInstanceOf('\EasyPost\Address', $address);
$this->assertIsString($address->id);
$this->assertStringMatchesFormat('adr_%s', $address->id);
$this->assertEquals($address->street1, '417 MONTGOMERY ST STE 500');
}
}
4 changes: 2 additions & 2 deletions test/EasyPost/Test/ParcelTest.php
Expand Up @@ -44,13 +44,13 @@ public function testCreate()
"length" => "10",
"width" => "8",
"height" => "4",
"weight" => "15",
"weight" => 15.4,
));

$this->assertInstanceOf('\EasyPost\Parcel', $parcel);
$this->assertIsString($parcel->id);
$this->assertStringMatchesFormat('prcl_%s', $parcel->id);
$this->assertEquals($parcel->weight, '15');
$this->assertEquals($parcel->weight, 15.4);

// Return so the `retrieve` test can reuse this object
return $parcel;
Expand Down
14 changes: 9 additions & 5 deletions test/EasyPost/Test/ShipmentTest.php
Expand Up @@ -81,14 +81,18 @@ public function testCreate()
),
),
"options" => array(
"label_format" => "PDF",
"label_format" => "PDF",
"invoice_number" => 123 // Tests that we encode integers to strings where appropriate
),
"reference" => 123 // Tests that we encode integers to strings where appropriate
));

$this->assertInstanceOf('\EasyPost\Shipment', $shipment);
$this->assertIsString($shipment->id);
$this->assertStringMatchesFormat('shp_%s', $shipment->id);
$this->assertEquals($shipment->options->label_format, 'PDF');
$this->assertEquals($shipment->options->invoice_number, '123');
$this->assertEquals($shipment->reference, '123');

// Return so the `retrieve` test can reuse this object
return $shipment;
Expand Down Expand Up @@ -172,11 +176,11 @@ public function testSmartrate()
$this->assertEquals($shipment->rates[0]['id'], $smartrates[0]['id']);
$this->assertEquals($smartrates[0]['time_in_transit']['percentile_50'], 1);
$this->assertEquals($smartrates[0]['time_in_transit']['percentile_75'], 1);
$this->assertEquals($smartrates[0]['time_in_transit']['percentile_85'], 1);
$this->assertEquals($smartrates[0]['time_in_transit']['percentile_85'], 2);
$this->assertEquals($smartrates[0]['time_in_transit']['percentile_90'], 2);
$this->assertEquals($smartrates[0]['time_in_transit']['percentile_95'], 2);
$this->assertEquals($smartrates[0]['time_in_transit']['percentile_97'], 3);
$this->assertEquals($smartrates[0]['time_in_transit']['percentile_99'], 6);
$this->assertEquals($smartrates[0]['time_in_transit']['percentile_99'], 3);
}

/**
Expand Down Expand Up @@ -220,7 +224,7 @@ public function testCreateEmptyObjects()
$this->assertIsString($shipment->id);
$this->assertStringMatchesFormat('shp_%s', $shipment->id);
$this->assertNotEmpty($shipment->options); // The EasyPost API populates some default values here
$this->assertNull($shipment->customs_info);
$this->assertEmpty($shipment->customs_info->customs_items);
}

/**
Expand Down Expand Up @@ -266,6 +270,6 @@ public function testCreateTaxIdentifiers()
$this->assertInstanceOf('\EasyPost\Shipment', $shipment);
$this->assertIsString($shipment->id);
$this->assertStringMatchesFormat('shp_%s', $shipment->id);
$this->assertNotEmpty($shipment->tax_identifiers);
$this->assertEquals($shipment->tax_identifiers[0]['tax_id_type'], 'IOSS');
}
}
80 changes: 80 additions & 0 deletions test/cassettes/addresses/createVerify.yml
@@ -0,0 +1,80 @@

-
request:
method: POST
url: 'https://api.easypost.com/v2/addresses?verify%5B0%5D=1'
headers:
Host: api.easypost.com
Accept: application/json
Authorization: ''
Content-Type: application/json
User-Agent: ''
X-Client-User-Agent: ''
EasyPost-Version: '2'
body: '{"address":{"street1":"417 montgomery streat","street2":"FL 5","city":"San Francisco","state":"CA","zip":"94104","country":"US","company":"EasyPost","phone":"415-123-4567"}}'
response:
status:
http_version: '1.1'
code: '201'
message: Created
headers:
x-frame-options: SAMEORIGIN
x-xss-protection: '1; mode=block'
x-content-type-options: nosniff
x-download-options: noopen
x-permitted-cross-domain-policies: none
referrer-policy: strict-origin-when-cross-origin
x-ep-request-uuid: af5516ab616e010fe7995cdc0016d634
cache-control: 'no-cache, no-store'
pragma: no-cache
expires: '0'
location: /api/v2/addresses/adr_c88caa95c20d494b96c1bc2426e3c7a8
content-type: 'application/json; charset=utf-8'
content-length: '632'
etag: 'W/"b40b1f29ab4d1494f4f905ce39b92410"'
x-request-id: b9d87d75-20fb-433f-9202-eaca29cefe36
x-runtime: '0.062946'
x-node: bigweb8nuq
x-version-label: easypost-202110181945-721ecffd7e-master
x-backend: easypost
x-proxied: ['intlb1nuq d40607e4ab', 'extlb1nuq d40607e4ab']
strict-transport-security: 'max-age=31536000; includeSubDomains; preload'
body: '{"id":"adr_c88caa95c20d494b96c1bc2426e3c7a8","object":"Address","created_at":"2021-10-18T23:19:43+00:00","updated_at":"2021-10-18T23:19:43+00:00","name":null,"company":"EASYPOST","street1":"417 MONTGOMERY ST STE 500","street2":"","city":"SAN FRANCISCO","state":"CA","zip":"94104-1100","country":"US","phone":"4151234567","email":null,"mode":"test","carrier_facility":null,"residential":false,"federal_tax_id":null,"state_tax_id":null,"verifications":{"zip4":{"success":true,"errors":[],"details":null},"delivery":{"success":true,"errors":[],"details":{"latitude":37.79342,"longitude":-122.40288,"time_zone":"America/Los_Angeles"}}}}'
curl_info:
url: 'https://api.easypost.com/v2/addresses?verify%5B0%5D=1'
content_type: 'application/json; charset=utf-8'
http_code: 201
header_size: 845
request_size: 705
filetime: -1
ssl_verify_result: 0
redirect_count: 0
total_time: 0.259213
namelookup_time: 0.009251
connect_time: 0.065975
pretransfer_time: 0.137607
size_upload: !!float 173
size_download: !!float 632
speed_download: !!float 2438
speed_upload: !!float 667
download_content_length: !!float 632
upload_content_length: !!float 173
starttransfer_time: 0.259129
redirect_time: !!float 0
redirect_url: ''
primary_ip: 169.62.110.131
certinfo: { }
primary_port: 443
local_ip: 10.130.6.11
local_port: 61512
http_version: 2
protocol: 2
ssl_verifyresult: 0
scheme: HTTPS
appconnect_time_us: 137541
connect_time_us: 65975
namelookup_time_us: 9251
pretransfer_time_us: 137607
redirect_time_us: 0
starttransfer_time_us: 259129
total_time_us: 259213
56 changes: 28 additions & 28 deletions test/cassettes/parcels/create.yml
Expand Up @@ -8,10 +8,10 @@
Accept: application/json
Authorization: ''
Content-Type: application/json
User-Agent: 'EasyPost/v2 PhpClient/3.6.0'
User-Agent: ''
X-Client-User-Agent: ''
EasyPost-Version: '2'
body: '{"parcel":{"length":"10","width":"8","height":"4","weight":"15"}}'
body: '{"parcel":{"length":"10","width":"8","height":"4","weight":"15.4"}}'
response:
status:
http_version: '1.1'
Expand All @@ -24,57 +24,57 @@
x-download-options: noopen
x-permitted-cross-domain-policies: none
referrer-policy: strict-origin-when-cross-origin
x-ep-request-uuid: 8d29b69461578bbce78bb42d003aedbd
x-ep-request-uuid: 58cc086e616ef22ce78737e1004fefdc
cache-control: 'no-cache, no-store'
pragma: no-cache
expires: '0'
location: /api/v2/parcels.prcl_055f28d3bd8b4fca91ab64a174612b01
location: /api/v2/parcels.prcl_57f00d7d1cba46e39d07bbd39862f75c
content-type: 'application/json; charset=utf-8'
content-length: '229'
etag: 'W/"0f49dcf52e72851ce4c98ae929ed072f"'
x-request-id: 7292d60a-91ee-45fe-9fce-f5e98d04cea9
x-runtime: '0.026217'
x-node: bigweb4nuq
x-version-label: easypost-202110011858-559f609973-master
etag: 'W/"b0a69e4f7370309827d5601fca09cb8b"'
x-request-id: a355714d-187f-4c5d-acb8-dea013dd14dc
x-runtime: '0.029035'
x-node: bigweb8nuq
x-version-label: easypost-202110181945-721ecffd7e-master
x-backend: easypost
x-proxied: ['intlb2nuq d40607e4ab', 'extlb2nuq d40607e4ab']
strict-transport-security: 'max-age=31536000; includeSubDomains; preload'
body: '{"id":"prcl_055f28d3bd8b4fca91ab64a174612b01","object":"Parcel","created_at":"2021-10-01T22:29:16Z","updated_at":"2021-10-01T22:29:16Z","length":10.0,"width":8.0,"height":4.0,"predefined_package":null,"weight":15.0,"mode":"test"}'
body: '{"id":"prcl_57f00d7d1cba46e39d07bbd39862f75c","object":"Parcel","created_at":"2021-10-19T16:28:28Z","updated_at":"2021-10-19T16:28:28Z","length":10.0,"width":8.0,"height":4.0,"predefined_package":null,"weight":15.4,"mode":"test"}'
curl_info:
url: 'https://api.easypost.com/v2/parcels'
content_type: 'application/json; charset=utf-8'
http_code: 201
header_size: 844
request_size: 582
request_size: 584
filetime: -1
ssl_verify_result: 0
redirect_count: 0
total_time: 0.233404
namelookup_time: 0.001814
connect_time: 0.062673
pretransfer_time: 0.142633
size_upload: !!float 65
total_time: 0.268922
namelookup_time: 0.040134
connect_time: 0.098593
pretransfer_time: 0.180402
size_upload: !!float 67
size_download: !!float 229
speed_download: !!float 981
speed_upload: !!float 278
speed_download: !!float 851
speed_upload: !!float 249
download_content_length: !!float 229
upload_content_length: !!float 65
starttransfer_time: 0.233373
upload_content_length: !!float 67
starttransfer_time: 0.268848
redirect_time: !!float 0
redirect_url: ''
primary_ip: 169.62.110.130
certinfo: { }
primary_port: 443
local_ip: 10.130.6.13
local_port: 59819
local_ip: 10.130.6.8
local_port: 51239
http_version: 2
protocol: 2
ssl_verifyresult: 0
scheme: HTTPS
appconnect_time_us: 142515
connect_time_us: 62673
namelookup_time_us: 1814
pretransfer_time_us: 142633
appconnect_time_us: 180328
connect_time_us: 98593
namelookup_time_us: 40134
pretransfer_time_us: 180402
redirect_time_us: 0
starttransfer_time_us: 233373
total_time_us: 233404
starttransfer_time_us: 268848
total_time_us: 268922