This repository has been archived by the owner on Mar 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_update_bag.py
executable file
·69 lines (61 loc) · 2.74 KB
/
create_update_bag.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
#!/usr/bin/python
""" Given a json bag record, create or update in the target system """
from app.dpn_python_library import *
import json
import requests
import sys
json_messages={}
# Retrieve environment variables
dpn_host, dpn_token = load_environment()
dpn_headers={'Content-Type': 'application/json','Accept': 'application/json'}
dpn_headers['Authorization']="Token token="+dpn_token
#log_message("DPN Host: "+dpn_host)
#log_message("DPN Headers: "+json.dumps(dpn_headers))
# Read content from stdin
try:
input_record=sys.stdin.read().replace('\n', '')
# log_message("length of JSON input " + str(len(input_record)))
if len(input_record) == 0:
log_message('"message": "Bag Record required as input"')
exit(1)
sync_record=json.loads(input_record)
dpn_querystring="/api-v2/bag/"+sync_record['uuid']
except (ValueError, IndexError, KeyError):
json_messages['message'] = "JSON formatted Bag record required as input"
json_messages['input'] = input_record
log_json_message(json_messages)
exit(0)
response = requests.get(dpn_host+dpn_querystring, headers=dpn_headers)
if response.status_code is 200:
response_record=json.loads(response.text)
# log_message("uuid: "+sync_record['uuid']+"="+response_record['uuid']+"; updated: "+sync_record['updated_at']+"="+response_record['updated_at'])
if sync_record['updated_at'] > response_record['updated_at']:
update_response=requests.put(dpn_host+dpn_querystring, headers=dpn_headers, data=input_record)
if update_response.status_code is not 200:
json_messages['message'] = "Update Bag Failed"
json_messages['return_code'] = str(update_response.status_code)
json_messages['bag_uuid'] = sync_record['uuid']
log_json_message(json_messages)
else:
json_messages['message'] = "updated bag record"
json_messages['bag_uuid'] = sync_record['uuid']
log_json_message(json_messages)
# else:
# if response_record['updated_at'] > sync_record['updated_at']:
# log_message("local record is newer")
# else:
# log_message("records match")
else:
# if response.status_code is 404:
update_response=requests.post(dpn_host+"/api-v2/bag", headers=dpn_headers, data=input_record)
if update_response.status_code is not 201:
json_messages['message'] = "create bag failed"
json_messages['bag_uuid'] = sync_record['uuid']
json_messages['return_code'] = str(update_response.status_code)
log_json_message(json_messages)
else:
json_messages['message'] = "created bag record"
json_messages['bag_uuid'] = sync_record['uuid']
json_messages['return_code'] = str(response.status_code)
log_json_message(json_messages)
exit(0)