|
2 | 2 |
|
3 | 3 | # osdf-python |
4 | 4 |
|
| 5 | +## Basic usage |
| 6 | + |
5 | 7 | import pprint |
6 | 8 | from osdf import OSDF |
7 | 9 |
|
|
15 | 17 | info = osdf.get_info() |
16 | 18 |
|
17 | 19 | pprint.pprint(info) |
| 20 | + |
| 21 | + |
| 22 | +## SSL/Encrypted connections |
| 23 | + |
| 24 | +To establish a connection to an instance of OSDF that is encrypted by |
| 25 | +operating with an SSL certificate, simply pass the a named ssl parameter |
| 26 | +set to true. |
| 27 | + |
| 28 | + osdf = OSDF(server, username, password, port, ssl=True) |
| 29 | + |
| 30 | +## Obtaining the server information |
| 31 | + |
| 32 | + info = osdf.get_info() |
| 33 | + |
| 34 | + pprint.pprint(info) |
| 35 | + |
| 36 | + { |
| 37 | + "api_version": "0.1", |
| 38 | + "title": "EXAMPLE-OSDF", |
| 39 | + "description": "Open Science Data Framework (OSDF)", |
| 40 | + "admin_contact_email1": "osdf-admin@example.edu", |
| 41 | + "admin_contact_email2": "help@example.edu", |
| 42 | + "technical_contact1": "osdf@example.edu", |
| 43 | + "technical_contact2": "osdf-helpdesk@example.edu", |
| 44 | + "comment1": "comment1", |
| 45 | + "comment2": "comment2" |
| 46 | + } |
| 47 | + |
| 48 | +## Retrieve an existing node |
| 49 | +One is able to retrieve a node by ID easily. |
| 50 | + |
| 51 | + node = osdf.get_node(node_id) |
| 52 | + |
| 53 | +## Retrieve a previous version of an existing node |
| 54 | +OSDF maintains the history of nodes as they change over time |
| 55 | +from edit to edit. To retrieve a particular node at a specific |
| 56 | +version number, simply pass the versoin number (as well as the node |
| 57 | +id of interest) to the get_node_by_version() function. |
| 58 | + |
| 59 | + node = osdf.get_node_by_version(node_id, version) |
| 60 | + |
| 61 | +## Validate a node document |
| 62 | +Sometimes its useful to check if a document validates against the OSDF instance |
| 63 | +to verify if the metadata in the document passes all the structural integrity |
| 64 | +and other requirements. To validate a document, use the validate_node() |
| 65 | +function, and pass the JSON data as an argument. The return is a tuple with the |
| 66 | +first value containing a boolean with the result of the validation, and the |
| 67 | +second value will contain the error message (if the document was not valid). |
| 68 | + |
| 69 | + (is_valid, error) = osdf.validate_node(json_data) |
| 70 | + |
| 71 | +## Inserting a node |
| 72 | +The creation/insertion of a new node returns the node's ID. |
| 73 | + |
| 74 | + document = { |
| 75 | + "ns": "test", |
| 76 | + "acl": { "read": [ "all" ], "write": [ "all" ] }, |
| 77 | + "linkage": {}, |
| 78 | + "node_type": "example", |
| 79 | + "meta": { |
| 80 | + "description": "something", |
| 81 | + "color": "blue" |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + node_id = osdf.insert_node(document) |
| 86 | + |
| 87 | +## Edit/Update a node |
| 88 | +Updates an existing node document with new/edited data. OSDF will save the |
| 89 | +older data to the node's history, and it will be available for retrieval |
| 90 | +by version number. The provided node data must contain the node ID as well |
| 91 | +as the current version number of the node, so as to avoid conflicts with others |
| 92 | +that may be attempting to edit that document. |
| 93 | + |
| 94 | + osdf.edit_node(node_data) |
| 95 | + |
| 96 | +## Deleting a node |
| 97 | +To delete a node, simply call the delete_node() function. This action also |
| 98 | +removes the historical information associated with that node (previous |
| 99 | +versions). |
| 100 | + |
| 101 | + osdf.delete_node(node_id) |
| 102 | + |
| 103 | +## ElasticSearch DSL queries |
| 104 | + namespace = "test" |
| 105 | + query = '{ "query": { "term": { "node_type": "example" }} }' |
| 106 | + first_page_results = osdf.query(namespace, query) |
| 107 | + # If there are more than 1 "page" of results, additional results |
| 108 | + # can be obtained... |
| 109 | + second_page_results = osdf.query(namespace, query, 2) |
| 110 | + |
| 111 | +To retrieve ALL results by aggregating all the available pages of results |
| 112 | + |
| 113 | + all_results = osdf.query_all_pages(namespace, query) |
| 114 | + |
| 115 | +## OQL (OSDF Query Language) queries |
| 116 | +OSDF also supports a simplified query language called OQL (OSDF Query Language). To issue |
| 117 | +an OQL query: |
| 118 | + |
| 119 | + namespace = "test" |
| 120 | + query = '"example"[node_type]' |
| 121 | + first_page_results = osdf.oql_query(namespace, query, 1) |
| 122 | + # If there are more than 1 "page" of results, additional results |
| 123 | + # can be obtained... |
| 124 | + second_page_results = osdf.oql_query(namespace, query, 2) |
| 125 | + |
| 126 | +To retrieve ALL results by aggregating all the available pages of results |
| 127 | + |
| 128 | + all_results = osdf.oql_query_all_pages(namespace, query) |
| 129 | + |
| 130 | +## Retrieve all schemas for a given namespace |
| 131 | +Namespaces can impose controls on the JSON data contained in their nodes according |
| 132 | +to the nodetype. To retrieve the complete set of registered schemas for a particular |
| 133 | +namespace, use the get_schemas() function: |
| 134 | + |
| 135 | + schemas = osdf.get_schemas(namespace) |
| 136 | + |
| 137 | +## Retrieve a specific schema |
| 138 | +If a node type has a JSON-Schema associated with it, that specific schema |
| 139 | +can be queried from OSDF using the get_schema() function by passing the name of |
| 140 | +namespace as well as the name of the schema/node_type. |
| 141 | + |
| 142 | + schema = osdf.get_schema(namespace, schema_name) |
| 143 | + |
| 144 | +## Retrieve an OSDF auxiliary schema |
| 145 | +Schemas can share JSON-Schema fragments between them in order to avoid duplication. |
| 146 | +These schema fragements are referred to as auxiliary schemas, and are also |
| 147 | +retrievable with the get_aux_schema() function by passing the namespace and |
| 148 | +auxiliary schema name. |
| 149 | + |
| 150 | + aux_schema = osdf.get_aux_schema(namespace, aux_schema_name) |
| 151 | + |
0 commit comments