def _check_type_attr(connector_name, cb_name, attr, value): if type(value) == type(None): if attr.empty: return else: raise ConnectorError( 'Attribute "{}" cannot be empty. Connector: {}, callback: {}'. format(attr.name, connector_name, cb_name)) else: if attr.type == 'str': if not isinstance(value, (basestring)): raise ConnectorError( 'Attribute "{}" type mismatch, expected string received {}. Connector: {}, callback: {}' .format(attr.name, type(value).__name__, connector_name, cb_name)) elif attr.type == 'num': if not isinstance(value, (int, long, float)): raise ConnectorError( 'Attribute "{}" type mismatch, expected string received {}. Connector: {}, callback: {}' .format(attr.name, type(value).__name__, connector_name, cb_name)) else: if not isinstance(value, bool): raise ConnectorError( 'Attribute "{}" type mismatch, expected string received {}. Connector: {}, callback: {}' .format(attr.name, type(value).__name__, connector_name, cb_name))
def _check_type_attr(attr_name, attr_type, value): if attr_type == 'str' and not type(value).__name__ == 'str': raise ConnectorError( 'Attribute {} type mismatch, expected string received {}'.format( attr_name, type(value))) elif attr_type == 'num' and not type(value).__name__ == 'int': raise ConnectorError( 'Attribute {} type mismatch, expected string received {}'.format( attr_name, type(value))) else: if not type(value).__name__ == 'bool': raise ConnectorError( 'Attribute {} type mismatch, expected string received {}'. format(attr_name, type(value)))
def get_json_or_error(connector_name, cb, response): if response.status_code and not 200 <= response.status_code < 300: raise ConnectorError('Error when calling the callback {} of the connector {}. Message: {}' .format(cb.name, connector_name, response.content)) else: if cb.format == 'json': return response.json() elif cb.format == 'xml': return response.xml() elif cb.format == 'txt': return response.text else: raise ConnectorError('Error, do not understand response of callback {} of the connector {}. Expected ' 'json or xml'.format(cb.name, connector_name))
def get_url_cb(connector, name): for url_cb in connector.url_callback.all(): if url_cb.callback.name == name: return url_cb raise ConnectorError( 'The callback \'{}\' was not defined in the connector {}'.format( name, connector))
def _check_composed_attrs(connector_name, cb_name, expected_ret_obj, ret_obj): for composed_attr in expected_ret_obj.composed_attributes.all(): if composed_attr.name not in ret_obj.keys(): raise ConnectorError( 'Required attribute "{}" not received. Connector: {}, callback: {}' .format(composed_attr.name, cb_name, connector_name)) _check_simple_attrs(connector_name, cb_name, composed_attr.type, ret_obj[composed_attr.name])
def _check_simple_attrs(expected_ret_obj, ret_obj): for attr in expected_ret_obj.attributes.all(): if attr.name not in ret_obj.keys(): if attr.required: raise ConnectorError( 'Required attribute {} not received'.format(attr.name)) else: value = ret_obj[attr.name] _check_type_attr(attr.name, attr.type, value)
def get_code(cls, app_id, app_secret, app_redirect_uri, access_token): url = cls.host + \ '/oauth/client_code?access_token={}&client_secret={}&redirect_uri={}&client_id={}' resp = requests.get( url=url.format(access_token, app_secret, app_redirect_uri, app_id)) if resp.status_code and not 200 <= resp.status_code < 300: raise ConnectorError('Error when trying to get the code') else: json_resp = json.loads(resp.text) return json_resp['code']
def _check_simple_attrs(connector_name, cb_name, expected_ret_obj, ret_obj): for attr in expected_ret_obj.attributes.all(): if attr.name not in ret_obj.keys(): if attr.required: raise ConnectorError( 'Required attribute "{}" not received. Connector: {}, callback: {}' .format(attr.name, connector_name, cb_name)) else: value = ret_obj[attr.name] _check_type_attr(connector_name, cb_name, attr, value)
def error_handler(cls, method, error_obj, params=None): logger.warning( 'Error when trying to call the method {}. Reason: {}'.format( method, error_obj)) if 'Error validating access token' in error_obj: # TODO: There is a problem with the access token, notify the user. # User's email is saved in params pass else: raise ConnectorError(error_obj)
def get_long_lived_page_token(cls, app_id, app_secret, access_token, page_id): access_token = cls.get_long_lived_access_token( app_id, app_secret, access_token)['access_token'] graph = facebook.GraphAPI(access_token) # Get page token to post as the page resp = graph.get_object('me/accounts') for page in resp['data']: if page['id'] == page_id: return page['access_token'] raise ConnectorError('Couldn\'t get page long-lived token')
def build_request_body(connector, callback, params): req_param = {} cb_body_params = callback.body_params.all() for cb_body_param in cb_body_params: try: req_param[cb_body_param.name] = params[cb_body_param.name] except KeyError: if cb_body_param.required: raise ConnectorError('Parameter {} missing. It required for calling {}, which is a callback of ' 'the connector {}. The callback must include this parameter as part of the ' 'parameter set'.format(cb_body_param.name, callback.name, connector.name)) return req_param
def subscribe_real_time_updates(cls, app, data): cls.authenticate(app) url = cls.api_app_page_subscription.format(app.community.external_id) data_token = ({'access_token': app.community.token}) resp = requests.post(url=url, data=data_token) resp_text = json.loads(resp.text) if resp.status_code and not 200 <= resp.status_code < 300: raise ConnectorError(cls._get_req_error_msg(resp_text)) else: if not app.app_access_token: access_token = facebook.get_app_access_token( app.app_id, app.app_secret) else: access_token = app.app_access_token data.update({'access_token': access_token}) url = cls.api_real_time_subscription.format(app.app_id) resp = requests.post(url=url, data=data) resp_text = json.loads(resp.text) if resp.status_code and not 200 <= resp.status_code < 300: raise ConnectorError(cls._get_req_error_msg(resp_text)) else: return resp_text
def get_long_lived_access_token(cls, app_id, app_secret, access_token): url = cls.host + \ '/oauth/access_token?client_id={}&client_secret={}&grant_type=fb_exchange_token&fb_exchange_token={}' resp = requests.get(url=url.format(app_id, app_secret, access_token)) if resp.status_code and not 200 <= resp.status_code < 300: raise ConnectorError( 'Error when trying to get long-lived access token') else: json_resp = json.loads(resp.text) return { 'access_token': json_resp['access_token'], 'expiration': json_resp['expires_in'] }
def delete_subscription_real_time_updates(cls, app, data): if not app.app_access_token: access_token = facebook.get_app_access_token( app.app_id, app.app_secret) else: access_token = app.app_access_token data.update({'access_token': access_token}) url = cls.api_real_time_subscription.format(app.app_id) resp = requests.delete(url=url, data=data) resp_text = json.loads(resp.text) if resp.status_code and not 200 <= resp.status_code < 300: raise ConnectorError(cls._get_req_error_msg(resp_text)) else: return resp_text
def get_long_lived_access_token(cls, app_id, app_secret, access_token): url = cls.host + \ '/oauth/access_token?client_id={}&client_secret={}&grant_type=fb_exchange_token&fb_exchange_token={}' resp = requests.get(url=url.format(app_id, app_secret, access_token)) if resp.status_code and not 200 <= resp.status_code < 300: raise ConnectorError( 'Error when trying to get long-lived access token') else: str_resp = resp.text resps = str_resp.split('&') return { 'access_token': resps[0].split('=')[1], 'expiration': resps[1].split('=')[1] }
def _check_cb_return_obj(connector_name, callback, ret_obj): expected_ret_obj = callback.return_object if len(ret_obj) > 0: if callback.return_type == 'set': for obj in ret_obj: _check_simple_attrs(connector_name, callback.name, expected_ret_obj, obj) _check_composed_attrs(connector_name, callback.name, expected_ret_obj, obj) else: _check_simple_attrs(connector_name, callback.name, expected_ret_obj, ret_obj) _check_composed_attrs(connector_name, callback.name, expected_ret_obj, ret_obj) return True else: raise ConnectorError( 'Received an empty response. Connector: {}, callback: {}'.format( connector_name, callback.name))
def _check_composed_attrs(expected_ret_obj, ret_obj): for composed_attr in expected_ret_obj.composed_attributes.all(): if composed_attr.name not in ret_obj.keys(): raise ConnectorError('Required attribute {} not received'.format( composed_attr.name)) _check_simple_attrs(composed_attr, ret_obj[composed_attr.name])