def list_paymentMethods(ran=CONF.rax_payment_system.ran, serialize_format='json', deserialize_format='json'): headers = {} headers['accept'] = 'application/%s' % deserialize_format headers['x-auth-token'] = get_token() endpoint = urlparse.urljoin(CONF.syntribos.endpoint, "/v1/accounts/%s/methods" % ran) resp, _ = SynHTTPClient().request("GET", endpoint, headers=headers, sanitize=True) if deserialize_format == 'json': _methods = resp.json()['methods']['method'] else: _methods = xmltodict.parse(resp.data())['ns1:methods'] paymentMethods = {} for m in _methods: _method_obj = models.PaymentMethod._dict_to_obj(m) paymentMethods[_method_obj.methodId] = _method_obj return paymentMethods
def create_refund(ran, serialize_format='json', deserialize_format='json', model=None, **kwargs): headers = {} headers['accept'] = 'application/%s' % deserialize_format headers['content-type'] = 'application/%s' % serialize_format headers['x-auth-token'] = get_token() endpoint = urlparse.urljoin(CONF.syntribos.endpoint, "/v1/accounts/{0}/refunds".format(ran)) if not model: model = models.Void(**kwargs) if serialize_format == 'json': data = model._obj_to_dict() else: data = model._obj_to_xml() resp, _ = SynHTTPClient().request("POST", endpoint, data=data, headers=headers, sanitize=True) if deserialize_format == 'json': resp_data = resp.json()['papi:refund'] else: resp_data = xmltodict.parse(resp.data())['ns2:refund'] return models.Refund._dict_to_obj(resp_data)
def list_payments(ran=CONF.rax_payment_system.ran, serialize_format='json', deserialize_format='json'): headers = {} headers['accept'] = 'application/%s' % deserialize_format headers['x-auth-token'] = get_token() endpoint = urlparse.urljoin(CONF.syntribos.endpoint, "/v1/accounts/%s/payments" % ran) resp, _ = SynHTTPClient().request("GET", endpoint, headers=headers, sanitize=True) if deserialize_format == 'json': _payments = resp.json()['payments']['payment'] else: _payments = xmltodict.parse(resp.data())['ns1:payments'] payments = {} for p in _payments: payments[p['id']] = models.Payment._dict_to_obj(p) return payments
def authenticate(endpoint, username, apiKey): headers = {'content-type': 'application/json'} if endpoint.endswith('/v2.0/'): endpoint = '{0}tokens'.format(endpoint) elif endpoint.endswith('/v2.0'): endpoint = '{0}/tokens'.format(endpoint) elif endpoint.endswith('/v2.0/tokens'): pass else: endpoint = '{0}/v2.0/tokens'.format(endpoint) data = {'auth': {"RAX-KSKEY:apiKeyCredentials": {"username": username, "apiKey": apiKey}}} data = json.dumps(data) try: resp, _ = SynHTTPClient().request( "POST", endpoint, headers=headers, data=data, sanitize=True) r = resp.json() except RequestException as e: LOG.debug(e) else: if not r: raise Exception("Failed to authenticate") if 'access' not in r or not r['access']: raise Exception("Failed to parse Auth response Body") return r['access']
def create_methodValidation(lineOfBusiness=None, contractEntity=None, currencyCode=None, addressVerificationInformation=None, method=None, ran=CONF.rax_payment_system.ran, serialize_format='json', deserialize_format='json'): headers = {} headers['accept'] = 'application/%s' % deserialize_format headers['content-type'] = 'application/%s' % serialize_format headers['x-auth-token'] = get_token() endpoint = urlparse.urljoin(CONF.syntribos.endpoint, "/v1/methodValidations") if addressVerificationInformation: _avi = addressVerificationInformation else: _avi = { 'country': 'US', 'state': 'TX', 'city': 'San Antonio', 'addressLine1': '1 Fanatical Pl', 'addressLine2': '', 'postalCode': '78218', } method = method or get_one_method() if not method.addressVerificationInformation: method.addressVerificationInformation = _avi _mv = models.MethodValidation(method=method, lineOfBusiness=lineOfBusiness or 'US_CLOUD', contractEntity=contractEntity or 'CONTRACT_US', currencyCode=currencyCode or 'USD') if serialize_format == 'json': data = _mv._obj_to_dict() else: data = _mv._obj_to_xml() resp, _ = SynHTTPClient().request("POST", endpoint, data=data, headers=headers, sanitize=True) if deserialize_format == 'json': _data = resp.json()['papi:methodValidation'] else: _data = xmltodict.parse(resp.data())['ns3:methodValidation'] validation = models.MethodValidation._dict_to_obj(_data) methodValidations[validation.methodValidationId] = validation return validation
def authenticate_v2(url, username=None, password=None, tenant_name=None, tenant_id=None, scoped=False, serialize_format="json", deserialize_format="json"): """Creates auth request body and sends it to the given v2 endpoint. :param str username: OpenStack username :param str password: OpenStack password :param str tenant_name: Name of tenant to which the user belongs :param str tenant_id: Id of the tenant :param bool scoped: Flag to retrieve scoped/unscoped tokens :param str serialize_format: Request body format(json/xml) :param str deserialize_format: Response body format(json/xml) """ headers = {} kwargs = {} password_creds = None if url.endswith('/v2.0/'): url = '{0}tokens'.format(url) elif url.endswith('/v2.0'): url = '{0}/tokens'.format(url) else: url = '{0}/v2.0/tokens'.format(url) headers["Content-Type"] = "application/{0}".format(serialize_format) headers["Accept"] = "application/{0}".format(deserialize_format) kwargs["tenant_name"] = tenant_name kwargs["tenant_id"] = tenant_id password_creds = v2.PasswordCredentials(username=username, password=password) if scoped: request_entity = v2.Auth(tenant_name=tenant_name, tenant_id=tenant_id, password_creds=password_creds) else: request_entity = v2.Auth(password_creds=password_creds) data = request_entity.serialize(serialize_format) try: resp, _ = SynHTTPClient().request("POST", url, headers=headers, data=data, sanitize=True) r = resp.json() except RequestException as e: LOG.debug(e) else: if not r: raise Exception("Failed to authenticate") if r['access'] is None: raise Exception("Failed to parse Auth response Body") return r['access']
def authenticate_v2( url, username=None, password=None, tenant_name=None, tenant_id=None, scoped=False, serialize_format="json", deserialize_format="json", ): """Creates auth request body and sends it to the given v2 endpoint. :param str username: OpenStack username :param str password: OpenStack password :param str tenant_name: Name of tenant to which the user belongs :param str tenant_id: Id of the tenant :param bool scoped: Flag to retrieve scoped/unscoped tokens :param str serialize_format: Request body format(json/xml) :param str deserialize_format: Response body format(json/xml) """ headers = {} kwargs = {} password_creds = None if url.endswith("/v2.0/"): url = "{0}tokens".format(url) elif url.endswith("/v2.0"): url = "{0}/tokens".format(url) else: url = "{0}/v2.0/tokens".format(url) headers["Content-Type"] = "application/{0}".format(serialize_format) headers["Accept"] = "application/{0}".format(deserialize_format) kwargs["tenant_name"] = tenant_name kwargs["tenant_id"] = tenant_id password_creds = v2.PasswordCredentials(username=username, password=password) if scoped: request_entity = v2.Auth(tenant_name=tenant_name, tenant_id=tenant_id, password_creds=password_creds) else: request_entity = v2.Auth(password_creds=password_creds) data = request_entity.serialize(serialize_format) try: resp, _ = SynHTTPClient().request("POST", url, headers=headers, data=data, sanitize=True) r = resp.json() except RequestException as e: LOG.debug(e) else: if not r: raise Exception("Failed to authenticate") if r["access"] is None: raise Exception("Failed to parse Auth response Body") return r["access"]
def download(uri, cache_dir=None): """A simple file downloader. A simple file downloader which returns the absolute path to where the file has been saved. In case of tar files the absolute patch excluding .tar extension is passed. :param str uri: The remote uri of the file :param str cache_dir: The directory name/handle :returns str: Absolute path to the downloaded file """ global temp_dirs global remote_dirs if not cache_dir: cache_dir = tempfile.mkdtemp() temp_dirs.append(cache_dir) remote_dirs.append(cache_dir) log_string = "Remote file location: {}".format(remote_dirs) LOG.debug(log_string) resp, signals = SynHTTPClient().request("GET", uri) os.chdir(cache_dir) saved_umask = os.umask(0o77) fname = uri.split("/")[-1] try: with open(fname, 'w') as fh: fh.write(resp.content) return os.path.abspath(fname) except IOError: LOG.error("IOError in writing the downloaded file to disk.") finally: os.umask(saved_umask)
def get_paymentMethod(methodId, ran=CONF.rax_payment_system.ran, serialize_format='json', deserialize_format='json'): headers = {} headers['accept'] = 'application/%s' % deserialize_format headers['x-auth-token'] = get_token() endpoint = urlparse.urljoin( CONF.syntribos.endpoint, "/v1/accounts/{0}/methods/{1}".format(ran, methodId)) resp, _ = SynHTTPClient().request("GET", endpoint, headers=headers, sanitize=True) m = models.PaymentMethod._dict_to_obj(resp.json()) return m
def create_paymentMethod(methodType, ran=CONF.rax_payment_system.ran, serialize_format='json', deserialize_format='json', model=None, **kwargs): headers = {} headers['accept'] = 'application/%s' % deserialize_format headers['content-type'] = 'application/%s' % serialize_format headers['x-auth-token'] = get_token() endpoint = urlparse.urljoin(CONF.syntribos.endpoint, "/v1/accounts/{0}/methods".format(ran)) if not model: if 'paymentCard' in methodType: _model_class = models.PaymentCardMethod elif 'electronicCheck' in methodType: _model_class = models.ACHMethod elif 'ukDirectDebit' in methodType: _model_class = models.UKDebitMethod elif 'sepa' in methodType: _model_class = models.SEPAMethod model = _model_class(**kwargs) if serialize_format == 'json': data = model._obj_to_dict() else: data = model._obj_to_xml() resp, _ = SynHTTPClient().request("POST", endpoint, data=data, headers=headers, sanitize=True) if deserialize_format == 'json': resp_data = resp.json()['papi:method'] else: resp_data = xmltodict.parse(resp.data())['ns2:method'] return type(model)._dict_to_obj(resp_data)
def delete_paymentMethod(methodId, ran=CONF.rax_payment_system.ran, serialize_format='json', deserialize_format='json'): headers = {} headers['x-auth-token'] = get_token() endpoint = urlparse.urljoin( CONF.syntribos.endpoint, "/v1/accounts/{0}/methods/{1}".format(ran, methodId)) resp, _ = SynHTTPClient().request("DELETE", endpoint, headers=headers, sanitize=True)
def list_voids(ran=CONF.rax_payment_system.ran, serialize_format='json', deserialize_format='json'): headers = {} headers['accept'] = 'application/%s' % deserialize_format headers['x-auth-token'] = get_token() endpoint = urlparse.urljoin(CONF.syntribos.endpoint, "/v1/payments/%s/voids" % get_one_paymentId()) resp, _ = SynHTTPClient().request("GET", endpoint, headers=headers, sanitize=True) if deserialize_format == 'json': _voids = resp.json()['voids']['void'] else: _voids = xmltodict.parse(resp.data())['ns1:voids'] voids = {} for v in _voids: voids[v['id']] = models.Void._dict_to_obj(v) return voids
def authenticate_v3(url, username=None, password=None, user_id=None, domain_id=None, domain_name=None, token=None, project_name=None, project_id=None, scoped=False, serialize_format="json", deserialize_format="json"): """Creates auth request body and sends it to the given v3 endpoint. :param str username: OpenStack username :param str password: OpenStack password :param str user_id: Id of the user :param str domain_name: Name of Domain the user belongs to :param str domain_id: Id of the domain :param str token: An auth token :param str project_name: Name of the project user is part of :param str project_id: Id of the project :param bool scoped: Flag to retrieve scoped/unscoped tokens :param str serialize_format: Request body format(json/xml) :param str deserialize_format: Response body format(json/xml) """ headers = {} kwargs = {} if url.endswith('/v3/'): url = '{0}auth/tokens'.format(url) elif url.endswith('/v3'): url = '{0}/auth/tokens'.format(url) else: url = '{0}/v3/auth/tokens'.format(url) headers["Content-Type"] = "application/json" headers["Accept"] = "application/json" if user_id: domain = None username = None else: domain = v3.Domain(name=domain_name, id_=domain_id) password = v3.Password(user=v3.User( name=username, password=password, id_=user_id, domain=domain)) if token: kwargs = {"token": v3.Token(id_=token), "methods": ["token"]} else: kwargs = {"password": password, "methods": ["password"]} if scoped: if project_id: project_name = None domain = None elif domain is None: domain = v3.Domain(name=domain_name, id_=domain_id) project = v3.Project(name=project_name, id_=project_id, domain=domain) scope = v3.Scope(project=project, domain=domain) else: scope = None request_entity = v3.Auth(identity=v3.Identity(**kwargs), scope=scope) data = request_entity.serialize(serialize_format) try: r, _ = SynHTTPClient().request("POST", url, headers=headers, data=data, sanitize=True) except RequestException as e: LOG.critical(e) else: if not r: raise Exception("Failed to authenticate") return r