I try to get extract some informations from bricks. Unfortunately bricks API is build on SOAP standart and don´t support JSON. If I use HTTP for getting the data everythink works fine:
<ArrayOfSets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="https://brickset.com/api/">
<sets>
<setID>494</setID>
<number>1665</number>
<numberVariant>1</numberVariant>
<name>Dual FX Racers</name>
So the authentification works. I tried to get the response by Pythons (3.6) Urllib:
import urllib.request
html = urllib.request.urlopen('https://brickset.com/api/v2.asmx/...')
print(html)
The result looks like that:
I already tried to get the data from this url by beautifulsoup4 but it doesnt worked. I got everytime an empty array.
EDIT: new code depending on 1N5818 answer
from zeep import Client
wsdl_url = "https://brickset.com/api/?wsdl"
soap_client = Client(wsdl_url)
result = soap_client.getSet("xxx","xxx","494")
WSDL DOKU:
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="https://brickset.com/api/">
<s:element name="getSet">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="apiKey" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="userHash" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="SetID" type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
Ok I got my misunderstanding. But if I do this code I get following Error:
AttributeError: 'Client' object has no attribute 'getSet'
What did I wrong?
If you're going to interact witha SOAP API you need a SOAP client to parse the WSDL url or else you won't be able to create a client to call like an RPC. Zeep is one that's available for Python. You can't just interact with it with CURL or something like that.
How you're trying to interact with the API is not really the way SOAP works. Soap stands for "Simple Object Access Protocol". It works like an RPC ("Remote Procedural Call"). You need to create an object, then call it like it was a statically typed class object. In order to create that client (auto-generated), you code needs to know the parameters of that server before a client is created (which it gets from the WSDL url "Web Service Description Language" URL).
WSDL urls are not human-readable (not intended to be human readable), they're there to give just enough information about the API for the object you'll be calling from.
You need to import a SOAP specific Python library. Zeep for example
from zeep import Client
# Here's an example for calling data to a fake weather API.
data = {
"timestamp": "now",
"city": "London"
}
# It would have to know how to interact with the client
wsdl_url = "weather_service_url.com/?wsdl" # always ending in "wsdl" (web service description language"
# Now python knows what functions parameters are
# available and creates a client you can interact with.
soap_client = Client(wsdl_url)
# Then interact with the client like a class object
weather_api_result = soap_client.service.get_weather(**data)
EDIT:
It looks like per the documentation for the API you're trying to use, an API_KEY is required for almost every request.
Here's their documentation: https://brickset.com/tools/webservices/v2
Here's an example of using their API. I had to obtain an API key and it was emailed to me.
>>> from zeep import Client
>>> client = Client("https://brickset.com/api/v2.asmx?WSDL")
Forcing soap:address location to HTTPS
Forcing soap:address location to HTTPS
Forcing http:address location to HTTPS
Forcing http:address location to HTTPS
>>> result = client.service.checkKey("T2BZ-ODTf-LK5p"); # This was the API key sent to me, but you should get your own.
>>> result
'OK'
Their webpage here shows the link the the WSDL url page (which is also in the above code).
Notice per the zeep
docs, every WSDL file you read in will create a service
for the client, so every call to any function of the API will be prepended with client.service
(web-service to be precise).
It looks like the method you're trying to call getSets
requires some information that you need to get by logging in first (or by calling the login
method).
You could also call the API by constructing a dictionary for your parameters like this:
>>> data = {
... "apiKey": "T2BZ-ODTf-LK5p"
... }
>>> result = client.service.checkKey(**data);
>>> result
'OK'
You would call the login form like this:
>>> login_request_data = {
... "apiKey": "T2BZ-ODTf-LK5p",
... "username": "Whatever your username is",
... "passowrd": "Whatever your password is"
... }
>>> result = client.service.checkKey(**login_request_data);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With