I have a SOAP web service that I am posting a request to with Python, but I am not sure how to format the return.
UPDATE: Return is an 'instance' being returned by SUDS
I have been able to return just one record of the 40, which is included below:
print len(xmloutput)
print len(xmloutput[1].ODI_Outage)
print xmloutput[1].ODI_Outage[6]
This is the return that I am getting:
2
40
(ODI_Outage){
metersAffected = 28
ERT =
(ert_time){
ert = "2013-07-19T20:50:00Z"
}
Incident =
(incident){
Location =
(location){
mainAddress =
(mailaddress){
townDetail =
(towninfo){
code = "L7L6W3"
name = "BURLINGTON"
stateOrProvince = "ONTARIO"
}
}
PositionPoints =
(coordinates){
xPosition = -79.7833492971
yPosition = 43.3923166879
}
}
}
}
How do I take this return and create either XML or JSON for all of the ODI_Outage objects? Just not sure what to do.
To check the type of the return use: print type(your_return_variable)
Related
Hello I am trying to retrieve multiple indcode using the below code. However I get an error saying "cannot specify for field more than once" Can you anyone please assist?
URL = "https://data.colorado.gov/resource/cjkq-q9ih.json"
D = dict()
D["area"] = 57
D["indcode"] = 10,23,81
document = requests.get(URL, D)
print(document.request.url)
Error message received.
{
"error" : true,
"message" : "cannot specify a field more than once"
}
screenshot attached
document = requests.get(URL, D)
print(document.request.url)
Getting data with multiple indcode is impossible at that URL until web developers provide that feature. You can try with another way.
If you want to retrieve multiple indcode, just try to iterate all data in one request from https://data.colorado.gov/resource/cjkq-q9ih.json?area=57. Try this :
import requests
URL = "https://data.colorado.gov/resource/cjkq-q9ih.json"
D = dict()
D["area"] = 57
# D["indcode"] = 10,23,81
indcode = [10,23,81]
document = requests.get(URL, D)
data = document.json()
filteredData = list(filter(lambda p: int(p['indcode']) in indcode, data))
print(filteredData)
I need to create the body for multiple updates to a Google Spreadsheet using Python.
I used the Python dictionary dict() but that doesn't work for multiple values that are repeated as dict() doesn't allow multiple keys.
My code snippet is:
body = {
}
for i in range (0,len(deltaListcolNames) ):
rangeItem = deltaListcolNames[i]
batch_input_value = deltaListcolVals[i]
body["range"] = rangeItem
body["majorDimension"] = "ROWS"
body["values"] = "[["+str(batch_input_value)+"]]"
batch_update_values_request_body = {
# How the input data should be interpreted.
'value_input_option': 'USER_ENTERED',
# The new values for the input sheet... to apply to the spreadsheet.
'data': [
dict(body)
]
}
print(batch_update_values_request_body)
request = service.spreadsheets().values().batchUpdate(
spreadsheetId=spreadsheetId,
body=batch_update_values_request_body)
response = request.execute()
Thanks for the answer, Graham.
I doubled back and went away from using the dict paradigm and found that by using this grid, I was able to make the data structure. Here is how I coded it...
perhaps a bit quirky but it works nicely:
range_value_data_list = []
width = 1
#
height = 1
for i in range (0,len(deltaListcolNames) ):
rangeItem = deltaListcolNames[i]
# print(" the value for rangeItem is : ", rangeItem)
batch_input_value = str(deltaListcolVals[i])
print(" the value for batch_input_value is : ", batch_input_value)
# construct the data structure for the value
grid = [[None] * width for i in range(height)]
grid[0][0] = batch_input_value
range_value_item_str = { 'range': rangeItem, 'values': (grid) }
range_value_data_list.append(range_value_item_str)
Review the documentation for the Python client library methods: The data portion is a list of dict objects.
So your construct is close, you just need a loop that fills the data list:
data = []
for i in range(0, len(deltaListcolNames)):
body = {}
# fill out the body
rangeItem = deltaListcolNames[i]
....
# Add this update's body to the array with the other update bodies.
data.append(body)
# build the rest of the request
...
# send the request
...
In SUDS when i call a method on the server and receive it's response, i access the return values/attributes using . notation (attribute access). Is there a way for me to have the response stored in a python object - after all the WSDL/SUDS has an idea of the structure of the server-data-type.
eg:
SOAP Type: ClientInfo
(reply){
Transaction =
(Transaction){
Reference1 = "1"
Reference2 = "2"
Reference3 = "3"
Reference4 = "4"
Reference5 = "5"
}
Notifications = ""
HasErrors = False
Countries =
(ArrayOfCountry){
Country[] =
(Country){
Code = "AD"
Name = "Andorra"
IsoCode = "020"
StateRequired = False
PostCodeRequired = False
PostCodeRegex = None
InternationalCallingNumber = "376"
},
Can i dump this into a python data-type/object by using the wsdl info?
The suds object is a python object, you're just looking at the print representation. Suds has many hooks where you can digest its results.
https://fedorahosted.org/suds/wiki/Documentation
I am new to python and using suds to consume data from a SOAP service. I face this error while making a call to the service. Other calls which do not require me to send some parameters are working fine. However this service is throwing me the following error.
Can somebody please help me in understanding what this error is?
primary is just a property of class devline and its a boolean.
criteria = connection.factory.create('criteria')
devline = connection.factory.create('devline')
devline.primary = True
devline.source ="abc"
devline.name = "xyz"
devline.hybridType = "xyz"
criteria.primaryDevline = devline
criteria.pairedDevlines = []
criteria.criteriaNumber = None
Criteria looks like this:
(criteriaKey){
primaryDevline =
(devline){
primary = None
sourceBase = None
devlineName = None
hybridType =
(hybridType){
value = None
}
}
pairedDevlines[] = <empty>
criteriaNumber = None
}
When I try to call the service using response = connection.service.somemethod(criteria,criteriaState.PUBLISHED)
I get the following error:
self = <suds.mx.literal.Literal instance at 0x103fd8998>
content = (Content){
tag = "primary"
value = True
type = None
}
> ???
E TypeNotFound: Type not found: 'primary'
File "/Users/nkimidi/projects/firstws/cca/ezcommit-client/test/unit/service/build/bdist.macosx-10.8-intel/egg/suds/mx/literal.py", line 87
TypeNotFound
=========================== 1 failed in 0.27 seconds ===========================
this started working when i used devline as a dictionary instead of the method used above. 1 days was wasted in this.
Instead of this:
devline.primary = True
devline.source ="abc"
devline.name = "xyz"
devline.hybridType = "xyz"
cKey.primaryDevline = devline
cKey.pairedDevlines = []
cKey.criteriaNumber = None
This snippet has worked:
devline = {
'primary':1,
'sourceBase':sourceBase,
'devlineName' : devlineName,
'hybridType' : hybridType
}
cKey['primaryDevline'] = devline
cKey['pairedDevlines'] = []
cKey['criteriaNumber'] = None
Documentation for suds says any of the methods would work, but the previous did not work for me.
I receive the following data from an API request, I would like to be able to search the data and select the ID where Name = "Steve" (for instance 3 in the example below)
The data returned from the API always has a different number of elements in, and the location of 'Steve' can be in a different part of the returned string. The ID will also change.
(Getdata){
header =
(APIResponseHeader){
sessionToken = "xxxx"
}
Items[] =
(Summary){
Id = 1
Name = "John"
TypeId = 1
},
(Summary){
Id = 2
Name = "Jack"
TypeId = 1
},
(Summary){
Id = 3
Name = "Steve"
TypeId = 1
},
}
I think the format of the data is JSON(?) and I'm not sure how to convert, and then search it, if this is at all possible..?