|
| 1 | +# URI Customization Interface |
| 2 | + |
| 3 | +The DCAT extension provides an interface (`IDCATURIGenerator`) that allows other plugins to customize how URIs are generated for RDF serializations. This is useful when you need to: |
| 4 | + |
| 5 | +- Use custom URI patterns for dataset, resources, and organizations. This is useful when you have decoupled frontend running in different domains or need to follow specific URI schemes. |
| 6 | +- Integrate with external authority systems |
| 7 | +- Use persistent identifiers like DOIs or handles etc. |
| 8 | + |
| 9 | +## Interface Methods |
| 10 | + |
| 11 | +The `IDCATURIGenerator` interface provides four methods that can be implemented: |
| 12 | + |
| 13 | +### `catalog_uri(default_uri)` |
| 14 | + |
| 15 | +Customize the catalog URI used to identify the entire CKAN instance. |
| 16 | + |
| 17 | +**Parameters:** |
| 18 | +- `default_uri` (string): The default catalog URI generated by the system |
| 19 | + |
| 20 | +**Returns:** |
| 21 | +- `string` or `None`: The catalog URI to use, or None to use the default |
| 22 | + |
| 23 | +**Example:** |
| 24 | +```python |
| 25 | +def catalog_uri(self, default_uri): |
| 26 | + custom_domain = toolkit.config.get('ckanext.my_extension.catalog_domain') |
| 27 | + if custom_domain: |
| 28 | + return f"https://{custom_domain}/{default_uri}" |
| 29 | + return None |
| 30 | +``` |
| 31 | + |
| 32 | +### `dataset_uri(dataset_dict, default_uri)` |
| 33 | + |
| 34 | +Customize the dataset URI used to identify individual datasets. |
| 35 | + |
| 36 | +**Parameters:** |
| 37 | +- `dataset_dict` (dict): The dataset dictionary containing metadata |
| 38 | +- `default_uri` (string): The default dataset URI generated by the system |
| 39 | + |
| 40 | +**Returns:** |
| 41 | +- `string` or `None`: The dataset URI to use, or None to use the default |
| 42 | + |
| 43 | +**Example:** |
| 44 | +```python |
| 45 | +def dataset_uri(self, dataset_dict, default_uri): |
| 46 | + # Use DOI if available |
| 47 | + doi = dataset_dict.get('doi') |
| 48 | + if doi: |
| 49 | + return f"https://doi.org/{doi}" |
| 50 | + return None # Use default |
| 51 | +``` |
| 52 | + |
| 53 | +### `resource_uri(resource_dict, default_uri)` |
| 54 | + |
| 55 | +Customize the resource URI used to identify individual resources. |
| 56 | + |
| 57 | +**Parameters:** |
| 58 | +- `resource_dict` (dict): The resource dictionary containing metadata |
| 59 | +- `default_uri` (string): The default resource URI generated by the system |
| 60 | + |
| 61 | +**Returns:** |
| 62 | +- `string` or `None`: The resource URI to use, or None to use the default |
| 63 | + |
| 64 | +**Example:** |
| 65 | +```python |
| 66 | +def resource_uri(self, resource_dict, default_uri): |
| 67 | + # Use the actual URL for API resources |
| 68 | + resource_format = resource_dict.get('format', '').lower() |
| 69 | + if resource_format == 'api': |
| 70 | + return resource_dict.get('url') |
| 71 | + return None # Use default |
| 72 | +``` |
| 73 | + |
| 74 | +### `publisher_uri(dataset_dict, default_uri)` |
| 75 | + |
| 76 | +Customize the publisher URI used to identify the organization that published the dataset. |
| 77 | + |
| 78 | +**Parameters:** |
| 79 | +- `dataset_dict` (dict): The dataset dictionary containing metadata |
| 80 | +- `default_uri` (string): The default publisher URI generated by the system |
| 81 | + |
| 82 | +**Returns:** |
| 83 | +- `string` or `None`: The publisher URI to use, or None to use the default |
| 84 | + |
| 85 | +**Example:** |
| 86 | +```python |
| 87 | +def publisher_uri(self, dataset_dict, default_uri): |
| 88 | + organization = dataset_dict.get('organization', {}) |
| 89 | + # Use ROR ID if available |
| 90 | + ror_id = organization.get('ror_id') |
| 91 | + if ror_id: |
| 92 | + return f"https://ror.org/{ror_id}" |
| 93 | + return None # Use default |
| 94 | +``` |
| 95 | + |
| 96 | +## Implementation Example |
| 97 | + |
| 98 | +Here's a complete example of a plugin that implements the `IDCATURIGenerator` interface: |
| 99 | + |
| 100 | +```python |
| 101 | +import ckan.plugins as plugins |
| 102 | +import ckan.plugins.toolkit as toolkit |
| 103 | +from ckanext.dcat.interfaces import IDCATURIGenerator |
| 104 | + |
| 105 | +class MyURIPlugin(plugins.SingletonPlugin): |
| 106 | + plugins.implements(plugins.IConfigurer) |
| 107 | + plugins.implements(IDCATURIGenerator) |
| 108 | + |
| 109 | + # IConfigurer |
| 110 | + def update_config(self, config_): |
| 111 | + pass |
| 112 | + |
| 113 | + # IDCATURIGenerator |
| 114 | + def catalog_uri(self, default_uri): |
| 115 | + # Add version to catalog URI |
| 116 | + version = toolkit.config.get('my_extension.catalog_version', 'v1') |
| 117 | + if default_uri: |
| 118 | + return f"{default_uri.rstrip('/')}/{version}" |
| 119 | + return None |
| 120 | + |
| 121 | + def dataset_uri(self, dataset_dict, default_uri): |
| 122 | + # Use custom identifier if available |
| 123 | + custom_id = dataset_dict.get('custom_identifier') |
| 124 | + if custom_id: |
| 125 | + base_uri = toolkit.config.get('ckanext.dcat.base_uri') |
| 126 | + return f"{base_uri}/data/{custom_id}" |
| 127 | + return None |
| 128 | + |
| 129 | + def resource_uri(self, resource_dict, default_uri): |
| 130 | + # Use content hash for stable URIs |
| 131 | + content_hash = resource_dict.get('content_hash') |
| 132 | + if content_hash: |
| 133 | + base_uri = toolkit.config.get('ckanext.dcat.base_uri') |
| 134 | + return f"{base_uri}/resource/{content_hash}" |
| 135 | + return None |
| 136 | + |
| 137 | + def publisher_uri(self, dataset_dict, default_uri): |
| 138 | + organization = dataset_dict.get('organization', {}) |
| 139 | + # Use external authority URI |
| 140 | + authority_id = organization.get('authority_id') |
| 141 | + if authority_id: |
| 142 | + return f"https://authority.example.org/org/{authority_id}" |
| 143 | + return None |
| 144 | +``` |
| 145 | + |
| 146 | +## Configuration |
| 147 | + |
| 148 | +You can add configuration options to your plugin to make URI generation configurable: |
| 149 | + |
| 150 | +```ini |
| 151 | +# In your CKAN configuration file |
| 152 | +my_extension.catalog_version = v2 |
| 153 | +my_extension.use_dois = true |
| 154 | +my_extension.authority_base_uri = https://authority.example.org |
| 155 | +``` |
| 156 | + |
| 157 | +## Multiple Plugins |
| 158 | + |
| 159 | +If multiple plugins implement the `IDCATURIGenerator` interface, only the first plugin that returns a non-None value will be used for each URI type. Plugins are called in the order they are loaded. |
| 160 | + |
| 161 | +## Backward Compatibility |
| 162 | + |
| 163 | +This interface is completely backward compatible. Existing installations will continue to work unchanged, and the interface only affects URI generation when plugins explicitly implement it. |
0 commit comments