diff --git a/includes/collective-access-api.php b/includes/collective-access-api.php new file mode 100644 index 0000000..1b68303 --- /dev/null +++ b/includes/collective-access-api.php @@ -0,0 +1,130 @@ + $method, + 'timeout' => 30, + 'headers' => array( + 'Accept' => 'application/json', + 'Content-Type' => 'application/json', + ), + ); + + // Make the request + $response = wp_remote_request($url, $args); + + // Check for errors + if (is_wp_error($response)) { + return $response; + } + + // Get the response body + $body = wp_remote_retrieve_body($response); + + // Decode the JSON response + $data = json_decode($body, true); + + // Check if JSON decoding failed + if (json_last_error() !== JSON_ERROR_NONE) { + return new WP_Error('json_decode_error', 'Failed to decode JSON response'); + } + + return $data; +} + +/** + * Get a list of objects from CollectiveAccess + * + * @param array $params Query parameters for filtering results + * @return array|WP_Error List of objects or WP_Error on failure + */ +function carhop_ca_get_objects($params = array()) +{ + return carhop_ca_api_request('objects', $params); +} + +/** + * Get a single object by ID from CollectiveAccess + * + * @param int $object_id The ID of the object to retrieve + * @param array $params Additional query parameters + * @return array|WP_Error Object data or WP_Error on failure + */ +function carhop_ca_get_object($object_id, $params = array()) +{ + return carhop_ca_api_request("objects/{$object_id}", $params); +} + +/** + * Get a list of collections from CollectiveAccess + * + * @param array $params Query parameters for filtering results + * @return array|WP_Error List of collections or WP_Error on failure + */ +function carhop_ca_get_collections($params = array()) +{ + return carhop_ca_api_request('collections', $params); +} + +/** + * Get a single collection by ID from CollectiveAccess + * + * @param int $collection_id The ID of the collection to retrieve + * @param array $params Additional query parameters + * @return array|WP_Error Collection data or WP_Error on failure + */ +function carhop_ca_get_collection($collection_id, $params = array()) +{ + return carhop_ca_api_request("collections/{$collection_id}", $params); +} + +/** + * Example usage function to test the API + */ +function carhop_test_ca_api() +{ + // Test getting objects + $objects = carhop_ca_get_objects([ + 'limit' => 5, + 'page' => 1 + ]); + + if (is_wp_error($objects)) { + error_log('CollectiveAccess API Error: ' . $objects->get_error_message()); + return; + } + + // Log the results + error_log('CollectiveAccess API Test Results: ' . print_r($objects, true)); +} diff --git a/page-templates/collective-access-test.php b/page-templates/collective-access-test.php new file mode 100644 index 0000000..872f9a6 --- /dev/null +++ b/page-templates/collective-access-test.php @@ -0,0 +1,101 @@ + + +
ID: ' . esc_html($object['object_id']) . '
'; + + // Display object type if available + if (isset($object['type'])) { + echo 'Type: ' . esc_html($object['type']) . '
'; + } + + echo 'ID: ' . esc_html($collection['collection_id']) . '
'; + + echo '