Introduction
In our previous post, we saw in general how to call REST API in Power BI. Now let’s look at how to call SOAP API in Power BI (e.g. WorkDay, Google AdWords services). SOAP API is in XML format so we will be using ZappySys XML Driver for a demonstration in this article.
To learn how to automate dataset refresh on a scheduled basis you can again refer to the previous post because that won’t be covered in this article.
So let’s get started !!!
Requirements
- First, you will need to have Power BI Desktop (FREE) installed
- Make sure you have ZappySys ODBC Power Pack installed
Creating SQL Queries to call SOAP API in Power BI (Using XML Driver)
Very first step to call SOAP API in Power BI is to understand how to craft SOAP Requests using ZappySys XML Driver. Once you play with sample SOAP queries you can copy/paste that into Power BI.
What is SOAP Web Service?
If you are new to SOAP Web Service sometimes referred as XML Web Service then please read some concept about SOAP Web service standard from this link There are two important aspects in SOAP Web service.- Getting WSDL file or URL
- Knowing exact Web Service URL
What is WSDL
In very simple term WSDL (often pronounced as whiz-dull) is nothing but a document which describes Service metadata (e.g. Functions you can call, Request parameters, response structure etc). Some service simply give you WSDL as xml file you can download on local machine and then analyze or sometimes you may get direct URL (e.g. http://api.mycompany.com/hr-soap-service/?wsdl )Example SQL Query for SOAP API call using ZappySys XML Driver
Here is an example SQL query you can write to call SOAP API. If you not sure about many details then check next few sections on how to use XML Driver User Interface to build desired SQL query to POST data to XML SOAP Web Service without any coding.SELECT * FROM $ WITH( Src='http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx' ,DataConnectionType='HTTP' ,CredentialType='Basic' --OR SoapWss ,SoapWssPasswordType='PasswordText' ,UserName='myuser' ,Password='pass$$w123' ,Filter='$.soap:Envelope.soap:Body.GetHolidaysAvailableResponse.GetHolidaysAvailableResult.HolidayCode[*]' ,ElementsToTreatAsArray='HolidayCode' ,RequestMethod='POST' ,Header='Content-Type: text/xml;charset=UTF-8 || SOAPAction: "http://www.holidaywebservice.com/HolidayService_v2/GetHolidaysAvailable"' ,RequestData=' <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hol="http://www.holidaywebservice.com/HolidayService_v2/"> <soapenv:Header/> <soapenv:Body> <hol:GetHolidaysAvailable> <!--type: Country - enumeration: [Canada,GreatBritain,IrelandNorthern,IrelandRepublicOf,Scotland,UnitedStates]--> <hol:countryCode>UnitedStates</hol:countryCode> </hol:GetHolidaysAvailable> </soapenv:Body> </soapenv:Envelope>' )Now let's look at steps to create SQL query to call SOAP API. Later we will see how to generate code for your desired programming language (e.g. C# or SQL Server)
Video Tutorial - Introduction to SOAP Web Service and SoapUI tool
Before we dive into details about calling SOAP API using ZappySys XML Driver, lets first understand what is SOAP API and how to create SOAP requests using SoapUI tool. You will learn more about this process in the later section. The video contains some fragment about using SOAP API in SSIS but just ignore that part because we will be calling Soap API using ZappySys ODBC Driver rather than SSIS Components.Using SoapUI to test SOAP API call / Create Request Body XML
Assuming you have downloaded and installed SoapUI from here, now we are ready to use WSDL for your SOAP Web Service Calls. If you do not have WSDL file or URL handy then contact your API provider (sometimes you just have to add ?wsdl at the end of your Service URL to get WSDL so try that. Example: http://mycompany/myservice?wsdl ). If you don't know what is WSDL then in short, WSDL is Web service Description Language (i.e. XML file which describes your SOAP Service). WSDL helps to craft SOAP API request Body for ZappySys XML Driver. So Let's get started.- Open SoapUI and click SOAP button to create new SOAP Project
- Enter WSDL URL or File Path of WSDLFor example WSDL for our sample service can be accessed via this URL
http://www.dneonline.com/calculator.asmx?wsdl
Create new SOAP API Project in SoapUI tool for SOAP API Testing - Once WSDL is loaded you will see possible operations you can call for your SOAP Web Service.
- If your web service requires credentials then you have to configure it. There are two common credential types for public services (SOAP WSS or BASIC )
-
To use SOAP WSS Credentials select request node and enter UserId, Password, and WSS-PasswordType (PasswordText or PasswordHash)Configure SOAP WSS Credentials for SoapUI (SOAP API Testing Tool)
- To use BASIC Auth Credentials select request node and double-click it. At the bottom click on Auth (Basic) and From Authorization dropdown click Add New and Select Basic.
Configure Basic Authorization for SoapUI (SOAP API Testing Tool)
-
- Now you can test your request first Double-click on the request node to open request editor.
- Change necessary parameters, remove optional or unwanted parameters. If you want to regenerate request you can click on Recreate default request toolbar icon.
Create SOAP Request XML (With Optional Parameters)
- Once your SOAP Request XML is ready, Click the Play button in the toolbar to execute SOAP API Request and Response will appear in Right side panel. Test SOAP API using SoapUI Tool (Change Default XML Body / Parameters, Execute and See Response)
Create DSN using ZappySys XML Driver to call SOAP API
Once you have tested your SOAP API in SoapUI tool, we are ready to use ZappySys XML driver to call SOAP API in your preferred BI tool or Programming language.- First open ODBC Data Sources (search ODBC in your start menu or go under ZappySys > ODBC PowerPack > ODBC 64 bit)
- Goto System DSN Tab (or User DSN which is not used by Service account)
- Click Add and Select ZappySys XML Driver ZappySys ODBC Driver for XML / SOAP API
- Configure API URL, Request Method and Request Body as below ZappySys XML Driver - Calling SOAP API - Configure URL, Method, Body
- (This step is Optional) If your SOAP API requires credentials then Select Connection Type to HTTP and configure as below.
ZappySys XML Driver - Configure SOAP WSS Credentials or Basic Authorization (Userid, Password)
- Configure-Request Headers as below (You can get it from Request > Raw tab from SoapUI after you test the request by clicking the Play button) Configure SOAP API Request Headers - ZappySys XML Driver
- Once credentials entered you can select Filter to extract data from the desired node. Make sure to select array node (see special icon) or select the node which contains all necessary columns if you don't have array node. Select Filter - Extract data from nested XML / SOAP API Response (Denormalize Hierarchy)
- If prompted select yes to treat selected node as Array (This is helpful when you expect one or more record for selected node) Treat selected node as XML Array Option for SOAP API Response XML
Preview SOAP API Response / Generate SQL Code for SOAP API Call
Once you configure settings for XML Driver now you can preview data or generate example code for desired language (e.g. C#, Python, Java, SQL Server). Go to Preview tab and you will see default query generated based on settings you entered in previous sections. Attributes listed in WITH clause are optional. If you omit attribute in WITH clause it will use it from Properties tab.Preview Data
Preview SOAP API Response in ZappySys XML DriverGenerate Code Option
Import XML SOAP Web Service in Power BI (Using XML Driver)
Once you know how to craft SQL Queries for SOAP API calls, and you save created DSN as per the previous section we are ready to load data into Power BI from XML Web Service.
So let’s get started.
- First launch Power BI and Click on Get Data >> More Data >> Other >> ODBC
- Select your ODBC DSN Source from the dropdown (Assuming you have created a ODBC Data Source using ZappySys XML Driver – See the previous Section)
- Expand Advanced Options to enter custom Query.
- Enter your SOAP API call SQL query here. For example purpose, we will use below query to try demo SOAP web service.
1234567891011121314151617181920212223SELECT * FROM $WITH(Src='http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx',DataConnectionType='HTTP',CredentialType='Basic' --OR SoapWss,SoapWssPasswordType='PasswordText',UserName='myuser',Password='pass$$w123',Filter='$.soap:Envelope.soap:Body.GetHolidaysAvailableResponse.GetHolidaysAvailableResult.HolidayCode[*]',ElementsToTreatAsArray='HolidayCode',RequestMethod='POST',Header='Content-Type: text/xml;charset=UTF-8 || SOAPAction: "http://www.holidaywebservice.com/HolidayService_v2/GetHolidaysAvailable"',RequestData='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hol="http://www.holidaywebservice.com/HolidayService_v2/"><soapenv:Header/><soapenv:Body><hol:GetHolidaysAvailable><!--type: Country - enumeration: [Canada,GreatBritain,IrelandNorthern,IrelandRepublicOf,Scotland,UnitedStates]--><hol:countryCode>UnitedStates</hol:countryCode></hol:GetHolidaysAvailable></soapenv:Body></soapenv:Envelope>') - Once you see data preview click OK to import.
- After data import is done you can edit your dataset (e.g. remove unwanted columns)
- You can also edit Source query after data is imported (See below)
- That’s it. Use your dataset to build Power BI dashboard and when you ready to publish just hit Publish Button.
Using Parameters in Power BI (Dynamic Query)
let vKey=paraAPIKey, Source = Odbc.Query( "dsn=ZS - OData Customers", "SELECT * FROM $ WITH (SRC='http://httpbin.org/post'," & "METHOD='POST'," & "HEADER='Content-Type:application/json'," & "BODY=@'{""CallerId"":1111, ""ApiKey"":""" & vKey & """}')") in Source
SOAP XML / REST API pagination in Power BI
Paginate by Response Attribute
This example shows how to paginate API calls where you need to paginate until the last page detected. In this example, next page is indicated by some attribute called nextlink (found in response). If this attribute is missing or null then it stops fetching the next page.SELECT * FROM $ WITH( SRC=@'https://zappysys.com/downloads/files/test/pagination_nextlink_inarray_1.json' ,NextUrlAttributeOrExpr = '$.nextlink' --keep reading until this attribute is missing. If attribute name contains dot then use brackets like this $.['my.attr.name'] )
Paginate by URL Parameter (Loop until certain StatusCode)
This example shows how to paginate API calls where you need to pass page number via URL. The driver keeps incrementing page number and calls next URL until the last page detected (401 error). There are few ways to indicate the last page (e.g. By status code, By row count, By response size). If you don't specify end detection then it will use the default (i.e. No records found).SELECT * FROM $ WITH ( SRC=@'https://zappysys.com/downloads/files/test/page-xml.aspx?page=1&mode=DetectBasedOnResponseStatusCode' ,PagingMode='ByUrlParameter' ,PagingByUrlAttributeName='page' ,PagingByUrlEndStrategy='DetectBasedOnResponseStatusCode' ,PagingByUrlCheckResponseStatusCode=401 ,IncrementBy=1 )
Paginate by URL Path (Loop until no record)
This example shows how to paginate API calls where you need to pass page number via URL Path. The driver keeps incrementing page number and calls next URL until the last page is detected. There are few ways to indicate the last page (e.g. By status code, By row count, By response size). If you don't specify end detection then it will use the default (i.e. No records found).SELECT * FROM $ WITH ( SRC=@'https://zappysys.com/downloads/files/test/cust-<%page%>.xml' ,PagingMode='ByUrlPath' ,PagingByUrlAttributeName='<%page%>' ,PagingByUrlEndStrategy='DetectBasedOnRecordCount' ,IncrementBy=1 )
Paginate by Header Link (RFC 5988)
API like GitHub / Wordpress use Next link in Headers (RFC 5988)SELECT * FROM $ LIMIT 25 WITH( Src='https://wordpress.org/news/wp-json/wp/v2/categories?per_page=10' ,PagingMode='ByResponseHeaderRfc5988' ,WaitTimeMs='200' --//wait 200 ms after each request )
SOAP XML / REST API Error Handling in Power BI
METHOD 1 - Using Error Handling Options
When to use?
You may want to use them when your source is a resource located on the Internet; e.g. a file on a website, a file on an FTP server or just a plain API HTTP response. By default, when a remote server returns an error, data retrieval is stopped, an error is raised and no data is given back to you. This might not be always desirable.Scenario 1
Imagine a scenario, that there is a web server which each day at 12 AM releases a new JSON file with that day's date as filename, e.g. http://www.some-server.com/data/2018-06-20.json. And, of course, you want to download it and use it daily in your Power BI report. But you have a problem: Power BI report data sources are refreshed each hour and you may get HTTP 404 status code (no file was found) when a file is not released yet. Which consequentially means other data sources won't be updated as well and you will see old and cached data on the report. That's where you could use Continue on any error or Continue when Url is invalid or missing (404 Errors) to avoid an error being raised and let other data sources to be updated.Scenario 2
Another scenario is when you expect a web server to raise some kind of HTTP error when accessing a URL. You don't want ODBC Data Source to raise an error but instead, you want to get response data. That's where you can use Continue on any error or alike together with Get response data on error to continue on an error and get the data:METHOD 2 - Using Connection [Retry Settings]
Another scenario you may run into is a buggy web server. You ask it to give you some file or data and it, like a snotty kid, just doesn't give it to you! You have to ask twice or thrice before it does its job. If that's the case, you have to retry HTTP requests using Connection:Security / Connection Types (Options for HTTP, OAuth, SOAP) in Power BI
- HTTP
- OAuth
HTTP Connection
- SOAP WSS (when accessing a SOAP WebService)
- Static Token / API Key (when need to pass an API key in HTTP header)
- Dynamic Token (same as Static Token method except that each time you need to log in and retrieve a fresh API key)
- JWT Token (As per RFC 7519)
OAuth
If you are trying to access REST API resource, it is a huge chance, you will need to use OAuth Connection. Read this article to understand how OAuth authentication and authorization works and how to use it (article originally was written for SSIS PowerPack, but the concepts and UI stay the same): https://zappysys.com/blog/rest-api-authentication-with-oauth-2-0-using-ssisOther Considerations for Calling Web API in Power BI
API Limit / Throttling
While calling public API or other external web services one important aspect you have to check, how many requests are allowed by your API. Especially when you use API pagination options to pull many records you have to slow down based on API limits. For example, your API may allow you only 5 requests per second. Use Throttling Tab on Driver UI to set delay after each request.2D Array Transformation
If you are using JSON or XML API Driver then possible you may have to transform your data using 2D array transformation feature. Check this link for more information.Conclusion
In this article, we explored possibilities to load SOAP API data in Power BI without any coding or ETL approach. You can build powerful dashboards and reports by consuming data from any REST / XML SOAP API services using ZappySys Drivers. You can download ZappySys ODBC Drivers and try yourself to learn many other features not covered in this article.