WordPress_API_Handler

The WooCommerce Naguro Connect WordPress API Handler class.

Defined (1)

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

/includes/wordpress/class-api-handler.php  
  1. class WordPress_API_Handler implements Naguro_API_Handler { 
  2. /** @var string */ 
  3. private $activation_key; 
  4.  
  5. /** @var string */ 
  6. private $api_url; 
  7.  
  8. /** @var mixed */ 
  9. private $data; 
  10.  
  11. public function __construct() { 
  12. $this->activation_key = get_option( 'naguro_activation_key' ); 
  13. $this->api_url = apply_filters( 'wc_naguro_api_endpoint_url', 'https://api.naguro.com/api/v1/' ); 
  14.  
  15. public function handle_request( $endpoint, $params = array(), $type = 'post', $request_params = array() ) { 
  16. $params = array_merge( $request_params, $this->setup_parameters( $params ) ); 
  17. $url = $this->generate_api_endpoint_url( $endpoint ); 
  18.  
  19. if ( 'get' == $type ) { 
  20. $data = $this->get_request( $url, $params ); 
  21. } else { 
  22. $data = $this->post_request( $url, $params ); 
  23.  
  24. $this->data = $data; 
  25.  
  26. public function is_error() { 
  27. return is_wp_error( $this->data ); 
  28.  
  29. public function get_data() { 
  30. return $this->data; 
  31.  
  32. private function generate_api_endpoint_url( $endpoint ) { 
  33. return trailingslashit( trailingslashit( $this->api_url ) . $endpoint ); 
  34.  
  35. private function setup_parameters( $params ) { 
  36. $params = $this->transform_params_to_body_param( $params ); 
  37. return $this->add_auth_token_to_header( $params ); 
  38.  
  39. private function add_auth_token_to_header( $params ) { 
  40. $params['headers']['X-Auth-Token'] = $this->activation_key; 
  41. return $params; 
  42.  
  43. private function transform_params_to_body_param( $params ) { 
  44. return array( 'body' => $params ); 
  45.  
  46. private function get_request( $url, $params ) { 
  47. return wp_remote_get( $url, $params ); 
  48.  
  49. private function post_request( $url, $params ) { 
  50. return wp_remote_post( $url, $params );