WP_API_Handler

The WooCommerce Naguro Connect WP API Handler class.

Defined (1)

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

/includes/woocommerce/core/class-wp-api-handler.php  
  1. class WP_API_Handler { 
  2. /** @var string */ 
  3. private $activation_key; 
  4.  
  5. /** @var string */ 
  6. private $api_url; 
  7.  
  8. public function __construct() { 
  9. // Load API credentials from WC integration and fall back on older options (or false default) 
  10. $options = get_option( 'naguro_settings' ); 
  11. $this->activation_key = get_option( 'naguro_activation_key' ); 
  12. $this->api_url = apply_filters( 'wc_naguro_api_endpoint_url', 'http://api.naguro.com/api/v1/' ); 
  13.  
  14. if ( ! $this->activation_key || ! $this->api_url ) { 
  15. wp_die('No valid API credentials provided...'); 
  16.  
  17. public function handle_request( $endpoint, $params = array(), $type = 'post' ) { 
  18. $params = $this->setup_parameters( $params ); 
  19. $url = $this->generate_api_endpoint_url( $endpoint ); 
  20.  
  21. if ( 'get' == $type ) { 
  22. $data = $this->get_request( $url, $params ); 
  23. } else { 
  24. $data = $this->post_request( $url, $params ); 
  25.  
  26. return $data; 
  27.  
  28. private function generate_api_endpoint_url( $endpoint ) { 
  29. return trailingslashit( trailingslashit( $this->api_url ) . $endpoint ); 
  30.  
  31. private function setup_parameters( $params ) { 
  32. $params = $this->transform_params_to_body_param( $params ); 
  33. return $this->add_auth_token_to_header( $params ); 
  34.  
  35. private function add_auth_token_to_header( $params ) { 
  36. $params['headers']['X-Auth-Token'] = $this->activation_key; 
  37. return $params; 
  38.  
  39. private function transform_params_to_body_param( $params ) { 
  40. return array( 'body' => $params ); 
  41.  
  42. private function get_request( $url, $params ) { 
  43. return wp_remote_get( $url, $params ); 
  44.  
  45. private function post_request( $url, $params ) { 
  46. return wp_remote_post( $url, $params );