Skip to content

Commit

Permalink
cache marketplace response for a day
Browse files Browse the repository at this point in the history
  • Loading branch information
circlecube committed Jun 16, 2022
1 parent 55a3e8c commit 58a9a45
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
1 change: 0 additions & 1 deletion components/marketplace/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import { default as MarketplaceList } from '../marketplaceList/';
methods.apiFetch( {
url: `${constants.resturl}/newfold-marketplace/v1/marketplace`
}).then( ( response ) => {
console.log( response );
setIsLoading( false );
setMarketplaceItems( response );
setMarketplaceCategories( collectCategories( response ) );
Expand Down
64 changes: 42 additions & 22 deletions includes/MarketplaceApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
*/
class MarketplaceApi {

/**
* Transient name where notifications are stored.
*/
const TRANSIENT = 'newfold_marketplace';

/**
* Register notification routes.
*/
Expand All @@ -25,28 +30,33 @@ public static function registerRoutes() {
'methods' => \WP_REST_Server::READABLE,
'callback' => function ( \WP_REST_Request $request ) {

$marketplace_endpoint = add_query_arg( array(
'brand' => container()->plugin()->id,
), NFD_HIIVE_URL . '/sites/v1/products' );

$response = wp_remote_get(
$marketplace_endpoint,
array(
'headers' => array(
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . HiiveConnection::get_auth_token(),
),
)
);

// return rest_ensure_response( $response );
if ( ! is_wp_error( $response ) ) {
$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body, true );
if ( $data && is_array( $data ) ) {
$marketplace = $data;
// set_transient( self::TRANSIENT, $marketplace, 7 * DAY_IN_SECONDS );
$marketplace = get_transient( self::TRANSIENT );

if ( false === $marketplace ) {

$marketplace_endpoint = add_query_arg( array(
'brand' => container()->plugin()->id,
), NFD_HIIVE_URL . '/sites/v1/products' );

$response = wp_remote_get(
$marketplace_endpoint,
array(
'headers' => array(
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . HiiveConnection::get_auth_token(),
),
)
);

// return rest_ensure_response( $response );
if ( ! is_wp_error( $response ) ) {
$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body, true );
if ( $data && is_array( $data ) ) {
$marketplace = $data;
self::setTransient( $marketplace );
}
}
}
return $marketplace;
Expand All @@ -59,4 +69,14 @@ public static function registerRoutes() {

}

/**
* Set the transient where marketplace is stored.
*
* @param string $data json of marketplace.
* @param float|int $expiration Transient expiration.
*/
public static function setTransient( $data, $expiration = DAY_IN_SECONDS ) {
set_transient( self::TRANSIENT, $data, $expiration );
}

}

0 comments on commit 58a9a45

Please sign in to comment.