Skip to content

Commit

Permalink
Merge pull request #6 from newfold-labs/PRESS3-96-default-search-result
Browse files Browse the repository at this point in the history
add hiive token to the default proxy call
  • Loading branch information
amartya-dev authored Aug 9, 2023
2 parents cb022f3 + 3d05f16 commit 728d5dd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
12 changes: 11 additions & 1 deletion includes/RestApi/AISearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,17 @@ public function get_search_result( \WP_REST_Request $request ) {
* @returns \WP_REST_Response|\WP_Error
*/
public function get_default_search_results( \WP_REST_Request $request ) {
$response = AISearchUtil::get_default_search_results();
$hiive_token = HiiveConnection::get_auth_token();

if ( ! $hiive_token ) {
return new \WP_Error(
'rest_forbidden',
__( 'You are not authorized to make this call' ),
array( 'status' => 403 )
);
}

$response = AISearchUtil::get_default_search_results( $hiive_token );

if ( array_key_exists( 'error', $response ) ) {
return new \WP_Error( 'ServerError', $response['error'] );
Expand Down
10 changes: 8 additions & 2 deletions includes/Utils/AISearchUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,29 @@ public static function get_search_results(
/**
* The function to get the default results from the proxy
*
* @param string $hiive_token The Hive token
* @return array
*/
public static function get_default_search_results() {
public static function get_default_search_results( $hiive_token ) {
if (!self::_check_help_capability()) {
return array(
'error' => __('We are unable to process the request at this moment'),
);
}

$response = wp_remote_post(
NFD_AI_SERVICE_BASE,
NFD_AI_SERVICE_BASE . 'defaultResults',
array(
'method' => 'POST',
'headers' => array(
'Content-Type' => 'application/json',
),
'timeout' => 60,
'body' => wp_json_encode(
array(
'hiivetoken' => $hiive_token,
)
),
)
);
if ( wp_remote_retrieve_response_code( $response ) !== 200 ) {
Expand Down

0 comments on commit 728d5dd

Please sign in to comment.