Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #9 from postatum/develop
Browse files Browse the repository at this point in the history
Implement posting to singular resource with post2api
  • Loading branch information
jstoiko committed Apr 17, 2015
2 parents ad37bef + fa1bebd commit 85cba0a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
91 changes: 91 additions & 0 deletions nefertari/scripts/post2api.py
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()
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@
entry_points="""\
[console_scripts]
nefertari.index = nefertari.scripts.es:main
nefertari.post2api = nefertari.scripts.post2api:main
""",
)

0 comments on commit 85cba0a

Please sign in to comment.