APIClient

The Membership 2 APIClient class.

Defined (1)

The class is defined in the following location(s).

/lib/taxamo/Taxamo/Swagger.php  
  1. class APIClient { 
  2.  
  3. public static $POST = "POST"; 
  4. public static $GET = "GET"; 
  5. public static $PUT = "PUT"; 
  6. public static $DELETE = "DELETE"; 
  7.  
  8. /** 
  9. * @param string $apiKey your API key 
  10. * @param string $apiServer the address of the API server 
  11. */ 
  12. function __construct($apiKey, $apiServer) { 
  13. $this->apiKey = $apiKey; 
  14. $this->apiServer = $apiServer; 
  15.  
  16.  
  17. /** 
  18. * @param string $resourcePath path to method endpoint 
  19. * @param string $method method to call 
  20. * @param array $queryParams parameters to be place in query URL 
  21. * @param array $postData parameters to be placed in POST body 
  22. * @param array $headerParams parameters to be place in request header 
  23. * @return mixed 
  24. */ 
  25. public function callAPI($resourcePath, $method, $queryParams, $postData,  
  26. $headerParams) { 
  27.  
  28. $headers = array(); 
  29.  
  30. # Allow API key from $headerParams to override default 
  31. $added_api_key = False; 
  32. if ($headerParams != null) { 
  33. foreach ($headerParams as $key => $val) { 
  34. $headers[] = "$key: $val"; 
  35. if ($key == 'token') { 
  36. $added_api_key = True; 
  37. if (! $added_api_key) { 
  38. $headers[] = "Token: " . $this->apiKey; 
  39.  
  40. if (is_object($postData) or is_array($postData)) { 
  41. $postData = json_encode($this->sanitizeForSerialization($postData)); 
  42.  
  43. $url = $this->apiServer . $resourcePath; 
  44.  
  45. $curl = curl_init(); 
  46. curl_setopt($curl, CURLOPT_TIMEOUT, 5); 
  47. // return the result on success, rather than just TRUE 
  48. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
  49. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
  50. curl_setopt($curl, CURLOPT_SSLVERSION, 1); 
  51. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1); 
  52. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); 
  53.  
  54. if (! empty($queryParams)) { 
  55. $url = ($url . '?' . http_build_query($queryParams)); 
  56.  
  57. if ($method == self::$POST) { 
  58. curl_setopt($curl, CURLOPT_POST, true); 
  59. curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); 
  60. } else if ($method == self::$PUT) { 
  61. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); 
  62. curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); 
  63. } else if ($method == self::$DELETE) { 
  64. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); 
  65. curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); 
  66. } else if ($method != self::$GET) { 
  67. throw new Exception('Method ' . $method . ' is not recognized.'); 
  68. curl_setopt($curl, CURLOPT_URL, $url); 
  69.  
  70. // Make the request 
  71. $response = curl_exec($curl); 
  72. $response_info = curl_getinfo($curl); 
  73.  
  74. // Handle the response 
  75. if ($response_info['http_code'] == 0) { 
  76. throw new TaxamoAPIException("TIMEOUT: api call to " . $url . 
  77. " took more than 5s to return",  
  78. $postData,  
  79. $response_info); 
  80. } else if ($response_info['http_code'] == 200) { 
  81. $data = json_decode($response); 
  82. } else if ($response_info['http_code'] == 401) { 
  83. throw new TaxamoAuthenticationException("Unauthorized API request to ".$url.": ".$response,  
  84. $postData,  
  85. $response); 
  86. } else if ($response_info['http_code'] == 400) { 
  87. try { 
  88. $data = json_decode($response); 
  89. } catch (Exception $e) { 
  90. throw new TaxamoValidationException("Validation error for " . $url . 
  91. ": ".$response."post data:".$postData,  
  92. $postData,  
  93. $response); 
  94. if (isset($data->validation_failures)) { 
  95. throw new TaxamoValidationException("Validation error for " . $url,  
  96. $postData,  
  97. $response,  
  98. $data->errors,  
  99. $data->validation_failures); 
  100. } else { 
  101. var_dump($data->errors); 
  102. throw new TaxamoValidationException("Validation error for " . $url . 
  103. ": ".$response."post data:".$postData,  
  104. $postData,  
  105. $response,  
  106. $data->errors); 
  107.  
  108. } else if ($response_info['http_code'] == 404) { 
  109. $data = null; 
  110. } else { 
  111. throw new TaxamoAPIException("Can't connect to the api: " . $url . 
  112. " response code: " . $response_info['http_code'],  
  113. $postData,  
  114. $response); 
  115.  
  116. return $data; 
  117.  
  118. /** 
  119. * Build a JSON POST object 
  120. */ 
  121. protected function sanitizeForSerialization($data) 
  122. if (is_scalar($data) || null === $data) { 
  123. $sanitized = $data; 
  124. } else if ($data instanceof \DateTime) { 
  125. $sanitized = $data->format(\DateTime::ISO8601); 
  126. } else if (is_array($data)) { 
  127. foreach ($data as $property => $value) { 
  128. if ($value === null) { 
  129. unset($data[$property]); 
  130. } else { 
  131. $data[$property] = $this->sanitizeForSerialization($value); 
  132. $sanitized = $data; 
  133. } else if (is_object($data)) { 
  134. $values = array(); 
  135. foreach (array_keys($data::$swaggerTypes) as $property) { 
  136. if ($data->$property !== null) { 
  137. $values[$property] = $this->sanitizeForSerialization($data->$property); 
  138. $sanitized = $values; 
  139. } else { 
  140. $sanitized = (string)$data; 
  141.  
  142. return $sanitized; 
  143.  
  144. /** 
  145. * Take value and turn it into a string suitable for inclusion in 
  146. * the path, by url-encoding. 
  147. * @param string $value a string which will be part of the path 
  148. * @return string the serialized object 
  149. */ 
  150. public static function toPathValue($value) { 
  151. return rawurlencode($value); 
  152.  
  153. /** 
  154. * Take value and turn it into a string suitable for inclusion in 
  155. * the query, by imploding comma-separated if it's an object. 
  156. * If it's a string, pass through unchanged. It will be url-encoded 
  157. * later. 
  158. * @param object $object an object to be serialized to a string 
  159. * @return string the serialized object 
  160. */ 
  161. public static function toQueryValue($object) { 
  162. if (is_array($object)) { 
  163. return implode(', ', $object); 
  164. } else { 
  165. return $object; 
  166.  
  167. /** 
  168. * Just pass through the header value for now. Placeholder in case we 
  169. * find out we need to do something with header values. 
  170. * @param string $value a string which will be part of the header 
  171. * @return string the header string 
  172. */ 
  173. public static function toHeaderValue($value) { 
  174. return $value; 
  175.  
  176. /** 
  177. * Deserialize a JSON string into an object 
  178. * @param object $object object or primitive to be deserialized 
  179. * @param string $class class name is passed as a string 
  180. * @return object an instance of $class 
  181. */ 
  182.  
  183. public static function deserialize($data, $class) 
  184. if ($class == 'number') { 
  185. $class = 'float'; 
  186. if (null === $data) { 
  187. $deserialized = null; 
  188. } else if (substr($class, 0, 6) == 'array[') { 
  189. $subClass = substr($class, 6, -1); 
  190. $values = array(); 
  191. foreach ($data as $value) { 
  192. $values[] = self::deserialize($value, $subClass); 
  193. $deserialized = $values; 
  194. } elseif ($class == 'DateTime') { 
  195. $deserialized = new \DateTime($data); 
  196. } elseif (in_array($class, array('string', 'int', 'float', 'bool'))) { 
  197. settype($data, $class); 
  198. $deserialized = $data; 
  199. } else { 
  200. $instance = new $class(); 
  201. foreach ($instance::$swaggerTypes as $property => $type) { 
  202. if (isset($data->$property)) { 
  203. $instance->$property = self::deserialize($data->$property, $type); 
  204. $deserialized = $instance; 
  205.  
  206. return $deserialized; 
  207.