-
Notifications
You must be signed in to change notification settings - Fork 4
/
create_remote_data_file.py
92 lines (61 loc) · 2.95 KB
/
create_remote_data_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-
"""create_remote_data_file.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1joQNoAgZlmeo0u5SCkldZQvkr7zZvOS4
Import the libraries so that they can be used within the notebook
* **requests** is used to make HTTP calls
* **json** is used to encode and decode strings into JSON
* **string** is used to perform text manipulation and checking
* **getpass** is used to do non-echoing password input
"""
import requests
import json
import string
import getpass
"""The **base_url** holds the URL to the SEEK instance that will be used in the notebook
**headers** holds the HTTP headers that will be sent with every HTTP call
* **Content-type: application/vnd.api+json** - indicates that any data sent will be in JSON API format
* **Accept: application/vnd.api+json** - indicates that the notebook expects any data returned to be in JSON API format
* **Accept-Charset: ISO-8859-1** - indicates that the notebook expects any text returned to be in ISO-8859-1 character set
"""
base_url = 'https://testing.sysmo-db.org'
headers = {"Content-type": "application/vnd.api+json",
"Accept": "application/vnd.api+json",
"Accept-Charset": "ISO-8859-1"}
"""Create a **requests** HTTP **Session**. A **Session** has re-usable settings such as **headers**
The **authorization** is username and password. The user is prompted for this information.
"""
session = requests.Session()
session.headers.update(headers)
session.auth = (input('Username: '), getpass.getpass('Password: '))
"""The **data_file** will be created within **Project** 33"""
containing_project_id = 33
"""Initialize a **data_file** as a hierarchical structure
The title of the **data_file** is input by the user
The **data_file** is linked to the containing **project**
"""
data_file = {}
data_file['data'] = {}
data_file['data']['type'] = 'data_files'
data_file['data']['attributes'] = {}
data_file['data']['attributes']['title'] = input('Please enter the name for the data_file: ')
remote_blob = {'url' : input('Please enter the URL for the remote data: ')}
data_file['data']['attributes']['content_blobs'] = [remote_blob]
data_file['data']['relationships'] = {}
data_file['data']['relationships']['projects'] = {}
data_file['data']['relationships']['projects']['data'] = [{'id' : containing_project_id, 'type' : 'projects'}]
"""**POST** the **data_file** to the SEEK instance
Check the status of the response
"""
r = session.post(base_url + '/data_files', json=data_file)
r.raise_for_status()
"""Extract the created **data_file** from JSON into **populated_data_file**"""
populated_data_file = r.json()
"""Extract the id and URL to the newly created **data_file**"""
data_file_id = populated_data_file['data']['id']
data_file_url = populated_data_file['data']['links']['self']
"""Tidy up the SEEK by deleting the **data_file**."""
session.delete(base_url + data_file_url)
"""Close the HTTP **session**"""
session.close()