Skip to content

Commit

Permalink
OAI-PMH: implementation of these verbs: Identify, ListMetadataFormats…
Browse files Browse the repository at this point in the history
…, GetRecord.
  • Loading branch information
nedbaldessin committed May 9, 2020
1 parent e1e6fcd commit 63c19d2
Show file tree
Hide file tree
Showing 9 changed files with 428 additions and 26 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@ MOB_NAT_USER_PASSWORD=**********
MOB_NAT_USER_PASSWORD=********

TRUSTED_PROXIES="127.0.0.1,REMOTE_ADDR"

# List of emails of the OAI-PMH repository
# Must be a comma separated string.
OAI_ADMIN_EMAILS="[email protected], [email protected]"
99 changes: 92 additions & 7 deletions app/Console/Commands/ImportUpdatedOn.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@
use GuzzleHttp\Client;
use GuzzleHttp\Psr7;
use GuzzleHttp\Exception\RequestException;
use Seld\Signal\SignalHandler;
// use Seld\Signal\SignalHandler;
use Illuminated\Console\Loggable;

use \App\Models\Product;

class ImportUpdatedOn extends Command
{
use Loggable;


/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'gobelins:importupdatedon';
protected $signature = 'gobelins:importupdatedon {--from= : Start at given page}';

/**
* The console command description.
Expand Down Expand Up @@ -43,19 +49,21 @@ class ImportUpdatedOn extends Command
* Instance of SignalHandler, used to gracefully exit
* when sent a SIGINT (ctrl-c).
*/
protected $signal_handle;
// protected $signal_handle;

/**
* Log of processing error or remarks that should be
* displayed in the terminal after exit;
*/
protected $report = [];


/**
* Any shared HTTP options, like auth…
*/
protected $http_options = [];
protected $http_options = [
'allow_redirects' => true,
];


/**
Expand All @@ -66,6 +74,13 @@ class ImportUpdatedOn extends Command
public function __construct()
{
parent::__construct();

$this->http_options = array_merge($this->http_options, [
'auth' => [
config('app.datasource_username'),
config('app.datasource_password'),
],
]);
}

/**
Expand All @@ -75,6 +90,76 @@ public function __construct()
*/
public function handle()
{
//
// $this->signal_handle = SignalHandler::create();

$this->initHttpClient();
$this->setupProgressBar();
$this->loadData();
}

private function initHttpClient()
{
$this->client = new Client([
'base_uri' => config('app.datasource_baseuri'),
'timeout' => 30.0,
]);
}



private function setupProgressBar()
{
$response = $this->client->request('GET', '/api/products', $this->http_options);
if ($response->getStatusCode() === 200) {
$json_resp = json_decode($response->getBody());
$this->progress_bar = $this->output->createProgressBar($json_resp->meta->total);
$this->progress_bar->advance($json_resp->meta->from - 1);
}
}

private function loadData()
{
$this->logInfo('Starting import from datasource…');

// The root request endpoint.
// All subsequent requests will be crawled from the next page in the response.
// products?page=3033
$next_page = '/api/products';
if ($this->option('from')) {
$this->logInfo('Resuming import from page ' . $this->option('from'));
$next_page .= '?page=' . $this->option('from');
}

do {
try {
$response = $this->client->request('GET', $next_page, $this->http_options);
if ($response->getStatusCode() === 200) {
$json_resp = json_decode($response->getBody());
$this->progress_bar->setProgress($json_resp->meta->from - 1);
$this->current_page = $json_resp->meta->current_page;

collect($json_resp->data)->map(function ($item) {
$this->updateRecord($item);
});

$this->logInfo('Completed import of page ' . $this->current_page);

$next_page = $json_resp->links->next ?: false;
}
} catch (ClientException $e) {
echo Psr7\str($e->getRequest());
echo Psr7\str($e->getResponse());
};
} while ($next_page !== false);
}

private function updateRecord($item)
{
Product::withoutSyncingToSearch(function () use ($item) {
Product::where('inventory_id', $item->inventory_id)
->update(['legacy_updated_on' => $item->updated_on]);
});

$this->progress_bar->advance();
}
}
7 changes: 4 additions & 3 deletions app/Http/Controllers/OaiPmhController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

use Psr\Http\Message\ServerRequestInterface;

use App\Repositories\Product;
use App\Http\OaiPmh\Repository;

class OaiPmhController extends Controller
{
public function index(ServerRequestInterface $request)
{
$repository = new Product();
// $provider = new \Picturae\OaiPmh\Provider($repository);
$repository = new Repository();

$provider = new \Picturae\OaiPmh\Provider($repository, $request);

$response = $provider->getResponse();

return $response;
Expand Down
Loading

0 comments on commit 63c19d2

Please sign in to comment.