def __init__(self, settings=None): """ Creates a client based off of the given settings. The settings parameter can be a string that points to a JSON file or it can be dictionary of setting values. These settings override the "Global settings" that are set per user via the GLOBAL_SETTINGS_FILE. """ self.logger = Logs.getLogger("PB4Py") if not os.path.exists(Client.GLOBAL_SETTINGS_FILE) and not settings: utils.log_and_raise(self.logger, "No settings given", exceptions.PB4PyConfigurationException) if os.path.exists(Client.GLOBAL_SETTINGS_FILE): self.settings = Client._load_config() self.logger.debug("Config file loaded") else: self.settings = {} if settings: if isinstance(settings, str): settings = Client._load_config(settings) self.logger.info("Parameter config loaded") self.settings.update(settings) self.auth = self._get_auth_module(self.settings.get("auth", None))
def _send_request(self, url, method, url_kwargs={}, skip_auth=False, **kwargs): url_parts = urlparse.urlparse(Helper.API_ROOT) url = urlparse.urlunparse((url_parts.scheme, url_parts.netloc, url.format(**url_kwargs), "", "", "")) auth = self.auth.get_request_auth() if not skip_auth else None resp = requests.request(method, url, auth=auth, **kwargs) if resp.status_code < 200 and resp.status_code >= 300: utils.log_and_raise( self.logger, "Bad status code of {} returned".format(resp.status_code), exceptions.PB4PyAPIException ) ret = resp.json() if resp.status_code != 204 else None return ret
def access_token(self): """ Get the user's access token for this application. """ if 'access_token' not in self.settings: # pylint: disable=line-too-long utils.log_and_raise( self.logger, 'User access token unkown. Grant the application permission by going to {} and then set the access_token with the value that is returned'.format( self.oauth_grant_url, ), OAuthAuthenticationError ) # pylint: enable=line-too-long return self.settings['access_token']
def _get_auth_module(self, auth_settings): if not auth_settings: utils.log_and_raise(self.logger, "No authentication settings found", exceptions.PB4PyConfigurationException) if auth_settings["type"] == "basic": self.logger.debug("Selected Basic Authenticator") return auth.BasicAuthenticator(auth_settings) elif auth_settings["type"] == "oauth": self.logger.debug("Selected OAuth Authenticator") return auth.OAuthAuthenticator(auth_settings) else: utils.log_and_raise( self.logger, "Invalid authentication scheme given. Must be basic or oauth", exceptions.PB4PyConfigurationException, )
def push(self, push_type, **kwargs): """ Push to a specific device, all devices, or a user. Pushes come in several type and each type requires different parameters. These types and their parameters are: push_type = note * title - note's title * body - note's message push_type = link * title - the link's title * url - the url to open * body - optional message push_type = file * file_name - the name of the file * file_type - the MIME type of the file * file_url - the url where the file can be downloaded * body - message to with the file All push types also take a device_iden or email parameter to push to a device or user. If device_iden is not given the push goes to all devices. To send a push to a channel use the parameter channel_tag. To push a file you must first upload it using the upload_file method. """ if push_type not in PushHelper.PUSH_TYPES: utils.log_and_raise( self.logger, 'Invalid push type {}'.format(push_type), exceptions.PB4PyException ) kwargs['type'] = push_type return self._send_request(PushHelper.URL_PUSH_SEND, 'POST', data = kwargs)