Skip to content

Commit

Permalink
Simple REST client.
Browse files Browse the repository at this point in the history
  • Loading branch information
weaver committed Sep 10, 2010
0 parents commit 412a2ca
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.egg-info
build
dist
*.py?
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Python Relay.IO Client #

Use this client to add data to your relay from a Python process. To
get started, [create a relay][1] if you haven't already.

Go to your relay's console and find the "Post to URL" and "Insert Key"
on the "Server-Side Insert" tab. Then, add this to your program:

import relayio

relay = relayio.RelayIO("{{Post to URL}}", "{{Insert Key}}")
relay.insert({ "sample" : "data", "addyourdata" : "here" })

Replace "Post to URL" and "Insert Key" in the example above with what
you found in the console's "Server-Side Insert" tab.

Take a look at the `examples` folder to see working scripts.

## License ##

Copyright (c) 2010, Medium <labs@thisismedium.net>
All rights reserved.

Redistribution and use in source and binary forms, 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.

* Neither the name of the <organization> nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE 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 <COPYRIGHT
HOLDER> 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, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

[1]: http://relay.io/
33 changes: 33 additions & 0 deletions examples/talk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python

"""talk.py post-to-url insert-key message
This talk script finds your username in the environment and reads a
message from standard input. It packages this information into a
dictionary, then adds it to your relay.
For example:
> talk.py http://api.relay.io/insert/MY-FEED-ID MY-INSERT-KEY 'Hello, relay!'
"""

import os, sys, relayio

def main(feedUri=None, insertKey=None, message=None):
if not (feedUri and insertKey and message):
usage()

relay = relayio.RelayIO(feedUri, insertKey)
relay.insert({
'who': os.environ.get('USER', 'anonymous'),
'said': message
})

print 'Message sent.'

def usage():
print __doc__
sys.exit(1)

if __name__ == '__main__':
main(*sys.argv[1:])
29 changes: 29 additions & 0 deletions relayio/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""relayio -- Python client interface to <http://relay.io/>."""

from __future__ import absolute_import
import httplib, urllib, urlparse, json

class RelayIO(object):
"""RelayIO -- REST client"""

def __init__(self, feedUri, insertKey):
self.insertKey = insertKey
self.apiUri = urlparse.urlparse(feedUri)

def insert(self, data):
params = urllib.urlencode({
'insertkey': self.insertKey,
'payload': json.dumps(data)
})

client = httplib.HTTPConnection(self.apiUri.netloc)
client.request('POST', self.apiUri.path, params, {
'Content-Type': 'application/x-www-form-urlencoded'
})

response = client.getresponse()
if response.status != 200:
raise RelayError(response.read())

class RelayError(RuntimeError):
pass
15 changes: 15 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from __future__ import absolute_import
from setuptools import setup, find_packages

setup(
name = 'relayio',
version = '0.1',
description = 'Relay.io Client',
author = 'Medium',
author_email = 'labs@thisismedium.com',
license = 'BSD',
keywords = 'rest relay.io',

packages = list(find_packages(exclude=('examples', ))),
install_requires = []
)

0 comments on commit 412a2ca

Please sign in to comment.