Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaiarocci committed Nov 21, 2012
0 parents commit 3562636
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 0 deletions.
12 changes: 12 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Eve Demo Client is written and maintained by Nicola Iarocci and various
contributors (I wish!):

Development Lead
````````````````

- Nicola Iarocci <nicola@nicolaiarocci.com>

Patches and Suggestions
```````````````````````

- <your name here>
33 changes: 33 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Copyright (c) 2012 by Nicola Iarocci and contributors. See AUTHORS
for more details.

Some rights reserved.

Redistribution and use in source and binary forms of the software as well
as documentation, with or without modification, are permitted provided
that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* The names of the contributors may not be used to endorse or
promote products derived from this software without specific
prior written permission.

THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE AND DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
16 changes: 16 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Eve Demo Client
===============

Simple and quickly-assembled script used to reset the `Eve-Demo
<https://github.com/nicolaiarocci/eve-demo>`_ REST Web API.

It will use standard API calls to:

1) delete all items in the 'people' and 'works' collections
2) post default items in both collections

I guess it can serve as basic example of how to programmatically manage
a remote API using the phenomenal Requests library by Kenneth Reitz.

:copyright: (c) 2012 by Nicola Iarocci.
:license: BSD, see LICENSE for more details.
108 changes: 108 additions & 0 deletions client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import requests
import json
import random

#ENTRY_POINT = 'http://eve-demo.herokuapp.com'
ENTRY_POINT = 'http://localhost:5000'


def post_people():
people = [
{
'firstname': 'John',
'lastname': 'Doe',
'role': ['author'],
'location': {'address': '422 South Gay Street', 'city': 'Auburn'},
'born': 'Thu, 27 Aug 1970 14:37:13 UTC'
},
{
'firstname': 'Serena',
'lastname': 'Love',
'role': ['author'],
'location': {'address': '363 Brannan St', 'city': 'San Francisco'},
'born': 'Wed, 25 Feb 1987 17:00:00 UTC'
},
{
'firstname': 'Mark',
'lastname': 'Green',
'role': ['copy', 'author'],
'location': {'address': '4925 Lacross Road', 'city': 'New York'},
'born': 'Sat, 23 Feb 1985 12:00:00 UTC'
},
{
'firstname': 'Julia',
'lastname': 'Red',
'role': ['copy'],
'location': {'address': '98 Yatch Road', 'city': 'San Francisco'},
'born': 'Sun, 20 Jul 1980 11:00:00 UTC'
},
{
'firstname': 'Anne',
'lastname': 'White',
'role': ['contributor', 'copy'],
'location': {'address': '32 Joseph Street', 'city': 'Ashfield'},
'born': 'Fri, 25 Sep 1970 10:00:00 UTC'
},
]

payload = {}
for person in people:
payload[person['lastname']] = json.dumps(person)

r = perform_post('people', payload)

valids = []
if r.status_code == 200:
response = r.json['response']
for person in payload:
result = response[person]
if result['status'] == "OK":
valids.append(result['_id'])

return valids


def post_works(ids):
titles = ['First', 'Second', 'Third', 'Fourth', 'Fifth']
works = []
for i in range(5):
works.append(
{
'title': '%s Book Title' % titles[i],
'description': '%s description' % titles[i],
'owner': random.choice(ids),
#'contributors': random.sample(ids, random.randint(1, 5))
}
)

payload = {}
for i in range(len(works)):
payload['work' + str(i+1)] = json.dumps(works[i])
r = perform_post('works', payload)
print r.json

def perform_post(resource, data):
return requests.post(endpoint(resource), data)


def delete():
perform_delete('people')
perform_delete('works')


def perform_delete(resource):
return requests.delete(endpoint(resource))


def endpoint(resource):
return '%s/%s/' % (ENTRY_POINT, resource)


def get():
r = requests.get('http://eve-demo.herokuapp.com')

if __name__ == '__main__':
delete()
ids = post_people()
post_works(ids)
#get()

0 comments on commit 3562636

Please sign in to comment.