Upload resource template via API


#1

Hi,

I’ve exported a resource template as JSON from one Omeka-S instance, and wanted to upload it to another instance using the REST API. I’ve tried POSTing the JSON to /api/resource_templates/, but while the template is created and correctly labelled, the class and the properties are missing. Any pointers? Presumably I’m missing something obvious…

Thanks, Tim


#2

Could it be that you don’t have the vocabulary that the class and properties belong to installed on the other instance?


#3

No, as far as I can tell the vocabs are identical. My only addition was to install schema.org in both instances.

This is the JSON exported from the first instance:

{
    "o:label": "Newspaper",
    "o:resource_template_property": [
        {
            "o:alternate_label": null,
            "o:alternate_comment": null,
            "o:is_required": false,
            "o:is_private": false,
            "data_type_name": null,
            "data_type_label": null,
            "vocabulary_namespace_uri": "http:\/\/purl.org\/dc\/terms\/",
            "vocabulary_label": "Dublin Core",
            "local_name": "title",
            "label": "Title"
        },
        {
            "o:alternate_label": null,
            "o:alternate_comment": null,
            "o:is_required": false,
            "o:is_private": false,
            "data_type_name": null,
            "data_type_label": null,
            "vocabulary_namespace_uri": "http:\/\/schema.org\/",
            "vocabulary_label": "Schema.org",
            "local_name": "name",
            "label": "name"
        },
        {
            "o:alternate_label": null,
            "o:alternate_comment": null,
            "o:is_required": false,
            "o:is_private": false,
            "data_type_name": null,
            "data_type_label": null,
            "vocabulary_namespace_uri": "http:\/\/purl.org\/dc\/terms\/",
            "vocabulary_label": "Dublin Core",
            "local_name": "description",
            "label": "Description"
        },
        {
            "o:alternate_label": "Trove newspaper id",
            "o:alternate_comment": null,
            "o:is_required": false,
            "o:is_private": false,
            "data_type_name": null,
            "data_type_label": null,
            "vocabulary_namespace_uri": "http:\/\/schema.org\/",
            "vocabulary_label": "Schema.org",
            "local_name": "identifier",
            "label": "identifier"
        },
        {
            "o:alternate_label": null,
            "o:alternate_comment": "Trove url",
            "o:is_required": false,
            "o:is_private": false,
            "data_type_name": "uri",
            "data_type_label": "URI",
            "vocabulary_namespace_uri": "http:\/\/schema.org\/",
            "vocabulary_label": "Schema.org",
            "local_name": "url",
            "label": "url"
        }
    ],
    "o:resource_class": {
        "vocabulary_namespace_uri": "http:\/\/schema.org\/",
        "vocabulary_label": "Schema.org",
        "local_name": "Newspaper",
        "label": "Newspaper"
    }
}

All I’m doing is loading the JSON and submitting via POST:

payload = json.load(open('Newspaper.json', 'r'))
params = {
            'key_identity': 'blahblah',
            'key_credential': 'blahblah'
        }
response = requests.post('https://timsherratt.org/wraggelabs/api/resource_templates/', json=payload, params=params)
print(response.json())

And this is what I get back – template created, but no class or props…

{'@context': 'https://timsherratt.org/wraggelabs/api-context', '@id': 'https://timsherratt.org/wraggelabs/api/resource_templates/8', '@type': 'o:ResourceTemplate', 'o:id': 8, 'o:label': 'Newspaper', 'o:owner': {'@id': 'https://timsherratt.org/wraggelabs/api/users/1', 'o:id': 1}, 'o:resource_class': None, 'o:resource_template_property': []}

#4

Have you tried the export/import feature? (There’s an “Export” button when you’re viewing a template and an “Import” button on the main templates browse page)


#5

Yep. I’m using the export button to create the JSON file. However, I was hoping to set up a Jupyter notebook that would let people do all the config (including importing vocabs and resource templates) via the API rather than the admin interface. But I can’t get it to work…


#6

Ah, okay, I missed that wrinkle.

The resource template API endpoint is looking for data in the structure of the endpoint’s GET output: in particular it’s looking for o:id subkeys on the resorce_template_property and resource_class keys.

The template import/export uses a different process that’s specifically designed for transferring between different installs and therefore doesn’t rely on internal IDs.

This would still be doable as code-only but you’d have to add a step or steps of reading the current properties/classes out of the API to fill in the needed IDs.

We could possibly expose an endpoint or mode in the API itself that accepts the “export” files… I’m not sure off the top of my head how difficult that would be to do.


#7

Ok, thanks – I’ll give that a try. BTW, here’s the notebook as it currently stands: https://nbviewer.jupyter.org/github/GLAM-Workbench/trove-newspapers/blob/master/Upload-Trove-newspapers-to-Omeka.ipynb