def mk_filename(pattern, response, sock, url, overwrite=False): name = get_filename(sock, url) if not name: if isinstance(response.fileid, bytes): name = response.fileid.decode("ascii", "ignore") else: name = response.fileid fs_encoding = sys.getfilesystemencoding() if fs_encoding is None: fs_encoding = "ascii" name = slugify(name) if not name: name = "file" fname = pattern.format(file=name, **serialize_object(response)) if not overwrite and os.path.exists(fname): fname = replacement_filename(fname) dir_ = os.path.abspath(os.path.dirname(fname)) if not os.path.exists(dir_): os.makedirs(dir_) return fname
def mk_filename(pattern, queryresponse, resp, url): name = None url_filename = url.split('/')[-1] if resp: name = resp.headers.get("Content-Disposition", url_filename) if name: name = get_content_disposition(name) if not name: if isinstance(queryresponse.fileid, bytes): name = queryresponse.fileid.decode("ascii", "ignore") else: name = queryresponse.fileid fs_encoding = sys.getfilesystemencoding() if fs_encoding is None: fs_encoding = "ascii" name = slugify(name) if not name: name = "file" fname = pattern.format(file=name, **serialize_object(queryresponse)) return fname
def test_nested_complex_types(): schema = xsd.Schema(load_xml(""" <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tests.python-zeep.org/" targetNamespace="http://tests.python-zeep.org/" elementFormDefault="qualified"> <xsd:element name="container"> <xsd:complexType> <xsd:sequence> <xsd:element name="item" type="tns:item"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:complexType name="item"> <xsd:sequence> <xsd:element name="item_1" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema> """)) container_elm = schema.get_element('{http://tests.python-zeep.org/}container') item_type = schema.get_type('{http://tests.python-zeep.org/}item') instance = container_elm(item=item_type(item_1='foo')) result = serialize_object(instance) assert isinstance(result, dict), type(result) assert isinstance(result['item'], dict), type(result['item']) assert result['item']['item_1'] == 'foo'
def test_serialize_any_array(): custom_type = xsd.Element( etree.QName('http://tests.python-zeep.org/', 'authentication'), xsd.ComplexType( xsd.Sequence([ xsd.Any(max_occurs=2), ]) )) any_obj = etree.Element('{http://tests.python-zeep.org}lxml') etree.SubElement(any_obj, 'node').text = 'foo' obj = custom_type(any_obj) expected = """ <document> <ns0:authentication xmlns:ns0="http://tests.python-zeep.org/"> <ns0:lxml xmlns:ns0="http://tests.python-zeep.org"> <node>foo</node> </ns0:lxml> </ns0:authentication> </document> """ node = etree.Element('document') custom_type.render(node, obj) assert_nodes_equal(expected, node) schema = xsd.Schema() obj = custom_type.parse(node.getchildren()[0], schema=schema) result = serialize_object(obj) assert result == { '_value_1': [any_obj], }
def test_choice_with_sequence_change(): node = load_xml(""" <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tests.python-zeep.org/" elementFormDefault="qualified" targetNamespace="http://tests.python-zeep.org/"> <xsd:element name='ElementName'> <xsd:complexType xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:choice> <xsd:sequence> <xsd:element name="item_1" type="xsd:string"/> <xsd:element name="item_2" type="xsd:string"/> </xsd:sequence> <xsd:sequence> <xsd:element name="item_3" type="xsd:string"/> <xsd:element name="item_4" type="xsd:string"/> </xsd:sequence> <xsd:element name="nee" type="xsd:string"/> </xsd:choice> </xsd:complexType> </xsd:element> </xsd:schema> """) schema = xsd.Schema(node) element = schema.get_element('ns0:ElementName') elm = element(item_1='foo', item_2='bar') assert serialize_object(elm) == { 'item_3': None, 'item_2': 'bar', 'item_1': 'foo', 'item_4': None, 'nee': None } elm.item_1 = 'bla-1' elm.item_2 = 'bla-2' expected = """ <document> <ns0:ElementName xmlns:ns0="http://tests.python-zeep.org/"> <ns0:item_1>bla-1</ns0:item_1> <ns0:item_2>bla-2</ns0:item_2> </ns0:ElementName> </document> """ node = etree.Element('document') element.render(node, elm) assert_nodes_equal(expected, node) value = element.parse(node[0], schema) assert value.item_1 == 'bla-1' assert value.item_2 == 'bla-2'
def test_serialize(): custom_type = xsd.Element( etree.QName('http://tests.python-zeep.org/', 'authentication'), xsd.ComplexType( children=[ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'name'), xsd.String()), xsd.Attribute( etree.QName('http://tests.python-zeep.org/', 'attr'), xsd.String()), xsd.ListElement( etree.QName('http://tests.python-zeep.org/', 'items'), xsd.ComplexType( children=[ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'x'), xsd.String()), xsd.Element( etree.QName('http://tests.python-zeep.org/', 'y'), xsd.ComplexType( children=[ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'x'), xsd.String()), ] ) ) ] ), max_occurs=2 ) ] )) obj = custom_type( name='foo', attr='x', items=[ {'x': 'bla', 'y': {'x': 'deep'}}, {'x': 'foo', 'y': {'x': 'deeper'}}, ]) result = serialize_object(obj) assert result == { 'name': 'foo', 'attr': 'x', 'items': [ {'x': 'bla', 'y': {'x': 'deep'}}, {'x': 'foo', 'y': {'x': 'deeper'}}, ] }
def test_serialize_nested_complex_type(): custom_type = xsd.Element( etree.QName('http://tests.python-zeep.org/', 'authentication'), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'items'), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'x'), xsd.String()), xsd.Element( etree.QName('http://tests.python-zeep.org/', 'y'), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'x'), xsd.String()), ]) ) ) ]) ), max_occurs=2 ) ]) )) obj = custom_type( items=[ {'x': 'bla', 'y': {'x': 'deep'}}, {'x': 'foo', 'y': {'x': 'deeper'}}, ]) assert len(obj.items) == 2 obj.items[0].x == 'bla' obj.items[0].y.x == 'deep' obj.items[1].x == 'foo' obj.items[1].y.x == 'deeper' result = serialize_object(obj) assert result == { 'items': [ {'x': 'bla', 'y': {'x': 'deep'}}, {'x': 'foo', 'y': {'x': 'deeper'}}, ] }
def serialize(self, *args, **kwargs): value = self.body(*args, **kwargs) headers = { 'Content-Type': self.content_type } data = '' if self.content_type == 'application/x-www-form-urlencoded': items = serialize_object(value) data = six.moves.urllib.parse.urlencode(items) elif self.content_type == 'text/xml': document = etree.Element('root') self.body.render(document, value) data = etree_to_string(document.getchildren()[0]) return SerializedMessage( path=self.operation.location, headers=headers, content=data)
def issue_credit(self, order, reference_number, amount, currency): try: client = Client(self.soap_api_url, wsse=UsernameToken(self.merchant_id, self.transaction_key)) credit_service = { 'captureRequestID': reference_number, 'run': 'true', } purchase_totals = { 'currency': currency, 'grandTotalAmount': unicode(amount), } response = client.service.runTransaction( merchantID=self.merchant_id, merchantReferenceCode=order.number, orderRequestToken=reference_number, ccCreditService=credit_service, purchaseTotals=purchase_totals) request_id = response.requestID ppr = self.record_processor_response(serialize_object(response), transaction_id=request_id, basket=order.basket) except: msg = 'An error occurred while attempting to issue a credit (via CyberSource) for order [{}].'.format( order.number) logger.exception(msg) raise GatewayError(msg) if response.decision == 'ACCEPT': return request_id else: raise GatewayError( 'Failed to issue CyberSource credit for order [{order_number}]. ' 'Complete response has been recorded in entry [{response_id}]'. format(order_number=order.number, response_id=ppr.id))
def get_power_unit(start_from, energy_carrier, datum='1900-01-01 00:00:00.00000', limit=API_MAX_DEMANDS): """Get Stromerzeugungseinheit from API using GetGefilterteListeStromErzeuger. Parameters ---------- start_from : int Skip first entries. datum: String the starting datestring to retrieve data, can be used for updating a data set limit : int Number of power unit to get (default: 2000) """ power_unit = pd.DataFrame() # status = 'InBetrieb' # power = 30 try: c = client_bind.GetGefilterteListeStromErzeuger( apiKey=api_key, marktakteurMastrNummer=my_mastr, # einheitBetriebsstatus=status, startAb=start_from, energietraeger=energy_carrier, limit=limit #bruttoleistungGroesser=power #datumAb = datum ) s = serialize_object(c) power_unit = pd.DataFrame(s['Einheiten']) power_unit.index.names = ['lid'] # power_unit['db_offset'] = [i for i in range(start_from, start_from+len(power_unit))] power_unit['version'] = get_data_version() power_unit['timestamp'] = str(datetime.now()) return [start_from, power_unit] except Exception as e: log.info(e)
def test_serialize_simple(): custom_type = xsd.Element( etree.QName("http://tests.python-zeep.org/", "authentication"), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName("http://tests.python-zeep.org/", "name"), xsd.String(), ), xsd.Attribute( etree.QName("http://tests.python-zeep.org/", "attr"), xsd.String(), ), ])), ) obj = custom_type(name="foo", attr="x") assert obj.name == "foo" assert obj.attr == "x" result = serialize_object(obj) assert result == {"name": "foo", "attr": "x"}
def test_serialize_simple(): custom_type = xsd.Element( etree.QName('http://tests.python-zeep.org/', 'authentication'), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'name'), xsd.String()), xsd.Attribute( etree.QName('http://tests.python-zeep.org/', 'attr'), xsd.String()), ]))) obj = custom_type(name='foo', attr='x') assert obj.name == 'foo' assert obj.attr == 'x' result = serialize_object(obj) assert result == { 'name': 'foo', 'attr': 'x', }
def push(): entities = request.get_json() return_entities = [] if not isinstance(entities, list): entities = [entities] for entity in entities: if os.environ.get('transit_decode', 'false').lower() == "true": rootlogger.info("transit_decode is set to True.") entity = typetransformer.transit_decode(entity) rootlogger.info("Finished creating request: " + str(entity)) response = do_soap(entity, client) serialized_response = helpers.serialize_object(response) return_entities.extend(serialized_response) rootlogger.info("Prosessed " + str(len(serialized_response)) + " Entities") return Response(response=json.dumps(return_entities, default=typetransformer.json_serial), mimetype='application/json')
def getEquations(q): while True: forecast_triplets = q.get() equations = awdb.service.getForecastEquationsMultiple(forecast_triplets) serialized_equations = helpers.serialize_object(equations) for trip in forecast_triplets: fcst_json = [] for eq in serialized_equations: if eq and eq['stationTriplet'] == trip: fcst_json.extend([eq]) makedirs(path.join(this_dir,'frcstEq'), exist_ok=True) fileName = path.join(this_dir,'frcstEq', trip.replace(':','_')) with open(fileName + '.json',"w") as json_out: json_out.write(json.dumps(fcst_json)) # for equation in serialized_equations: # makedirs(path.join(this_dir,'frcstEq'), exist_ok=True) # fileName = path.join(this_dir,'frcstEq', # equation['stationTriplet'].replace(':','_')) # with open(fileName + '.json',"w") as json_out: # json_out.write(json.dumps(equation)) q.task_done()
def submit_relays_file(self, relays_file_uri): """ Send information about relays in a file :param str relays_file_uri: A link pointing to a XLSX file with information about relays Usage:: response = api.relays.submit_relays_file( 'http://spreadsheetpage.com/downloads/xl/worksheet%20functions.xlsx' ) where relays_file_uri is the URI to a XLSX file :returns: The response with the RelaysFileId for the file. """ relays_file_request = self.api.factory.RelaysFileIntegrationRequest( relays_file_uri) response = self.api.client.service.SubmitRelaysFile( headerMessage=self.api.header, relaysFileRequest=relays_file_request) return serialize_object(response, dict)
def getSiteData(q): while True: site = q.get() getUpdt = awdb.service.getDataInsertedOrUpdatedSince chunkData = getUpdt(site, sensor, 1, None, duration, False, sDate, eDate, updt_since, True) for siteData in chunkData: serialized_data = helpers.serialize_object(siteData) if serialized_data['dataContentList']: fileName = path.join(this_dir, sensor, siteData.stationTriplet.replace(':', '_')) with open(fileName + '.json', "r") as f: json_in = json.load(f) endDate = dt.strptime(json_in['endDate'], "%Y-%m-%d %H:%M:%S") for updt in serialized_data['dataContentList']: time_stamp_str = updt['timestamp'] time_stamp = dt.strptime(time_stamp_str, "%Y-%m-%d %H:%M:%S") endDateTemp = dt.strptime(json_in['endDate'], "%Y-%m-%d %H:%M:%S") value = float(updt['value']) time_delta = (time_stamp - endDate).days if time_delta <= 0: json_in['values'][time_delta - 1] = value elif abs(time_stamp - endDateTemp).days == 1: json_in['endDate'] = time_stamp_str json_in['values'].extend([value]) else: nans = abs(time_stamp - endDateTemp).days - 1 json_in['endDate'] = time_stamp_str json_in['values'].extend(nans * [None]) json_in['values'].extend([value]) with open(fileName + '.json', "w") as f: json.dump(json_in, f) q.task_done()
def get_order_question_list(self, **order_question_filter): """ Return the list of questions about orders with the specified criteria :param order_question_filter: The keywords for the filter ``orderQuestionFilter``: - BeginCreationDate - BeginModificationDate - EndCreationDate - EndModificationDate - StatusList - DiscussionStateFilter can have the values - All - Open - Closed - NotProcessed Example:: response = api.get_order_question_list( StatusList={'DiscussionStateFilter': 'Open'}, BeginCreationDate='2019-01-01' ) :returns: An OrderQuestionListMessage dictionary. .. note:: A date is mandatory in the query. """ order_question_filter = self.api.factory.OrderQuestionFilter( **order_question_filter) response = self.api.client.service.GetOrderQuestionList( headerMessage=self.api.header, orderQuestionFilter=order_question_filter) return serialize_object(response, dict)
def list_incidents_command(client: Client, args: dict, saved_report_id: str) -> Tuple[str, dict, dict]: if not saved_report_id: raise ValueError( 'Missing saved report ID. Configure it in the integration instance settings.' ) creation_date = parse_date_range(args.get('creation_date', '1 day'))[0] raw_incidents = client.service.incidentList( savedReportId=saved_report_id, incidentCreationDateLaterThan=creation_date) human_readable: str entry_context: dict = {} raw_response: dict = {} if raw_incidents: serialized_incidents: dict = helpers.serialize_object(raw_incidents) incidents_ids_list = serialized_incidents.get('incidentId') if incidents_ids_list: raw_response = serialized_incidents incidents = [{ 'ID': str(incident_id) } for incident_id in incidents_ids_list] human_readable = tableToMarkdown('Symantec DLP incidents', incidents, removeNull=True) entry_context = { 'SymantecDLP.Incident(val.ID && val.ID == obj.ID)': incidents } else: human_readable = 'No incidents found.' else: human_readable = 'No incidents found.' return human_readable, entry_context, raw_response
def sync_transactions(client): schema = load_schema("transactions") singer.write_schema("transactions", schema, ["Id"]) dateFrom = get_start("transactions") - timedelta( days=CONFIG['validation_window']) dateTo = datetime.now(timezone.utc) start = dateFrom offset = 0 finalRow = None # handle batches by number of days and number of rows while start < dateTo: end = start + timedelta(days=MAX_DAYS) if (end > dateTo): end = dateTo resp = client.service.getTransactionList(dStartDate=start, dEndDate=end, iOffset=offset, iLimit=BATCH_SIZE, sDateType="transaction") if (resp.body.getTransactionListCountReturn.iRowsReturned > 0): for t in resp.body.getTransactionListReturn: t = helpers.serialize_object(t) if t['aTransactionParts'] != None: t['aTransactionParts'] = t['aTransactionParts'] singer.write_record("transactions", map_type(t)) finalRow = t if (offset + resp.body.getTransactionListCountReturn.iRowsReturned ) < resp.body.getTransactionListCountReturn.iRowsAvailable: offset += resp.body.getTransactionListCountReturn.iRowsReturned else: start = end offset = 0 if finalRow != None: utils.update_state(STATE, "transactions", finalRow['dTransactionDate'])
def list_device_pools(): cucm = connect_to_cucm(username=session.get('cucm_username'), password=session.get('cucm_password'), cucm_ip=session.get('cucm_ip')) returned_tags = { 'name': '', 'dateTimeSettingName': '', 'callManagerGroupName': '', 'mediaResourceListName': '', 'regionName': '', 'networkLocale': '', 'srstName': '', 'locationName': '', 'mobilityCssName': '', 'physicalLocationName': '', 'deviceMobilityGroupName': '' } returned_data = cucm.listDevicePool(searchCriteria={'name': '%'}, returnedTags=returned_tags) returned_dict = serialize_object(returned_data['return']['devicePool']) filtered_data = [] for device_pool in returned_dict: filtered_pool = {} for k, v in device_pool.items(): if device_pool[k] is not None: from collections import OrderedDict if isinstance(device_pool[k], OrderedDict): filtered_pool[k] = v['_value_1'] else: filtered_pool[k] = v filtered_data.append(filtered_pool) return filtered_data
def run(self): users = self.client.service.GetUsers( self.source_definition['ibabs_sitename']) if users.Users: total_count = 0 for user in users.Users.iBabsUserBasic: identifier = user['UniqueId'] user_details = self.client.service.GetUser( self.source_definition['ibabs_sitename'], identifier) cached_path = 'GetUser/Sitename=%s/UserId=%s' % ( self.source_definition['ibabs_sitename'], identifier) profile = serialize_object(user_details.User.PublicProfile, dict) image = user_details.User.PublicProfile.Picture if image: profile['Picture'] = base64.encodestring(image).decode( 'ascii') yield 'application/json', json.dumps( profile), None, 'ibabs/' + cached_path total_count += 1 log.info("[%s] Extracted total of %s ibabs persons" % (self.source_definition['index_name'], total_count)) elif users.Message == 'No users found!': log.info('[%s] No ibabs persons were found' % self.source_definition['index_name']) else: log.warning('[%s] SOAP service error: %s' % (self.source_definition['index_name'], users.Message))
def create_header_message(self, data): messages_factory = self.client.type_factory( "http://schemas.datacontract.org/2004/07/" "Cdiscount.Framework.Core.Communication.Messages") # Set default values if they are not provided if "Context" in data: data["Context"].setdefault("SiteID", DEFAULT_SITE_ID) data["Context"].setdefault("CatalogID", DEFAULT_CATALOG_ID) else: data["Context"] = { "SiteID": DEFAULT_SITE_ID, "CatalogID": DEFAULT_CATALOG_ID, } if "Security" in data: data["Security"].setdefault("TokenId", self.token) else: data["Security"] = {"UserName": "", "TokenId": self.token} if "Version" not in data: data["Version"] = DEFAULT_VERSION return serialize_object(messages_factory.HeaderMessage(**data), dict)
def get_fulfillment_order_list_to_supply(self, **request): """ To ask for fulfillment on demand order lines to supply. :param str OrderReference: :param str ProductEan: :param str Warehouse: name of warehouses where products are stored: - 'CEM' - 'ANZ' - 'SMD' Example:: response = api.fulfillment.get_fulfillment_order_list_to_supply( ProductEan='2009863600561' ) :return: fulfillment on demand order lines to supply answering the search criterion. """ references = self.api.factory.FulfilmentOnDemandOrderLineFilter( **request) response = self.api.client.service.GetFulfilmentActivationReportList( headerMessage=self.api.header, request=references) return serialize_object(response, dict)
def array_of(self, type_name, sequence): """ Cast the sequence into an array of the given type. The arrays are defined in the XSD file (cf http://schemas.microsoft.com/2003/10/Serialization/Arrays) (cf https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-ipamm/38d7c101-385d-4180-bb95-983955f41e19) ? """ valid_type_names = ( "int", "string", "long", "KeyValueOfstringArrayOfstringty7Ep6D1", "KeyValueOfintstring", ) if type_name not in valid_type_names: raise TypeError( "Invalid type_name. " "Please choose between {}".format(valid_type_names)) array = getattr(self.arrays_factory, "ArrayOf{}".format(type_name))(sequence) return serialize_object(array, dict)
def test_serialize_simple(): custom_type = xsd.Element( etree.QName('http://tests.python-zeep.org/', 'authentication'), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'name'), xsd.String()), xsd.Attribute( etree.QName('http://tests.python-zeep.org/', 'attr'), xsd.String()), ]) )) obj = custom_type(name='foo', attr='x') assert obj.name == 'foo' assert obj.attr == 'x' result = serialize_object(obj) assert result == { 'name': 'foo', 'attr': 'x', }
def fetchservices(self): #Creating a Multi thread pool to fetch services from each node simultaneously services_thread_pool = ThreadPoolExecutor(max_workers=8) allServicesresults = { keys: services_thread_pool.submit(self.setupserviceconnection, keys) for keys in self.nodedict.keys() } for node, services in allServicesresults.items(): #changing datatype to List from zeep object type try: temp_zeep_service_list = list( services.result()['ServiceInfoList']) temp_services = [ serialize_object(value) for value in temp_zeep_service_list ] temp_df = DataFrame.from_dict(temp_services, orient='columns') temp_df['ReasonCodeString'] = temp_df[ 'ReasonCodeString'].fillna('Activiated', inplace=True) #temp_df.drop('ReasonCode',axis=1,inplace=True) self.nodedict[node].services = temp_df except: self.nodedict[node].services = "Cannot Find Services" return None
def post(self, content): response = self.client.service.insere(content) return zeep_helper.serialize_object(response)
def stock_quote(symbol): app.logger.info('stock quote for %s', symbol) # query param access: request.args.get('q') return jsonify(serialize_object(client.service.GetQuote(StockSymbol=symbol, LicenseKey=0)))
def request_apple_pay_authorization(self, basket, billing_address, payment_token): """ Authorizes an Apple Pay payment. For details on the process, see the CyberSource Simple Order API documentation at https://www.cybersource.com/developers/integration_methods/apple_pay/. Args: basket (Basket) billing_address (BillingAddress) payment_token (dict) Returns: HandledProcessorResponse Raises: GatewayError """ try: client = Client(self.soap_api_url, wsse=UsernameToken(self.merchant_id, self.transaction_key)) card_type = APPLE_PAY_CYBERSOURCE_CARD_TYPE_MAP[ payment_token['paymentMethod']['network'].lower()] bill_to = { 'firstName': billing_address.first_name, 'lastName': billing_address.last_name, 'street1': billing_address.line1, 'street2': billing_address.line2, 'city': billing_address.line4, 'state': billing_address.state, 'postalCode': billing_address.postcode, 'country': billing_address.country.iso_3166_1_a2, 'email': basket.owner.email, } purchase_totals = { 'currency': basket.currency, 'grandTotalAmount': str(basket.total_incl_tax), } encrypted_payment = { 'descriptor': 'RklEPUNPTU1PTi5BUFBMRS5JTkFQUC5QQVlNRU5U', 'data': base64.b64encode(json.dumps(payment_token['paymentData'])), 'encoding': 'Base64', } card = { 'cardType': card_type, } auth_service = { 'run': 'true', } capture_service = { 'run': 'true', } # Enable Export Compliance for SDN validation, amongst other checks. # See https://www.cybersource.com/products/fraud_management/export_compliance/ export_service = { 'run': 'true', } item = [{ 'id': index, 'productCode': line.product.get_product_class().slug, 'productName': clean_field_value(line.product.title), 'quantity': line.quantity, 'productSKU': line.stockrecord.partner_sku, 'taxAmount': str(line.line_tax), 'unitPrice': str(line.unit_price_incl_tax), } for index, line in enumerate(basket.all_lines())] response = client.service.runTransaction( merchantID=self.merchant_id, merchantReferenceCode=basket.order_number, billTo=bill_to, purchaseTotals=purchase_totals, encryptedPayment=encrypted_payment, card=card, ccAuthService=auth_service, ccCaptureService=capture_service, exportService=export_service, paymentSolution='001', item=item, ) except: msg = 'An error occurred while authorizing an Apple Pay (via CyberSource) for basket [{}]'.format( basket.id) logger.exception(msg) raise GatewayError(msg) request_id = response.requestID ppr = self.record_processor_response(serialize_object(response), transaction_id=request_id, basket=basket) if response.decision == 'ACCEPT': currency = basket.currency total = basket.total_incl_tax transaction_id = request_id return HandledProcessorResponse( transaction_id=transaction_id, total=total, currency=currency, card_number='Apple Pay', card_type=CYBERSOURCE_CARD_TYPE_MAP.get(card_type)) else: msg = ( 'CyberSource rejected an Apple Pay authorization request for basket [{basket_id}]. ' 'Complete response has been recorded in entry [{response_id}]') msg = msg.format(basket_id=basket.id, response_id=ppr.id) logger.warning(msg) raise GatewayError(msg)
def test_serialize_nested_complex_type(): custom_type = xsd.Element( etree.QName('http://tests.python-zeep.org/', 'authentication'), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'items'), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName('http://tests.python-zeep.org/', 'x'), xsd.String()), xsd.Element( etree.QName('http://tests.python-zeep.org/', 'y'), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName( 'http://tests.python-zeep.org/', 'x'), xsd.String()), ]))) ])), max_occurs=2) ]))) obj = custom_type(items=[ { 'x': 'bla', 'y': { 'x': 'deep' } }, { 'x': 'foo', 'y': { 'x': 'deeper' } }, ]) assert len(obj.items) == 2 obj.items[0].x == 'bla' obj.items[0].y.x == 'deep' obj.items[1].x == 'foo' obj.items[1].y.x == 'deeper' result = serialize_object(obj) assert result == { 'items': [ { 'x': 'bla', 'y': { 'x': 'deep' } }, { 'x': 'foo', 'y': { 'x': 'deeper' } }, ] }
def default(o): if isinstance(o, (datetime.date, datetime.datetime)): return o.isoformat() WSDL = 'http://lite.realtime.nationalrail.co.uk/OpenLDBWS/wsdl.aspx?ver=2017-10-01' if LDB_TOKEN == '': raise Exception( "Please configure your OpenLDBWS token in getDepartureBoardExample!") history = HistoryPlugin() client = Client(wsdl=WSDL, plugins=[history]) header = xsd.Element( '{http://thalesgroup.com/RTTI/2013-11-28/Token/types}AccessToken', xsd.ComplexType([ xsd.Element( '{http://thalesgroup.com/RTTI/2013-11-28/Token/types}TokenValue', xsd.String()), ])) header_value = header(TokenValue=LDB_TOKEN) res = client.service.GetDepBoardWithDetails(numRows=10, crs='CLS', _soapheaders=[header_value]) input_dict = helpers.serialize_object(res) db_as_str = json.dumps(input_dict, default=default) services = res.trainServices.service sys.stdout.write(db_as_str) sys.stdout.flush() sys.exit(0)
def sql_device(service, dn): sql_statement = "select d.name from device as d, numplan as n, devicenumplanmap as dnpm " \ "where dnpm.fkdevice = d.pkid and dnpm.fknumplan = n.pkid and d.tkclass IN (1, 254) and n.DNOrPattern='"+dn+"' " axl_resp = service.executeSQLQuery(sql=sql_statement) if not axl_resp["return"]: pass else: return [OrderedDict((element.tag, element.text) for element in row) for row in serialize_object(axl_resp)["return"]["row"]]
def transform(response): return transform_dts(serialize_object(response))
def update_job_from_ad(job, ad, homepage, defaults=None, import_categories=False): defaults = defaults or {} cleaner = Cleaner( tags=constants.BLEACH_ALLOWED_TAGS, attributes=constants.BLEACH_ALLOWED_ATTRIBUTES, strip=True, ) job.homepage = homepage job.job_number = ad["jobNumber"] job.title = ad["jobTitle"].strip() job.is_published = ad[ "postingTargetStatus"] == POSTING_TARGET_STATUS_PUBLISHED job.posting_start_date = ad["postingStartDate"] job.posting_end_date = ad["postingEndDate"] job.expected_start_date = ad["expectedStartDate"] job.application_url_query = urlsplit(ad["applicationUrl"]).query for configurable_field in ad["configurableFields"]["configurableField"]: try: target_field, parser = JOB_CONFIGURABLE_FIELDS_MAPPING[ configurable_field["label"]] except KeyError: pass else: setattr( job, target_field, parser( configurable_field["criteria"]["criterion"][0]["value"]), ) # The description is conveyed in 'custom' fields, where the label acts as a subheading description = [] custom_fields = sorted(ad["customFields"]["customField"], key=lambda x: x["order"]) for i, custom_field in enumerate(custom_fields): if custom_field["value"]: description.append(f"<h3>{custom_field['label'].strip()}</h3>") description.append(cleaner.clean(custom_field["value"])) if i == 0: soup = BeautifulSoup(custom_field["value"], "html.parser") if soup.find("p"): text = soup.find("p").text else: # The value is plaintext, or at least contains no p tags text = strip_tags(custom_field["value"]) job.short_description = cleaner.clean(" ".join(text.split())) job.description = "\n".join(description) # CustomLovs and StandardLovs for lov in ad["customLovs"]["customLov"] + ad["standardLovs"][ "standardLov"]: try: target_field, parser = JOB_LOVS_MAPPING[lov["label"]] except KeyError: pass else: # Use insert subcategory parser if command specifies `--import_categories` if (parser is job_subcategory_parser) and import_categories: parser = job_subcategory_insert_parser setattr(job, target_field, parser(lov["criteria"]["criterion"][0]["label"])) for k, v in defaults.items(): setattr(job, k, v) # Get location data if ad["jobLocations"]: # Unit tests provide dicts, the Zeep library provides Zeep objects. We convert # Zeep instance to native Python OrderedDict, so we can use obj.get() for # everything. NB this also serializes Python dicts to OrderedDicts. location = serialize_object(ad["jobLocations"]["jobLocation"][0]) for ad_attr, job_attr in [ ("location_name", "location_name"), ("streetNumber", "location_street_number"), ("street", "location_street"), ("city", "location_city"), ("region", "location_region"), ("country", "location_country"), ("zipCode", "location_postcode"), ("latitude", "location_lat"), ("longitude", "location_lon"), ]: value = location.get(ad_attr) if value: setattr(job, job_attr, value) job.organisation = " — ".join([ organisation["value"] for organisation in sorted( ad["organizations"]["organization"], key=lambda organisation: organisation["level"], ) ]) job.save() return job
def test_serialize_nested_complex_type(): custom_type = xsd.Element( etree.QName("http://tests.python-zeep.org/", "authentication"), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName("http://tests.python-zeep.org/", "items"), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName("http://tests.python-zeep.org/", "x"), xsd.String(), ), xsd.Element( etree.QName("http://tests.python-zeep.org/", "y"), xsd.ComplexType( xsd.Sequence([ xsd.Element( etree.QName( "http://tests.python-zeep.org/", "x", ), xsd.String(), ) ])), ), ])), max_occurs=2, ) ])), ) obj = custom_type(items=[{ "x": "bla", "y": { "x": "deep" } }, { "x": "foo", "y": { "x": "deeper" } }]) assert len(obj.items) == 2 obj.items[0].x == "bla" obj.items[0].y.x == "deep" obj.items[1].x == "foo" obj.items[1].y.x == "deeper" result = serialize_object(obj) assert result == { "items": [{ "x": "bla", "y": { "x": "deep" } }, { "x": "foo", "y": { "x": "deeper" } }] }
response = service.getDeviceProfile(name=ucmObject) print("="*75) print("Sample Output") print("="*75) ##Silly Cisco, you should have consistant output ##The vendor config from phone devices needs special handling ##We don't typically set these values during a MACD, so we will ignore them try: del response["return"]["phone"]["vendorConfig"] except: pass print(response) ucmDict = helpers.serialize_object(response) print("="*50) print("Save Dictionary as JSON") print("="*50) filename = ucmObjectType + "-Template.json" with open(filename, 'w') as file: json.dump(ucmDict, file, indent=4, separators=(',', ': ')) print("="*50) print("Dictionary Post Helper Conversion") print("="*50)
def delete(self, content): response = self.client.service.remove(content) return zeep_helper.serialize_object(response)
def put(self, pk, content): content['id'] = pk response = self.client.service.altera(content) return zeep_helper.serialize_object(response)
def __call__(self, request): """Makes a SoftLayer API call against the SOAP endpoint. :param request request: Request object """ zeep_settings = Settings(strict=False, xml_huge_tree=True) zeep_transport = Transport(cache=SqliteCache(timeout=86400)) client = Client(f"{self.endpoint_url}/{request.service}?wsdl", settings=zeep_settings, transport=zeep_transport, plugins=[self.history]) # print(client.wsdl.dump()) # print("=============== WSDL ==============") # Must define headers like this because otherwise the objectMask header doesn't work # because it isn't sent in with a namespace. xsd_userauth = xsd.Element( f"{{{self.soapns}}}authenticate", xsd.ComplexType([ xsd.Element(f'{{{self.soapns}}}username', xsd.String()), xsd.Element(f'{{{self.soapns}}}apiKey', xsd.String()) ])) # factory = client.type_factory(f"{self.soapns}") the_mask = client.get_type(f"{{{self.soapns}}}SoftLayer_ObjectMask") xsd_mask = xsd.Element(f"{{{self.soapns}}}SoftLayer_ObjectMask", the_mask) # Object Filter filter_type = client.get_type( f"{{{self.soapns}}}{request.service}ObjectFilter") xsd_filter = xsd.Element( f"{{{self.soapns}}}{request.service}ObjectFilter", filter_type) # Result Limit xsd_resultlimit = xsd.Element( f"{{{self.soapns}}}resultLimit", xsd.ComplexType([ xsd.Element('limit', xsd.String()), xsd.Element('offset', xsd.String()), ])) # Might one day want to support unauthenticated requests, but for now assume user auth. headers = [ xsd_userauth(username=request.transport_user, apiKey=request.transport_password), ] if request.limit: headers.append( xsd_resultlimit(limit=request.limit, offset=request.offset)) if request.mask: headers.append(xsd_mask(mask=request.mask)) if request.filter: # The ** here forces python to treat this dict as properties headers.append(xsd_filter(**request.filter)) if request.identifier: init_param = f"{request.service}InitParameters" init_paramtype = client.get_type(f"{{{self.soapns}}}{init_param}") xsdinit_param = xsd.Element(f"{{{self.soapns}}}{init_param}", init_paramtype) # Might want to check if its an id or globalIdentifier at some point, for now only id. headers.append(xsdinit_param(id=request.identifier)) # NEXT Add params... maybe try: method = getattr(client.service, request.method) except AttributeError as ex: message = f"{request.service}::{request.method}() does not exist in {self.soapns}{request.service}?wsdl" raise exceptions.TransportError(404, message) from ex result = method(_soapheaders=headers) # NEXT GET A WAY TO FIND TOTAL ITEMS try: method_return = f"{request.method}Return" serialize = serialize_object(result) if serialize.get('body'): return serialize['body'][method_return] else: # Some responses (like SoftLayer_Account::getObject) don't have a body? return serialize except KeyError as ex: message = f"Error serializeing response\n{result}\n{ex}" raise exceptions.TransportError(500, message)
def get_charity_network( charity_number: CharityIDType = 1085314, # TATE branches: int = 0, # FOUNDATION client: Optional[Client] = None, api_key: Optional[str] = CHARITY_COMMISSION_API_KEY, test_name: str = None, *args, **kwargs, ) -> Optional[Graph]: """Query Charity Commission API for board interlock network.""" g: Graph = Graph() if not client: client = get_client(api_key_value=api_key) assert isinstance(charity_number, int) try: charity_data = client.service.GetCharityByRegisteredCharityNumber( registeredCharityNumber=charity_number, ) except Fault: logger.error(f"Fault error pulling for {charity_number}") return None if not charity_data: logger.warning(f"No data on charity {charity_number}") if not test_name: logger.warning("No records from Charities Commision on " "this Arts Council Instition") return None charity_name = charity_data["CharityName"] if test_name: # Test the name of the queried Charity is the intended try: assert charity_name == test_name except AssertionError: logger.exception('Referral test name "{0}" is different from ' '"{1}" which is associated ' "with charity_number {2}".format( test_name.strip(), charity_name.strip(), charity_number)) g.add_node( charity_number, name=charity_name.strip(), bipartite=0, # Add a charity/company attribute kind=CHARITY_NETWORK_KINDS[0], data=serialize_object(charity_data), ) logger.debug(charity_name) for subsidiary in range(charity_data["SubsidiaryNumber"] + 1): trustees = client.service.GetCharityTrustees( registeredCharityNumber=charity_number, subsidiaryNumber=subsidiary) if not trustees: logger.warning("No trustees for charity {0} ({1} " "subsidiary {2})".format(charity_name.strip(), charity_number, subsidiary)) continue for trustee in trustees: logger.debug("{0} {1} {2}".format( charity_number, trustee["TrusteeName"], trustee["TrusteeNumber"], )) g.add_node( trustee["TrusteeNumber"], name=trustee["TrusteeName"].strip(), bipartite=1, kind=CHARITY_NETWORK_KINDS[1], data=serialize_object(trustee), ) g.add_edge(charity_number, trustee["TrusteeNumber"]) if branches and trustee["RelatedCharitiesCount"]: for charity in trustee["RelatedCharities"]: # related_charity_number = \ # get_charity_number(charity['CharityName'], # charity['CharityNumber'], # client=client) if charity["CharityNumber"] not in g.nodes: subgraph = get_charity_network( charity["CharityNumber"], branches - 1, client=client, # type: ignore[misc] test_name=charity["CharityName"], *args, **kwargs, ) assert is_bipartite(subgraph) g = compose(g, subgraph) assert is_bipartite(g) return g