Skip to content

Commit

Permalink
Add docstring for all classes and methods.
Browse files Browse the repository at this point in the history
Fix bug with docs generator
  • Loading branch information
james2710 committed Sep 4, 2015
1 parent 8983458 commit f796702
Show file tree
Hide file tree
Showing 12 changed files with 589 additions and 93 deletions.
159 changes: 78 additions & 81 deletions DOCUMENTATION.md

Large diffs are not rendered by default.

Empty file added __init__.py
Empty file.
2 changes: 0 additions & 2 deletions docs/doc2md.py
Expand Up @@ -19,7 +19,6 @@

INDENT = " "
NEW_LINE = ""
LINK = '<a name="{name}"></a>'

# Level for each section in class
CLASS_NAME = 2
Expand Down Expand Up @@ -164,7 +163,6 @@ def doc2md(docstr, title, type=0):
intro, contents = _get_class_intro(lines)
if type == 0:
level = CLASS_NAME
title = LINK.format(name=title.lower())+title
if type == 1:
level = FUNCTION_NAME
title = 'Function: {func_name}'.format(func_name=title)
Expand Down
31 changes: 31 additions & 0 deletions resin/__init__.py
@@ -0,0 +1,31 @@
"""
Welcome to the Resin Python SDK documentation.
This document aims to describe all the functions supported by the SDK, as well as
showing examples of their expected usage.
If you feel something is missing, not clear or could be improved, please don't
hesitate to open an issue in GitHub, we'll be happy to help.
"""

from .base_request import BaseRequest
from .auth import Auth
from .token import Token
from .logs import Logs
from .settings import Settings
from .models import Models

class Resin(object):
"""
This class implements all functions supported by the Python SDK.
Attributes:
settings (Settings): configuration settings for Resin Python SDK.
logs (Logs): logs from devices working on Resin.
auth (Auth): authentication handling.
models (Models): all models in Resin Python SDK.
"""

def __init__(self):
self.settings = Settings()
self.logs = Logs()
self.auth = Auth()
self.models = Models()
128 changes: 128 additions & 0 deletions resin/auth.py
Expand Up @@ -5,50 +5,178 @@


class Auth(object):
"""
This class implements all authentication functions for Resin Python SDK.
"""

def __init__(self):
self.base_request = BaseRequest()
self.settings = Settings()
self.token = Token()

def login(self, **credentials):
"""
This function is used for logging into Resin.io using username and password.
Args:
**credentials: credentials keyword arguments.
username (str): Resin.io username.
password (str): Password.
Returns:
This functions saves Auth Token to Settings and returns nothing.
Raises:
LoginFailed: if the username or password is invalid.
"""

token = self.authenticate(**credentials)
if self.token.is_valid_token(token):
self.token.set(token)
else:
raise exceptions.LoginFailed()

def login_with_token(self, token):
"""
This function is used for logging into Resin.io using Auth Token.
Auth Token can be found in Preferences section on Resin.io Dashboard.
Args:
token (str): Auth Token.
Returns:
This functions saves Auth Token to Settings and returns nothing.
Raises:
MalformedToken: if token is invalid.
"""

if self.token.is_valid_token(token):
self.token.set(token)
else:
raise exceptions.MalformedToken(token)

def who_am_i(self):
"""
This function retrieves username of logged in user.
Returns:
str: username.
Raises:
NotLoggedIn: if there is no user logged in.
"""

return self.token.get_username()

def authenticate(self, **credentials):
"""
This function authenticates provided credentials information.
You should use Auth.login when possible, as it takes care of saving the Auth Token and username as well.
Args:
**credentials: credentials keyword arguments.
username (str): Resin.io username.
password (str): Password.
Returns:
str: Auth Token,
Raises:
LoginFailed: if the username or password is invalid.
"""

return self.base_request.request(
'login_', 'POST', data=credentials,
endpoint=self.settings.get('api_endpoint'), auth=False
)

def is_logged_in(self):
"""
This function checks if you're logged in
Returns:
bool: True if logged in, False otherwise.
"""

return self.token.has()

def get_token(self):
"""
This function retrieves Auth Token.
Returns:
str: Auth Token.
Raises:
InvalidOption: if not logged in and there is no token in Settings.
"""

return self.token.get()

def get_user_id(self):
"""
This function retrieves current logged in user's id.
Returns:
str: user id.
Raises:
InvalidOption: if not logged in.
"""

return self.token.get_user_id()

def get_email(self):
"""
This function retrieves current logged in user's get_email
Returns:
str: user email.
Raises:
InvalidOption: if not logged in.
"""

return self.token.get_email()

def log_out(self):
"""
This function is used for logging out from Resin.io.
Returns:
bool: True if successful, False otherwise.
"""

return self.token.remove()

def register(self, **credentials):
"""
This function is used for registering to Resin.io.
Args:
**credentials: credentials keyword arguments.
email (str): email to register.
password (str): Password.
Returns:
str: Auth Token for new account.
Raises:
RequestError: if error occurs during registration.
"""

return self.base_request.request(
'user/register', 'POST', data=credentials,
endpoint=self.settings.get('api_endpoint'), auth=False
Expand Down

0 comments on commit f796702

Please sign in to comment.