From de169dba955a9ce0afa1671fc0436ca21bfc267a Mon Sep 17 00:00:00 2001 From: Antoine Date: Thu, 6 Mar 2025 10:11:59 +0100 Subject: [PATCH] test querying showing collective acess datas --- includes/collective-access-api.php | 130 ++++++++++++++++++++++ page-templates/collective-access-test.php | 101 +++++++++++++++++ 2 files changed, 231 insertions(+) create mode 100644 includes/collective-access-api.php create mode 100644 page-templates/collective-access-test.php 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 @@ + + +
+

Test de l'API CollectiveAccess

+ + 10, + 'page' => 1 + ]); + + if (is_wp_error($objects)) { + echo ''; + } else { + echo '
'; + + foreach ($objects as $object) { + echo '
'; + + // Display object image if available + if (isset($object['representations'][0]['urls']['preview'])) { + echo '' . esc_attr($object['preferred_labels']['name']) . ''; + } + + echo '
'; + + // Display object title + if (isset($object['preferred_labels']['name'])) { + echo '

' . esc_html($object['preferred_labels']['name']) . '

'; + } + + // Display object ID + echo '

ID: ' . esc_html($object['object_id']) . '

'; + + // Display object type if available + if (isset($object['type'])) { + echo '

Type: ' . esc_html($object['type']) . '

'; + } + + echo '
'; // End of card content + echo '
'; // End of card + } + + echo '
'; // End of grid + } + ?> + +
+

Collections

+ 5, + 'page' => 1 + ]); + + if (is_wp_error($collections)) { + echo ''; + } else { + echo '
'; + + foreach ($collections as $collection) { + echo '
'; + + // Display collection name + if (isset($collection['preferred_labels']['name'])) { + echo '

' . esc_html($collection['preferred_labels']['name']) . '

'; + } + + // Display collection ID + echo '

ID: ' . esc_html($collection['collection_id']) . '

'; + + echo '
'; + } + + echo '
'; + } + ?> +
+
+ +