diff --git a/pypodio2/api.py b/pypodio2/api.py index 75a3a0d..dc7286f 100644 --- a/pypodio2/api.py +++ b/pypodio2/api.py @@ -16,12 +16,31 @@ def OAuthClient(api_key, api_secret, login, password, user_agent=None, return AuthorizingClient(domain, auth, user_agent=user_agent) +def OAuthClientReturningToken(api_key, api_secret, login, password, user_agent=None, + domain="https://api.podio.com"): + auth = transport.OAuthAuthorization(login, password, + api_key, api_secret, domain) + return auth.token, AuthorizingClient(domain, auth, user_agent=user_agent) + + +def OAuthClientUsingToken(token, user_agent=None, + domain="https://api.podio.com"): + auth = transport.OAuthTokenAuthorization(token) + return AuthorizingClient(domain, auth, user_agent=user_agent) + + +def OAuthClientRefresh(api_key, api_secret, refresh_token, user_agent=None, + domain="https://api.podio.com"): + auth = transport.OAuthRefreshAuthorization(api_key, api_secret, + refresh_token, domain) + return auth.token, AuthorizingClient(domain, auth, user_agent=user_agent) + + def OAuthAppClient(client_id, client_secret, app_id, app_token, user_agent=None, domain="https://api.podio.com"): auth = transport.OAuthAppAuthorization(app_id, app_token, client_id, client_secret, domain) - return AuthorizingClient(domain, auth, user_agent=user_agent) diff --git a/pypodio2/transport.py b/pypodio2/transport.py index 2b8eee9..629d390 100644 --- a/pypodio2/transport.py +++ b/pypodio2/transport.py @@ -31,6 +31,34 @@ def to_headers(self): return {'authorization': "OAuth2 %s" % self.access_token} +class OAuthRefreshAuthorization(object): + """Regenerates headers for Podio OAuth2 Authorization""" + + def __init__(self, key, secret, refresh_token, domain): + body = {'grant_type': 'refresh_token', + 'client_id': key, + 'client_secret': secret, + 'refresh_token': refresh_token} + h = Http(disable_ssl_certificate_validation=True) + headers = {'content-type': 'application/x-www-form-urlencoded'} + response, data = h.request(domain + "/oauth/token", "POST", + urlencode(body), headers=headers) + self.token = OAuthToken(_handle_response(response, data)) + + def __call__(self): + return self.token.to_headers() + + +class OAuthTokenAuthorization(object): + """Generates headers for Podio OAuth2 Authorization""" + + def __init__(self, token): + self.token = token + + def __call__(self): + return self.token.to_headers() + + class OAuthAuthorization(object): """Generates headers for Podio OAuth2 Authorization"""