Skip to content

Commit

Permalink
Update BaseHandler.php
Browse files Browse the repository at this point in the history
  • Loading branch information
girafffee committed May 10, 2021
1 parent 3a8552f commit 58e6d0b
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/BaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ abstract public function ajax_action(): string;

abstract public function get_all_data();

public function get_api_request_args() {
return array();
}

public function get_api_data() {
if ( empty( $_REQUEST['api_key'] ) ) {
wp_send_json_error();
Expand All @@ -47,10 +51,16 @@ public function get_api_data() {
$this->api_key( sanitize_text_field( $_REQUEST['api_key'] ) );
$this->filter_result();
} catch ( ApiHandlerException $exception ) {
wp_send_json_error();
wp_send_json_error( $this->parse_exception( $exception ) );
}
}

protected function parse_exception( ApiHandlerException $exception ) {
return array_merge( array(
'message' => $exception->getMessage()
), $exception->getAdditional() );
}

public function filter_result() {
$data = $this->get_all_data();

Expand All @@ -62,7 +72,9 @@ public function filter_result() {
}

public function request( $end_point, $request_args = array() ) {
$args = array_merge_recursive( $this->api_request_args, $request_args );
$args = $this->get_api_request_args();

$args = array_merge_recursive( $args, $request_args );
$response = wp_remote_request( $this->api_base_url . $end_point, $args );

if ( ! $response || is_wp_error( $response ) ) {
Expand Down

0 comments on commit 58e6d0b

Please sign in to comment.