Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Figure out a way to get a list of fields for a content type via REST #6

Closed
mjordan opened this issue Jun 30, 2019 · 7 comments
Closed

Comments

@mjordan
Copy link
Owner

mjordan commented Jun 30, 2019

If we can inspect the fields that a content type has configured, we can validate that the columns in the input file match machine field names, and we can also dynamically create requests for string, taxo term, and linked references.

@mjordan
Copy link
Owner Author

mjordan commented Jun 30, 2019

Unfortunately, curl -v "admin:islandora@localhost:8000/entity/node_type/islandora_object" doesn't return a list of fields:

{"uuid":"c720394c-5f28-4290-be53-88bc7de9c7bc","langcode":"en","status":true,"dependencies":{"enforced":{"module":["islandora_defaults"]},"module":["menu_ui"]},"third_party_settings":{"menu_ui":{"available_menus":["main"],"parent":"main:"}},"name":"Repository Item","type":"islandora_object","description":"An item in your Islandora repository.","help":"","new_revision":true,"preview_mode":1,"display_submitted":true}

JSON::API also provides a way to GET config entities, and provides this example:

curl \ --header 'Accept: application/vnd.api+json' \ http://admin:admin@drupal.d8/jsonapi/menu/menu

Can't find any documentation though.

@mjordan
Copy link
Owner Author

mjordan commented Jun 30, 2019

JSON::API to the rescue:

curl --header 'Accept: application/vnd.api+json' -o test2.json http://admin:islandora@localhost:8000/jsonapi/node/islandora_object?page[limit]=1

will return one node of the islandora_object type, with all fields listed and populated. Doesn't give you the field type or UUID, though.

@mjordan
Copy link
Owner Author

mjordan commented Jun 30, 2019

JSON::API allows fetching of field configuration by field UUID:

http://localhost:8000/jsonapi/field_config/field_config/8d5c9dc2-1823-43c9-8c6f-5fae6ae77ec4

So, if we can get field UUIDs from content types, we can introspect a node's fields prior to generating requests. Only missing piece is to get the field UUIDs on a node.

mjordan added a commit that referenced this issue Jul 1, 2019
@mjordan
Copy link
Owner Author

mjordan commented Jul 1, 2019

http://admin:islandora@localhost:8000/jsonapi/field_config/field_config provides all the fields and their configuration info.

#!/usr/bin/env python3

import os
import sys
import json
import requests

fields_url = 'http://admin:islandora@localhost:8000/jsonapi/field_config/field_config'

headers = {'Content-Type': 'application/json'}
response = requests.get(fields_url, auth=('admin', 'islandora'), headers=headers)

field_config = json.loads(response.text)
field_types = {}
for item in field_config['data']:
	field_name = item['attributes']['field_name']
	field_types[field_name] = item['attributes']['field_type']

print(field_types)

gets us a dictionary like this:

{
'body': 'text_with_summary',
'comment_body': 'text_long',
'field_access_terms': 'entity_reference',
'field_file_size': 'integer', 
'field_media_audio_file': 'file',
'field_media_of': 'entity_reference',
'field_media_use': 'entity_reference',
'field_mime_type': 'string',
'field_media_file': 'file',
'field_height': 'integer',
'field_media_image': 'image',
'field_width': 'integer',
'field_media_oembed_video': 'string',
'field_media_video_file': 'file',
'comment': 'comment',
'field_image': 'image',
'field_tags': 'entity_reference',
'field_alternative_title': 'string',
'field_description': 'string_long',
'field_display_hints': 'entity_reference',
'field_edtf_date': 'edtf',
'field_edtf_date_created': 'edtf',
'field_edtf_date_issued': 'edtf',
'field_extent': 'string',
'field_identifier': 'string',
'field_linked_agent': 'typed_relation',
'field_member_of': 'entity_reference',
'field_model': 'entity_reference',
'field_pid': 'string',
'field_resource_type': 'entity_reference',
'field_rights': 'string',
'field_subject': 'entity_reference'
}

@mjordan
Copy link
Owner Author

mjordan commented Jul 2, 2019

A glitch. Neither /jsonapi/field_config/field_config/8d5c9dc2-1823-43c9-8c6f-5fae6ae77ec4 nor /jsonapi/field_config/field_config provides the cardinality of a field. This is important since PATCHing replaces all values in a field. If a multivalued field already has a value, and we want to keep it and add a second value, we need to get the original value and include it in our PATCH. If a field is monovalued, we can only include one value in our PATCH.

@mjordan
Copy link
Owner Author

mjordan commented Jul 2, 2019

http://admin:islandora@localhost:8000/jsonapi/field_storage_config/field_storage_config provides cardinality.

mjordan added a commit that referenced this issue Jul 2, 2019
mjordan added a commit that referenced this issue Jul 2, 2019
@mjordan
Copy link
Owner Author

mjordan commented Jul 2, 2019

Resolved with 3e95222.

@mjordan mjordan closed this as completed Jul 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant