I have a soap 1.1/1.2 web service I'm trying to access using suds.
Unfortunately the service puts authentication token in response soap header.
Is it possible to access the header somehow?
I know one can set a custom soap header in the request, but that's not what I'm looking for.
I've been using the (still maintained) suds-jurko branch, and ran into trouble because client.last_received() was removed early after it was forked. So I had to figure out an alternative way to access the headers.
Fortunately, you can use a message plugin to store the parsed document, and then later access the headers via the plugin. For added convenience, instead of working with raw values from the xml document, you can process the headers based on the service-method, to get a correctly typed/structured value.
from suds.plugin import MessagePlugin
class HeaderPlugin(MessagePlugin):
def __init__(self):
self.document = None
def parsed(self, context):
self.document = context.reply
def get_headers(self, method):
method = method.method
binding = method.binding.output
rtypes = binding.headpart_types(method, False)
envns = ('SOAP-ENV', 'http://schemas.xmlsoap.org/soap/envelope/')
soapenv = self.document.getChild('Envelope', envns)
soapheaders = soapenv.getChild('Header', envns)
nodes = soapheaders.children
if len(nodes):
resolved = rtypes[0].resolve(nobuiltin=True)
return binding.unmarshaller().process(nodes[0], resolved)
return None
usage:
from suds.client import Client
hp = HeaderPlugin()
client = Client(wsdl, plugins=[hp])
response = client.service.LoremIpsum()
headers = hp.get_headers(client.service.LoremIpsum)
example output:
>>> headers
(AuthenticationResponseHeader){
sessionKey = "a631cd00-c6be-416f-9bd3-dbcd322e0848"
validUntil = 2030-01-01 01:01:01.123456+01:00
}
>>> headers.validUntil
datetime.datetime(2030, 1, 1, 1, 1, 1, 123456, tzinfo=<suds.sax.date.FixedOffsetTimezone object at 0x7f7347856be0>)
You can do something like
print client.last_received().getChild("soap:Envelope").getChild("soap:Header")
.getChild("ResponseHeader").getChild("resultCode").getText()
The above reads a field resultCode in the soap header. You have to do this for each field. This was a back door left to read headers as much as i know.
For details look at soap headers with suds
The Towr's class plugin works well, but it has a problem when do you have more then one obj in Soapheader Response.
His code get only the first object.
Here is the code to improvement the Towr's class:
class HeaderPlugin(MessagePlugin):
def __init__(self):
self.document = None
def parsed(self, context):
self.document = context.reply
def get_headers(self, method):
Result = {}
method = method.method
binding = method.binding.output
SHeaderElem = binding.headpart_types(method, False)
envns = ('SOAP-ENV', 'http://schemas.xmlsoap.org/soap/envelope/')
soapenv = self.document.getChild('Envelope', envns)
soapheaders = soapenv.getChild('Header', envns)
SHeaderNodes = soapheaders.children
for Elem in SHeaderElem:
for Node in SHeaderNodes:
if(Node.name == Elem.name):
ElemRes = Elem.resolve(nobuiltin=True)
NodeRes = binding.unmarshaller().process(Node, ElemRes)
Result[Elem.name] = NodeRes
return Result
#
To understand better, see the example.
If do you receive this Soap Response:
<soap-env:Envelope xmlns:eb="http://www.ebxml.org/namespaces/messageHeader" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext">
<soap-env:Header>
<eb:MessageHeader eb:version="1.0" soap-env:mustUnderstand="1">
<!-- -->
</eb:MessageHeader>
<wsse:Security>
<!-- -->
</wsse:Security>
</soap-env:Header>
<soap-env:Body>
<!-- -->
</soap-env:Body>
</soap-env:Envelope>
The function get_headers will return a dict like this:
SoapHeadersResp = {'MessageHeader':MessageHeaderObj, 'Security':SecurityObj}
To use this class just follow the same steps that Towr said, replacing his HeaderPlugin class with this one.
Related
I want to get the parameters sent to my rest api
what I want is to obtain the parameters that to use them consume another api and return the response of the third party api
but in name and comic i get None
http://127.0.0.1:8000/searchComics/
{name:"3-D Man","comics":12}
this is my view
class MarvelApi(APIView):
def get(self, request):
private_key = "88958f2d87bd2c0c2fa07b7ea654bcdf9f0389b3"
public_key = "8d415ffcc9add56b0a47c0a7c851afc3"
ts = 1
md5_hash = "46ecbbd63108b0561b8778a57823bd34"
query_params = self.request.query_params
name = query_params.get('kword', None)
comic = query_params.get('comic', None)
end_point = f"https://gateway.marvel.com:443/v1/public/characters?ts={ts}&apikey={public_key}&hash={md5_hash}&name={name}&comic={comic}"
response = requests.get(end_point)
response_json = json.loads(response.text)
return Response(status=status.HTTP_200_OK, data=response_json)
I think the problem is these two lines
name = query_params.get('kword', None)
comic = query_params.get('comic', None)
that do not capture the values correctly, do you know how to solve it?
You wanted to get them from GET method, but instead you gave a dictionary, so I guess you sent it via POST. Instead of posting dictionary you should go with url:
http://127.0.0.1:8000/searchComics/?name=3-D+Man&comic=12
And you had probably a typo. You had plural "comics" in dictionary and you seek for "comic" singular.
And if you want to have data with POST method, just change def get(...) to def post(...).
My goal is to create a pdf using WeasyPrint and add it the the payload sent to the Docusign Api when requesting an envelope to be created.
Here are my steps:
generate the a pdf with WeasyPrint and return a based64 string
def generate_envelope_document(document_name: str, context: dict):
content = render_to_string(f"insurance_contracts/{document_name}.html",
context=context)
css = find(f"insurance_contracts/{document_name}.css")
doc = HTML(string=content, media_type="screen").write_pdf(stylesheets=[css],
zoom=0.8)
return base64.b64encode(doc).decode("utf-8")
create my envelope definition:
def create_envelope_definition(envelope_data: dict, context: dict, custom_fields: dict = None):
mandate = Document(
document_base64=generate_envelope_document("name1", context),
name="name1",
file_extension="pdf",
document_id=1,
)
conditions = Document(
document_base64=generate_envelope_document("name2", context),
name="name2",
file_extension="pdf",
document_id=2,
)
signer = Signer(
email=envelope_data["signer_email"],
name=envelope_data["signer_name"],
recipient_id="1",
routing_order="1",
)
signer.tabs = Tabs(
sign_here_tabs=[
SignHere(
anchor_string="Sign",
anchor_units="pixels",
anchor_y_offset="50",
anchor_x_offset_metadata="50",
)
]
)
envelope_definition = EnvelopeDefinition(
status="sent", documents=[mandate, conditions], recipients=Recipients(signers=[signer])
)
if custom_fields:
envelope_definition.custom_fields = CustomFields(
text_custom_fields=[
TextCustomField(name=field_name, value=field_value, required=False)
for field_name, field_value in enumerate(custom_fields)
]
)
return envelope_definition
create a Docusign Api object:
def get_envelopes_api_client():
"""
Create the docusign api client object
Return EnvelopesApi object
"""
api_client = ApiClient()
api_client.host = settings.DOCUSIGN_BASE_PATH
api_client.set_default_header("Authorization", "Bearer " + get_access_token())
envelope_api = EnvelopesApi(api_client)
return envelope_api
create and send the Docusign envelope:
envelope_api = get_envelopes_api_client()
try:
envelope = envelope_api.create_envelope(
settings.DOCUSIGN_ACCOUNT_ID, envelope_definition=envelope_definition
)
except ApiException as e:
logger.error(e.body.decode())
return None
return envelope
at the moment I'm getting this error:
{"errorCode":"INVALID_REQUEST_BODY","message":"The request body is missing or improperly formatted. Could not cast or convert from System.String to API_REST.Models.v2_1.propertyMetadata."}
I don't understand what I could be doing wrong. Is my envelope definition not correct or is there something else I am missing. I can't seem to find official documentation on how to do this. All I have found is [https://developers.docusign.com/docs/esign-rest-api/how-to/send-binary/][1] which does not use the docusign SDK.
Any help would be welcome. Thanks!
email_subject needs to be added to envelope_definition and has some value. That's the subject of the email sent out by DocuSign.
document_id="2" instead of document_id=2
anchor_x_offset_metadata should not be used here and is probably the reason for your error.
I have been having trouble figuring out how to add element types using a Suds factory. My code is below:
from xml.etree import cElementTree as ElementTree
from suds.client import Client
from suds.wsse import *
from suds.sax.text import Raw
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
username = 'username#tenant'
password = 'password'
url = 'https://wd2-impl-
services1.workday.com/ccx/service/[tenant]/Revenue_Management/v31.1?wsdl'
client = Client(url, username = username, password = password, faults = False)
CustomerObjectType = client.factory.create('ns0:CustomerObjectType')
CustomerObjectType.ID = 'CUS3466'
FinancialsBusinessProcess = client.factory.create('ns0:Financials_Business_Process_ParametersType')
FinancialsBusinessProcess.Auto_Complete = 'true'
CustomerData = client.factory.create('ns0:Customer_WWS_DataType')
CustomerData.Customer_Name = 'Test'
CustomerData.Worktag_Only = 'false'
CustomerData.Submit = 'true'
CustomerData.Payment_Terms_Reference.ID = ['NET30', 'Customer_ID']
CustomerData.Default_Payment_Type_Reference.ID = ['PTY_CHECK', 'Payment_Terms_ID']
CustomerData.Included_Children_Reference = ['CUS3029', 'Customer_ID']
CustomerData.Business_Entity_Data.Business_Entity_Name = 'Test'
security = Security()
token = UsernameToken(username, password)
security.tokens.append(token)
client.set_options(wsse=security)
client.service.Submit_Customer('true', CustomerObjectType, FinancialsBusinessProcess, CustomerData)
The error I am receiving is:
ERROR:suds.client:<suds.sax.document.Document instance at 0x10eb0e488>
With the output:
DEBUG:suds.client:headers = {'SOAPAction': '""', 'Content-Type': 'text/xml; charset=utf-8'}
DEBUG:suds.client:HTTP failed - 500 - Internal Server Error:
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<SOAP-ENV:Fault xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wd="urn:com.workday/bsvc">
<faultcode>SOAP-ENV:Client.validationError</faultcode>
<faultstring>Validation error occurred. Element 'ID' is missing attribute 'type'</faultstring>
<detail>
<wd:Validation_Fault>
<wd:Validation_Error>
<wd:Message>Element 'ID' is missing attribute 'type'</wd:Message>
<wd:Detail_Message>Element 'ID' is missing attribute 'type'</wd:Detail_Message>
<wd:Xpath>/ns0:Submit_Customer_Request[1]/ns0:Customer_Data[1]/ns0:Payment_Terms_Reference[1]/ns0:ID[2]</wd:Xpath>
</wd:Validation_Error>
</wd:Validation_Fault>
</detail>
</SOAP-ENV:Fault>
The formatting given back by the suds factory for the "Payment_Terms_Reference" element is as follows:
Payment_Terms_Reference =
(Payment_TermsObjectType){
ID[] =
"NET30",
"Customer_ID",
_Descriptor = ""
}
Where I am confused is that it looks like it is using the index
Payment_Terms_Reference[1]/ns0:ID[2]
to find the element type, but when I add more data to the list in the complex object type it doesn't solve the problem.
The current XML code that spits out for Payment_Terms_Reference when I run this code comes out as:
<ns0:Payment_Terms_Reference>
<ns0:ID>PTY_CHECK</ns0:ID>
<ns0:ID>Payment_Terms_ID</ns0:ID>
</ns0:Payment_Terms_Reference>
But I think I need it to format like:
<ns0:Payment_Terms_Reference>
<ns0:ID ns0:type="Payment_Terms_ID">ID</ns0:ID>
</ns0:Payment_Terms_Reference>
If anyone knows what might help me solve this problem, that would be much appreciated.
Thanks!
You need to create a factory for the Payment_TermsObjectType
paymenttype = client.factory.create('ns0:Payment_TermsObjectIDType')
paymenttype._type = "PTY_CHECK"
paymenttype.value = "Payment_Type_ID"
CustomerData.Payment_Terms_Reference.ID = paymenttype
I have a problem with WSDL operation name:import. It is one of the most important remote operation, that update product list on the remote server.
The problem starts when I want to call the method:
client.service.import('ns0:Product_Import', _soapheaders = [header_value])
node = client.service.import(product_name)
^
SyntaxError: invalid syntax
because the 'import' statement is reserved to the python. How to make that calling this method does not interfere with python?
This code below works fine. Maybe someone will use it.
from zeep import Client
from zeep import xsd
loginIn = {'username': 'my_username', 'password': 'my_password'}
wsdl_auth = 'http://some-wsdl-service.com/auth/wsdl/'
wsdl_products = 'http://some-wsdl-service.com/products/wsdl/'
header = xsd.Element(
'{http://some-wsdl-service.com/products/wsdl/}Header',
xsd.ComplexType([
xsd.Element(
'{http://some-wsdl-service.com/products/wsdl/}sessionId',
xsd.String()
),
])
)
client = Client(wsdl = wsdl_auth)
response = client.service.login(loginIn)
sid = response.sessionId
header_value = header(sessionId = sid)
client = Client(wsdl = wsdl_products)
list_of_products = client.service.get('ns0:Product_List',
_soapheaders [header_value])
client = Client(wsdl = wsdl_auth)
request_to_end = client.service.logout(_soapheaders=[header_value]))
You can use getattr() to access methods in client.service
_import = getattr(client.service, 'import')
result = _import(product_name)
My table is around 220mb with 250k records within it. I'm trying to pull all of this data into python. I realize this needs to be a chunked batch process and looped through, but I'm not sure how I can set the batches to start where the previous left off.
Is there some way to filter my scan? From what I read that filtering occurs after loading and the loading stops at 1mb so I wouldn't actually be able to scan in new objects.
Any assistance would be appreciated.
import boto3
dynamodb = boto3.resource('dynamodb',
aws_session_token = aws_session_token,
aws_access_key_id = aws_access_key_id,
aws_secret_access_key = aws_secret_access_key,
region_name = region
)
table = dynamodb.Table('widgetsTableName')
data = table.scan()
I think the Amazon DynamoDB documentation regarding table scanning answers your question.
In short, you'll need to check for LastEvaluatedKey in the response. Here is an example using your code:
import boto3
dynamodb = boto3.resource('dynamodb',
aws_session_token=aws_session_token,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
region_name=region
)
table = dynamodb.Table('widgetsTableName')
response = table.scan()
data = response['Items']
while 'LastEvaluatedKey' in response:
response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
data.extend(response['Items'])
DynamoDB limits the scan method to 1mb of data per scan.
Documentation: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Client.scan
Here is an example loop to get all the data from a DynamoDB table using LastEvaluatedKey:
import boto3
client = boto3.client('dynamodb')
def dump_table(table_name):
results = []
last_evaluated_key = None
while True:
if last_evaluated_key:
response = client.scan(
TableName=table_name,
ExclusiveStartKey=last_evaluated_key
)
else:
response = client.scan(TableName=table_name)
last_evaluated_key = response.get('LastEvaluatedKey')
results.extend(response['Items'])
if not last_evaluated_key:
break
return results
# Usage
data = dump_table('your-table-name')
# do something with data
boto3 offers paginators that handle all the pagination details for you. Here is the doc page for the scan paginator. Basically, you would use it like so:
import boto3
client = boto3.client('dynamodb')
paginator = client.get_paginator('scan')
for page in paginator.paginate():
# do something
Riffing off of Jordon Phillips's answer, here's how you'd pass a FilterExpression in with the pagination:
import boto3
client = boto3.client('dynamodb')
paginator = client.get_paginator('scan')
operation_parameters = {
'TableName': 'foo',
'FilterExpression': 'bar > :x AND bar < :y',
'ExpressionAttributeValues': {
':x': {'S': '2017-01-31T01:35'},
':y': {'S': '2017-01-31T02:08'},
}
}
page_iterator = paginator.paginate(**operation_parameters)
for page in page_iterator:
# do something
Code for deleting dynamodb format type as #kungphu mentioned.
import boto3
from boto3.dynamodb.types import TypeDeserializer
from boto3.dynamodb.transform import TransformationInjector
client = boto3.client('dynamodb')
paginator = client.get_paginator('query')
service_model = client._service_model.operation_model('Query')
trans = TransformationInjector(deserializer = TypeDeserializer())
for page in paginator.paginate():
trans.inject_attribute_value_output(page, service_model)
Turns out that Boto3 captures the "LastEvaluatedKey" as part of the returned response. This can be used as the start point for a scan:
data= table.scan(
ExclusiveStartKey=data['LastEvaluatedKey']
)
I plan on building a loop around this until the returned data is only the ExclusiveStartKey
The 2 approaches suggested above both have problems: Either writing lengthy and repetitive code that handles paging explicitly in a loop, or using Boto paginators with low-level sessions, and foregoing the advantages of higher-level Boto objects.
A solution using Python functional code to provide a high-level abstraction allows higher-level Boto methods to be used, while hiding the complexity of AWS paging:
import itertools
import typing
def iterate_result_pages(function_returning_response: typing.Callable, *args, **kwargs) -> typing.Generator:
"""A wrapper for functions using AWS paging, that returns a generator which yields a sequence of items for
every response
Args:
function_returning_response: A function (or callable), that returns an AWS response with 'Items' and optionally 'LastEvaluatedKey'
This could be a bound method of an object.
Returns:
A generator which yields the 'Items' field of the result for every response
"""
response = function_returning_response(*args, **kwargs)
yield response["Items"]
while "LastEvaluatedKey" in response:
kwargs["ExclusiveStartKey"] = response["LastEvaluatedKey"]
response = function_returning_response(*args, **kwargs)
yield response["Items"]
return
def iterate_paged_results(function_returning_response: typing.Callable, *args, **kwargs) -> typing.Iterator:
"""A wrapper for functions using AWS paging, that returns an iterator of all the items in the responses.
Items are yielded to the caller as soon as they are received.
Args:
function_returning_response: A function (or callable), that returns an AWS response with 'Items' and optionally 'LastEvaluatedKey'
This could be a bound method of an object.
Returns:
An iterator which yields one response item at a time
"""
return itertools.chain.from_iterable(iterate_result_pages(function_returning_response, *args, **kwargs))
# Example, assuming 'table' is a Boto DynamoDB table object:
all_items = list(iterate_paged_results(ProjectionExpression = 'my_field'))
I had some problems with Vincent's answer related to the transformation being applied to the LastEvaluatedKey and messing up the pagination. Solved as follows:
import boto3
from boto3.dynamodb.types import TypeDeserializer
from boto3.dynamodb.transform import TransformationInjector
client = boto3.client('dynamodb')
paginator = client.get_paginator('scan')
operation_model = client._service_model.operation_model('Scan')
trans = TransformationInjector(deserializer = TypeDeserializer())
operation_parameters = {
'TableName': 'tablename',
}
items = []
for page in paginator.paginate(**operation_parameters):
has_last_key = 'LastEvaluatedKey' in page
if has_last_key:
last_key = page['LastEvaluatedKey'].copy()
trans.inject_attribute_value_output(page, operation_model)
if has_last_key:
page['LastEvaluatedKey'] = last_key
items.extend(page['Items'])
If you are landing here looking for a paginated scan with some filtering expression(s):
def scan(table, **kwargs):
response = table.scan(**kwargs)
yield from response['Items']
while response.get('LastEvaluatedKey'):
response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'], **kwargs)
yield from response['Items']
Example usage:
table = boto3.Session(...).resource('dynamodb').Table('widgetsTableName')
items = list(scan(table, FilterExpression=Attr('name').contains('foo')))
I can't work out why Boto3 provides high-level resource abstraction but doesn't provide pagination. When it does provide pagination, it's hard to use!
The other answers to this question were good but I wanted a super simple way to wrap the boto3 methods and provide memory-efficient paging using generators:
import typing
import boto3
import boto3.dynamodb.conditions
def paginate_dynamodb_response(dynamodb_action: typing.Callable, **kwargs) -> typing.Generator[dict, None, None]:
# Using the syntax from https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/python/example_code/dynamodb/GettingStarted/scenario_getting_started_movies.py
keywords = kwargs
done = False
start_key = None
while not done:
if start_key:
keywords['ExclusiveStartKey'] = start_key
response = dynamodb_action(**keywords)
start_key = response.get('LastEvaluatedKey', None)
done = start_key is None
for item in response.get("Items", []):
yield item
## Usage ##
dynamodb_res = boto3.resource('dynamodb')
dynamodb_table = dynamodb_res.Table('my-table')
query = paginate_dynamodb_response(
dynamodb_table.query, # The boto3 method. E.g. query or scan
# Regular Query or Scan parameters
#
# IndexName='myindex' # If required
KeyConditionExpression=boto3.dynamodb.conditions.Key('id').eq('1234')
)
for x in query:
print(x)```