Presentation for February 2010 "Using Web Services within an RPG

advertisement
Web Services on your
Power i in RPG
Jared Gerber
jgerber@brotherhoodmutual.com
260-481-9906
February 9, 2010
How many of you have done
anything with web services?
How many have coded programs
that consume a web services (in
any language)?
How many have published a
web service?
Saturday, May 14, 2005
A component walks into a bar....
The component asks the bartender via .NET
remoting:
"Gimme a beer"
Bartender:
"What kind of beer?"
Component:
"Just give me all of them one by one and I'll
drink the one I like."
The component asks the bartender via Web
Services:
"Gimme a beer"
Bartender:
"What kind of beer?"
The component thinks a minute.
"I didn't know you'd have more than one. I just want a
beer."
Bartender:
"What kind of beer?"
Component:
"A plain one."
Bartender:
"What's plain beer? How about a bud?"
Component:
"Umm.. ok."
The component tries to take the beer but it
slips onto the floor and the glass breaks.
Bartender:
"What's wrong with you?"
Component:
"I got a slippery SoapException."
In this presentation my intent is to encourage
you to try using web services in your RPG
applications.
I hope you will see that it’s not that hard of an
assignment. The mechanics of it really
aren’t any different than any other service
program procedures that you might call.
The cool thing is the resulting information or
data that can be had and the innovative
applications you can bring to your
company.
Disclaimer:
Just so we are clear.
In this presentation I do not intend to spend time on the how to
and/or jots and tittles of coding web services. There are
many articles and presentations available that will help you
with that.
I simply want to expose you to the concepts and encourage you
to delve into it yourself. I think you will be pleased with the
results.
A little history on how I got involved with web services.
• This in not new technology. It’s been around for about a decaded.
• Lots of articles dated in the 2001 to 2006 time frame. Check them out.
• Kentucky Municipal Taxes project.
• S&P request and/or detour.
• Consuming a web service is not a lengthy process to implement.
Getting data is fairly easy to do. What you do with that data, is often
the more involved part of the process.
What is web services?
First what it’s not:
One popular misconception is that Web Services are pretty
much interchangeable with SOA. Web Services are the most
common way to implement SOA, but they are neither a
requirement nor an equivalent idea.
MC Press Online, 2/1/10 article by John Ghrist
What is web services?
• Mario Pesce’s definition:
• Web Services consist of a group of standards intended to
make it possible for diverse systems to communicate, without
requiring a particular type of middleware, programming
language or even operating system.
What is web services?
• Scott Klements definition:
• Web Services is about using the same HTTP protocol that
you use to surf the Web, to make calls from a program to a
program anywhere in the world.
• What is SOAP:
• SOAP (an XML document) is a standardized format for passing
parameters to and from a Web Service.
• What is WSDL:
• WSDL (an XML document) contains a list of the Web Services that you
offer and provides information about the data types, lengths, and
formats of the parameters that need to be passed to each Web
Service. When you know this information, you can pick a service from
the list and use the information in the WSDL to create a SOAP
message.
So how does it work?
• Sockets and API programming is the key to the HTTP
protocol used to go to the internet. (Sockets programming is
something I have not done! The beauty is I don’t need to.)
• In my searching for an interface to the internet, I found about
four different product possibilities.
• 1) You can use the Power i PASE environment to run cURL.
• 2) I think you can use tools like CGIDEV2, CGITOOLS, or
eRPGSDK.
• 3) Bob Cozzi has a service program called iSockets.
• 4) Scott Klement has a freeware service program called
HTTPAPI.
How does it work?
• We chose HTTPAPI because it’s open source and free.
• Scott comments in one of his write ups that after you have used
the WSDL to format your SOAP message, you can use HTTPAPI
to send it to the server. The server interprets it while you wait and
sends back another SOAP message in response. You can then
interpret that message to get the parameters that the service
returns.
• Typically the messages are in XML format that you parse out,
yielding the results that you requested. But this is not a
requirement (S&P data).
• Scott also recommends a PC based tool called SoapUI that can
be used to more easily retrieve and interpret WSDL documents.
http:/www.scottklement.com
The place to go!
What does the RPG look like?
I have included a couple of snippits of what the RPG code and
service program calls look like, to show you that it really isn’t
much different than any other sub-procedure or service
program that you would write yourself.
begsr blkLookup;
locData.val = *zeros;
setll (asstno:'M':savnumb) blksch;
reade (asstno:'M':savnumb) blksch;
dow not %eof(blksch);
if bkbval + bkpval > locData.val;
locData.val = bkbval + bkpval;
parmIn.street = bkloc;
parmIn.city = bkcity;
parmIn.state = bkst;
parmIn.zip = %char(bkzip);
ENDIF;
reade (asstno:'M':savnumb) blksch;
ENDDO;
if locData.val <> *zeros;
parmIn.lob = 'M';
result = DC5710(parmIn);
else;
//not found error
result.ErrDtlDesc = 'Unable to locate address for loc:' +
%char(savLoc#) + ' on policy:' + %char(asnumb);
endif;
if result.ErrDtlDesc = *blanks;
if result.MQmatchCode <> '0';
//didn't have a good match
result.ErrDtlDesc = result.MQdesc;
else;
//interpret the results
exsr parseWeb;
ENDIF;
ENDIF;
ENDSR;
postData =
'<?xml version="1.0" encoding="utf-8"?>'
'<soap:Envelope xmlns:xsi='
'"http://www.w3.org/2001/XMLSchema-instance" '
'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
'<soap:Body>'
'<AllocateAddress xmlns='
'"http://allocator.tritechsoft.com/AllocatorAgent/">'
'<CompanyID>'
'brotherhood'
'</CompanyID>'
'<Password>'
'P12345MXY'
'</Password>'
'<SourceStreet>'
%trim(userData.street)
'</SourceStreet>'
'<SourceCity>'
%trim(userData.city)
'</SourceCity>'
'<SourceState>'
%trim(userData.state)
'</SourceState>'
'<SourceZipCode>'
%trim(userData.zip)
'</SourceZipCode>'
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
'<LineOfBusiness>'
%trim(userData.lob)
'</LineOfBusiness>'
'<EffectiveDate>'
%trim(userData.effDate)
'</EffectiveDate>'
'</AllocateAddress>'
'</soap:Body>'
'</soap:Envelope>'
act = 1;
url = 'http://allocator.tritechsoft.com/AllocatorAgent/' +
'/Service.asmx';
rc = http_url_post_xml( url
: %addr(postData) + 2
: %len(postData)
: %paddr(StartOfElement)
: %paddr(EndOfElement)
: *NULL );
if (rc <> 1);
result.ErrDtlDesc = http_error();
endif;
+
+
+
+
+
+
+
+
;
• Some of the web services available…
–
–
–
–
–
–
Currency conversion
DOW Jones, NASDAQ, S&P
Municipal Tax Rates
Converting text from one language to another language
Tracking UPS packages
Weather information
– Etc., etc., etc.
• Unlimited possibilities exist. What do you
think you need? I would be surprised if it
isn’t already available.
I’ll be available for questions and/or
you can contact me through the STATUS website (www.statususer.org).
Download