Unverified Commit fcf962c6 authored by everykittysdaydream's avatar everykittysdaydream Committed by GitHub
Browse files

Extractor settings (#30)

* extractors now smaller

* decrease setting load

* configure placement of entrypoint

* null checks

* remember we move 10 pages at once

* fix header into entryobject
parent 1f0ea185
......@@ -159,7 +159,9 @@ function civicrm_api3_exporter_Export($params) {
// nextPage.
$config = CRM_Core_Config::singleton();
$paramscopy = $params;
$response["_links"]["self"] = CRM_Utils_System::url("civicrm/osdi/webhook", URLformat($paramscopy), TRUE, NULL, FALSE, TRUE);
$response["_links"]["self"] = array(
"href" => CRM_Utils_System::url("civicrm/osdi/webhook", URLformat($paramscopy), TRUE, NULL, FALSE, TRUE)
);
if (!$singleuser) {
$paramscopy["page"]++;
$nextarray = array();
......
......@@ -7,7 +7,9 @@
require __DIR__ . '/../../../vendor/autoload.php';
use Ekino\HalClient\HttpClient\FileGetContentsHttpClient;
use Ekino\HalClient\Resource;
use GuzzleHttp\Client;
require_once __DIR__ . '/../../../importers/ActionNetworkContactImporter.php';
require_once __DIR__ . '/../../../importers/ResourceStruct.php';
......@@ -26,6 +28,25 @@ require_once __DIR__ . '/../../../importers/ResourceStruct.php';
function _civicrm_api3_importer_Schedule_spec(&$spec) {
}
/**
* given a URL, request the relevant object
*/
function buildRequest($entryobject) {
$raw_client = new Client();
$full_client = new FileGetContentsHttpClient($entryobject["endpoint"], $entryobject["headers"]);
$response = $raw_client->request('GET', $entryobject["endpoint"], [
'headers' => $entryobject["headers"]
]);
// Wrap everything into a hal-client resource so nobody knows I used Guzzle.
$response_string = $response->getBody()->getContents();
$data = json_decode($response_string, TRUE);
$data = Resource::create($full_client, $data);
return $data;
}
/**
* Importer.Schedule API.
*
......@@ -57,6 +78,7 @@ function civicrm_api3_importer_Schedule($params) {
$malformed = FALSE;
if (is_string($to_unserialize)) {
$rootdata = unserialize($to_unserialize);
// actually convert $rootdata into a data point
}
else {
$malformed = TRUE;
......@@ -68,7 +90,7 @@ function civicrm_api3_importer_Schedule($params) {
return civicrm_api3_create_success($returnValues, $params, 'Importer', 'schedule');
}
$root = $rootdata->resource;
$root = buildRequest($rootdata->resource);
$zone = $rootdata->zone;
$apikey = $rootdata->apikey;
......@@ -85,6 +107,9 @@ function civicrm_api3_importer_Schedule($params) {
// Add the relevant contacts that can be added to the queue.
$returnValues["person"] = array();
// for storing the next object
$entryobject = array();
for ($i = 0; $i <= 10; $i++) {
$people = $root->get('osdi:people');
if ($people == NULL) {
......@@ -123,14 +148,20 @@ function civicrm_api3_importer_Schedule($params) {
// ActionNetworkContactImporter::merge_task_with_page($rootdata->rule);.
CRM_Core_Session::setStatus('adding contacts to pipeline', 'Queue task', 'success');
return civicrm_api3_create_success($returnValues, $params, 'Importer', 'schedule');
}
}
}
if (array_key_exists("next", $root->getLinks())) {
if (array_key_exists("href", $root->getLinks()["next"])) {
$entryobject["endpoint"] = $root->getLinks()["next"]["href"];
$entryobject["headers"] = $rootdata->resource["headers"];
}
}
// In this case, we still got stuff to do! so im gonna put it back into the array.
// i throw it into tthe back to prevent starvation in event of multiple extractors.
CRM_Core_Session::setStatus('adding contacts to pipeline', 'Queue task', 'success');
$returned_data = new ResourceStruct($root, $rootdata->rule, $rootdata->filter, $rootdata->group, $zone, $apikey, $rootdata->endpoint);
$returned_data = new ResourceStruct($entryobject, $rootdata->rule, $rootdata->filter, $rootdata->group, $zone, $apikey, $rootdata->endpoint);
$extractors = Civi::settings()->get("extractors");
$extractors[] = serialize($returned_data);
......
......@@ -17,6 +17,7 @@ abstract class AbstractContactImporter {
protected $apikey;
protected $client;
protected $entrypoint;
protected $endpath;
/**
* For raw http requests.
......@@ -43,7 +44,13 @@ abstract class AbstractContactImporter {
$extractors = array();
}
$final_data = new ResourceStruct($resource_root, $rule, $filter, $group, $zone, $this->apikey, $this->endpoint);
$entryobject = array();
$entryobject["endpoint"] = $this->endpoint . $this->endpath;
$entryobject["headers"] = $this->headers;
$final_data = new ResourceStruct($entryobject, $rule, $filter, $group, $zone, $this->apikey, $this->endpoint);
$extractors[] = serialize($final_data);
Civi::settings()->set("extractors", $extractors);
......
......@@ -27,6 +27,11 @@ class ActionNetworkContactImporter extends AbstractContactImporter {
$this->endpoint = $endpoint;
$this->schema = $schema;
$this->apikey = $apikey;
$this->endpath = "/people";
$this->headers = [
'OSDI-API-Token' => $this->apikey,
'Content-Type' => "application/json"
];
// Create a HttpClient to perform http request.
$this->client = new FileGetContentsHttpClient($this->endpoint, array(
......@@ -43,6 +48,22 @@ class ActionNetworkContactImporter extends AbstractContactImporter {
// $this->queue = CRM_OSDIQueue_Helper::singleton()->getQueue();
}
/**
* given a URL, request the relevant object
*/
public function request_object($full_uri) {
$response = $this->raw_client->request('GET', $full_uri, [
'headers' => $this->headers
]);
// Wrap everything into a hal-client resource so nobody knows I used Guzzle.
$response_string = $response->getBody()->getContents();
$data = json_decode($response_string, TRUE);
$data = Resource::create($this->client, $data);
return $data;
}
/**
*
*/
......@@ -52,10 +73,7 @@ class ActionNetworkContactImporter extends AbstractContactImporter {
$full_uri = $this->endpoint . $query_string;
$response = $this->raw_client->request('GET', $full_uri, [
'headers' => [
'OSDI-API-Token' => $this->apikey,
'Content-Type' => "application/json",
],
'headers' => $this->headers
]);
// Wrap everything into a hal-client resource so nobody knows I used Guzzle.
......@@ -64,7 +82,12 @@ class ActionNetworkContactImporter extends AbstractContactImporter {
$data = json_decode($response_string, TRUE);
$data = Resource::create($this->client, $data);
$final_data = new ResourceStruct($data, $rule, $filter, $group, $zone, $this->apikey, "");
$entryobject = array();
$entryobject["endpoint"] = $full_uri;
$entryobject["headers"] = $this->headers;
$final_data = new ResourceStruct($entryobject, $rule, $filter, $group, $zone, $this->apikey, "");
// Shunt the root into the queue.
$extractors = Civi::settings()->get("extractors");
......
......@@ -27,13 +27,15 @@ class CiviCRMContactImporter extends AbstractContactImporter {
$this->endpoint = $endpoint;
$this->schema = $schema;
$this->apikey = $apikey;
// Create a HttpClient to perform http request.
$this->client = new FileGetContentsHttpClient($this->endpoint, array(
$this->endpath = "/osdi/webhook";
$this->headers = [
'OSDI-API-Token' => $this->apikey,
'Object' => 'Contact',
'Content-Type' => "application/hal+json",
));
];
// Create a HttpClient to perform http request.
$this->client = new FileGetContentsHttpClient($this->endpoint, $this->headers);
$this->entrypoint = new EntryPoint('/osdi/webhook', $this->client);
......@@ -44,28 +46,44 @@ class CiviCRMContactImporter extends AbstractContactImporter {
// $this->queue = CRM_OSDIQueue_Helper::singleton()->getQueue();
}
/**
* given a URL, request the relevant object
*/
public function request_object($full_uri) {
$response = $this->raw_client->request('GET', $full_uri, [
'headers' => $this->headers
]);
// Wrap everything into a hal-client resource so nobody knows I used Guzzle.
$response_string = $response->getBody()->getContents();
$data = json_decode($response_string, TRUE);
$data = Resource::create($this->client, $data);
return $data;
}
/**
*
*/
public function update_endpoint_data($date, $filter = NULL, $rule = NULL, $group = -1, $zone = 0) {
// TODO: sanitize this input later.
// The actual heavy lifting is done by the request_object function, but we
// want any errors to throw first.
$query_string = "/osdi/webhook?filter=modified_date gt '" . $date . "'";
$full_uri = $this->endpoint . $query_string;
$response = $this->raw_client->request('GET', $full_uri, [
'headers' => [
'OSDI-API-Token' => $this->apikey,
'Object' => 'Contact',
'Content-Type' => "application/hal+json",
],
'headers' => $this->headers
]);
// Wrap everything into a hal-client resource so nobody knows I used Guzzle.
$response_string = $response->getBody()->getContents();
$data = json_decode($response_string, TRUE);
$data = Resource::create($this->client, $data);
$final_data = new ResourceStruct($data, $rule, $filter, $group, $zone, $this->apikey, $this->endpoint);
$entryobject = array();
$entryobject["endpoint"] = $full_uri;
$entryobject["headers"] = $this->headers;
$final_data = new ResourceStruct($entryobject, $rule, $filter, $group, $zone, $this->apikey, $this->endpoint);
// Shunt the root into the queue.
$extractors = Civi::settings()->get("extractors");
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment