Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RESTful client 1

Status
Not open for further replies.

florindaniel

Programmer
Dec 4, 2009
120
0
0
RO
Hello,

I need to access some information via a REST service.
Is it possible to implement this client in VFP... and HOW ?

Thank you,
Daniel
 
wwJsonServiceClient seems useful. You can also use the more basic xmlhttprequest which started the idea of service usage even in javascript clients, the basis of web 2.0 and AJAX. Microsoft implemented it for IE JS and made it available as OLE class Microsoft.XMLHTTP

Anyway, Rick always was adding more to the basic winsock or wininet API calls etc with all his ww-Classes and I assume wwJsonServiceClient roots in .net, which would open Restful services in the way .net uses them.

It's worth noting though, the compatibility of the offered methods and parameterization of Microsoft.XMLHTTP as MS OLE class implementation of the xmlhttprequest makes it your entry point to orientation with any JS code using the xmlhttprequest, it's easy to port such code to VFP almost 1:1 in terms of using this object and the structure of result objects like DOM documents/XML nodes this object can receive as response to a request.

Another such basis could be curl, many PHP or Python code makes use of the curl library and there is a VFP binding to libcurl via Carlos Alloatti (unfortunately again as ever so often down at the moment).

A similar sample without parsing out the resultset, taking Rick Strahls sample URL:

Code:
Local loRequest as Microsoft.XMLHTTP

loRequest = CREATEOBJECT("Microsoft.XMLHTTP")

*** Make the service call
loAlbums = loRequest.open("GET","[URL unfurl="true"]http://albumviewerswf.west-wind.com/api/albums")[/URL]
loRequest.send()
Do While loRequest.readyState <>4
   Doevents && Force && when using VFP9
EndDo
*** inspect the result
? loRequest.status && 200 means ok, 404 not found etc. http status numbers see wikipedia, w3c, google them, etc.
? Left(loRequest.responseText,1000)+"..."

So you see what work wwJsonServiceClients adds in the form of parsing the response Json text to a VFP object collection it then iterates with For Each. Anyway, you may use nfJson for parsing JSON, too and the ResponseText might be anything else like XML or HTTP or any proprietary format of a result in other cases of RESTful APIs. RESTful is not as strict a standard, there are many flavours, like with XML or even HTML. The common denominator is basing on http requests with the different actions meaning CREATE, READ, UPDATE and DELETE requests, i.e. "GET" to read an entity or list, "POST" or "PUT" to create and update or vice versa - this already is where approaches differ. If the service is more like an enterprise service, you might be lucky it implements the OData standard, which offers a rich variety of querying data even with join like sql capabilities.

It's a bit of personal taste to use a high or low level library and you can't decide what RESTful implementation the service uses and offers. In the end you're always more flexible doing things with low level classes, pay this with more work, but are able to mend slight differences or do future tasks with other APIs, where a high level library might just fail overall, eg wwJsonServiceClient most probably won't switch to parsing XML, it might also fail on JSON nested in more complex ways. But Rick Strahl offers both things, high and low level, wwConnect has many things about HTTP and FTP and XML very high level and also some of his free libs like the older wwXML library are still good to go at any XML.

If you go for low level implementation it makes sense to write classes doing essential stuff like sending a request and waiting for the response in one method like .CallService which makes the call and goes as far as returning the result in a VFP friendlier form of a collection. nfJson parses JSON to a Cursor, for example, also useful. Such things as the few lines of code from open to enddo can be put into such a method. Just like any good code to not repeat it and let it surface on the highest level methods using the service but let it be part of the core method making any request and parsing any result. The more you do yourself, the more you know in detail and can adapt to other situations.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top