Skip to content

Commit

Permalink
New class has been created for the API part
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmolinacano committed Jul 13, 2023
1 parent c76bb7d commit 13a4e46
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 36 deletions.
33 changes: 25 additions & 8 deletions doofinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ class Doofinder extends Module
const DOOMANAGER_URL = 'https://admin.doofinder.com';
const GS_SHORT_DESCRIPTION = 1;
const GS_LONG_DESCRIPTION = 2;
const VERSION = '4.7.0';
const VERSION = '4.6.6';
const YES = 1;
const NO = 0;

public function __construct()
{
$this->name = 'doofinder';
$this->tab = 'search_filter';
$this->version = '4.7.0';
$this->version = '4.6.6';
$this->author = 'Doofinder (http://www.doofinder.com)';
$this->ps_versions_compliancy = ['min' => '1.5', 'max' => _PS_VERSION_];
$this->module_key = 'd1504fe6432199c7f56829be4bd16347';
Expand Down Expand Up @@ -749,12 +749,7 @@ protected function postProcess()
$messages = '';

if ((bool) Tools::isSubmit('submitDoofinderModuleLaunchReindexing')) {
require_once dirname(__FILE__) . '/lib/doofinder_api_items.php';
$api_key = Configuration::get('DF_AI_APIKEY');
$region = Configuration::get('DF_REGION');
$api_key = $region . '-' . $api_key;
$api = new DoofinderApiItems(null, $api_key, $region);
$api->invokeReindexing();
$this->indexApiInvokeReindexing();
}
if (((bool) Tools::isSubmit('submitDoofinderModuleDataFeed')) == true) {
$form_values = array_merge($form_values, $this->getConfigFormValuesDataFeed());
Expand Down Expand Up @@ -1260,6 +1255,28 @@ private function deleteItemsApi($hashid, $type, $payload)
}
}

private function indexApiInvokeReindexing()
{
require_once dirname(__FILE__) . '/lib/doofinder_api_index.php';

$region = Configuration::get('DF_REGION');
$api_key = $region . '-' . Configuration::get('DF_AI_APIKEY');
$api = new DoofinderApiIndex($api_key, $region);
$response = $api->invokeReindexing(Configuration::get('DF_INSTALLATION_ID'), $this->getProcessCallbackUrl());
if (empty($response)) {
$this->debug('Empty response from invoke reindexing');

return;
}
if (!empty($response['errors'])) {
$this->debug(json_encode($response['errors']));

return;
}

Configuration::updateValue('DF_FEED_INDEXED', false);
}

/**
* Perform an API connection test
*
Expand Down
59 changes: 59 additions & 0 deletions lib/doofinder_api_index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the Software License Agreement.
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* You must not modify, adapt or create derivative works of this source code
*
* @author Doofinder
* @copyright Doofinder
* @license GPLv3
*/
require_once _PS_MODULE_DIR_ . 'doofinder/lib/EasyREST.php';

const API_URL = 'https://{region}-admin.doofinder.com';
const API_VERSION = '1';

class DoofinderApiIndex
{
private $api_key;
private $api_url;

public function __construct($api_key, $region)
{
$this->api_key = $api_key;
$this->api_url = str_replace('{region}', $region, API_URL);
}

/**
* Make a request to the API to reprocess all the feeds
*
* @param string $installation_id
* @param string $callback_url
*/
public function invokeReindexing($installation_id, $callback_url = '')
{
$json_data = json_encode(['query' => 'mutation { process_store_feeds(id: "' . $installation_id . '", callback_url: "' . $callback_url . '") { id }}']);

return $this->post(sprintf('%1$s/api/v%2$s/graphql.json', $this->api_url, API_VERSION), $json_data);
}

private function post($url, $payload)
{
$client = new EasyREST();

$response = $client->post(
$url,
$payload,
false,
false,
'application/json',
['Authorization: Token ' . $this->api_key]
);

return json_decode($response->response, true);
}
}
28 changes: 0 additions & 28 deletions lib/doofinder_api_items.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,6 @@ public function deleteBulk($payload)
return $this->post($url, $payload);
}

/**
* Make a request to the API to reprocess all the feeds
*
* @param array items ids
*/
public function invokeReindexing()
{
$installation_id = Configuration::get('DF_INSTALLATION_ID');
$json_data = json_encode(['query' => 'mutation { process_store_feeds(id: "' . $installation_id . '", callback_url: "' . $this->getProcessCallbackUrl() . '") { id }}']);
$response = $this->post($this->api_url . '/api/v1/graphql.json', $json_data);
if (empty($response)) {
return;
}
if (empty($response_array['errors'])) {
Configuration::updateValue('DF_FEED_INDEXED', false);
}
}

private function post($url, $payload)
{
$client = new EasyREST();
Expand All @@ -93,14 +75,4 @@ private function post($url, $payload)

return json_decode($response->response, true);
}

/**
* Get Process Callback URL
*
* @return string
*/
private function getProcessCallbackUrl()
{
return Context::getContext()->link->getModuleLink('doofinder', 'callback', []);
}
}

0 comments on commit 13a4e46

Please sign in to comment.