This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from postatum/develop
Implement posting to singular resource with post2api
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#!/usr/bin/env python | ||
import json | ||
import requests | ||
import sys | ||
import getopt | ||
|
||
|
||
def _jdefault(obj): | ||
return obj.__dict__ | ||
|
||
|
||
def load(inputfile, destination): | ||
json_file = open(inputfile) | ||
json_data = json.load(json_file) | ||
|
||
for i in json_data: | ||
data = json.dumps(i, default=_jdefault) | ||
print('Posting: %s' % data) | ||
r = requests.post( | ||
destination, | ||
data=data, | ||
headers={'Content-type': 'application/json'}) | ||
print r.status_code | ||
|
||
json_file.close() | ||
|
||
|
||
def load_singular_objects(inputfile, destination): | ||
parent_route, dynamic_part = destination.split('{') | ||
parent_route = parent_route.strip('/') | ||
id_field, singlular_field = dynamic_part.split('}') | ||
singlular_field = singlular_field.strip('/') | ||
|
||
json_file = open(inputfile) | ||
json_data = json.load(json_file) | ||
objects_count = len(json_data) | ||
|
||
query_string = '?_limit={}&_fields={}'.format(objects_count, id_field) | ||
parent_objects = requests.get(parent_route + query_string).json()['data'] | ||
|
||
for parent in parent_objects: | ||
parent_url = parent['self'].replace(query_string, '') | ||
singular_url = parent_url + '/' + singlular_field | ||
child = json_data.pop() | ||
data = json.dumps(child, default=_jdefault) | ||
print('Posting: {} to {}'.format(data, singular_url)) | ||
r = requests.post( | ||
singular_url, | ||
data=data, | ||
headers={'Content-type': 'application/json'}) | ||
print r.status_code | ||
|
||
|
||
def main(): | ||
argv = sys.argv[1:] | ||
try: | ||
opts, args = getopt.getopt(argv, 'hf:u:', ['help', 'file=', 'url=']) | ||
except getopt.GetoptError: | ||
usage() | ||
sys.exit(2) | ||
|
||
for opt, arg in opts: | ||
if opt == '-h': | ||
usage() | ||
sys.exit() | ||
elif opt in ('-f', '--file'): | ||
inputfile = arg | ||
elif opt in ('-u', '--url'): | ||
destination = arg | ||
|
||
try: | ||
inputfile | ||
destination | ||
except NameError: | ||
usage() | ||
sys.exit() | ||
|
||
if '{' in destination and not destination.endswith('}'): | ||
# E.g. /users/{username}/profile | ||
load_singular_objects(inputfile, destination) | ||
else: | ||
# E.g. /users | ||
load(inputfile, destination) | ||
|
||
|
||
def usage(): | ||
print 'Usage: nefertari.post2api -f <jsonFile> -u <urlToPost>' | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters