Skip to content

Commit

Permalink
initial commit. slightly borrowed from twilio client
Browse files Browse the repository at this point in the history
Signed-off-by: David A. Cuadrado <[email protected]>
  • Loading branch information
dcu committed Apr 4, 2012
0 parents commit 8d8cea4
Show file tree
Hide file tree
Showing 19 changed files with 3,521 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
build
dist
docs/tmp
docs/build
*.tmproj
*.py[co]
10 changes: 10 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
python-authy Changelog
=======================

Here you can see the full list of changes between each python-authy release.

Version 0.0.1
-------------

- Initial version

22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2012 Authy, Inc.

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
Empty file added README.md
Empty file.
18 changes: 18 additions & 0 deletions authy/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
__version_info__ = ('0', '0', '1')
__version__ = '.'.join(__version_info__)


class AuthyException(Exception):
pass


class AuthyApiException(AuthyException):

def __init__(self, status, uri, msg=""):
self.uri = uri
self.status = status
self.msg = msg

def __str__(self):
return "HTTP ERROR %s: %s \n %s" % (self.status, self.msg, self.uri)

22 changes: 22 additions & 0 deletions authy/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import logging
import os
from authy import AuthyException
from authy.api.resources import Users
from authy.api.resources import Tokens

from urllib import urlencode
from urlparse import urljoin


class AuthyApiClient(object):
"""
A client for accessing the Authy REST API
"""
def __init__(self, api_key, api_uri="https://api.authy.com"):
"""
Create a Authy REST API client.
"""
self.api_uri = api_uri
self.users = Users(api_uri, api_key)
self.tokens = Tokens(api_uri, api_key)
self.api_key = api_key
88 changes: 88 additions & 0 deletions authy/api/resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import re
import datetime
import logging
import authy
from authy import AuthyException
from authy import AuthyApiException
from urllib import urlencode
from urlparse import urlparse

# import json
try:
import json
except ImportError:
try:
import simplejson as json
except ImportError:
from django.utils import simplejson as json

# import httplib2
try:
import httplib2
except ImportError:
from authy.contrib import httplib2


class Resource(object):
def __init__(self, api_uri, api_key):
self.api_uri = api_uri
self.api_key = api_key

def post(self, path, data = {}):
return self.request("POST", path, data, {'Content-Type': 'application/json'})

def get(self, path):
return self.request("GET", path)

def put(self, path, data = {}):
return self.request("PUT", path, data, {'Content-Type': 'application/json'})

def delete(self, path):
return self.request("DELETE", path, data)

def request(self, method, path, data = {}, headers = {}):
http = httplib2.Http()
http.follow_redirects = True

body = json.dumps(data)

url = self.api_uri + path + "?api_key="+self.api_key
return http.request(url, method, headers=headers, body=body)

class Instance(object):
def __init__(self, resource, response, content):
self.resource = resource
self.response = response

try:
self.content = json.loads(content)
except ValueError:
self.content = content

def ok(self):
return self.response['status'] == '200'


class User(Instance):
pass

class Users(Resource):
def create(self, email, phone):
data = {
"user": {
"email": email,
"phone": phone
}
}

resp, content = self.post("/protected/json/users/new", data)

return User(self, resp, content)

class Token(Instance):
pass

class Tokens(Resource):
def verify(self, device_id, token):
resp, content = self.get("/protected/json/verify/"+token+"/"+str(device_id)+"?api_key="+self.api_key)
return Token(self, resp, content)
Empty file added authy/contrib/__init__.py
Empty file.
Loading

0 comments on commit 8d8cea4

Please sign in to comment.