Class wwJsonServiceClient

Class to make it super easy to call JSON REST services with Visual FoxPro. Use it standalone with the CallMethod() method to call a service directly, or - better - create a subclass and build a dedicated service class implementation.

For a more detailed look please check out the following blog post:

You can use this class to:

  • Directly call JSON REST services
  • Subclass it and implement service methods that map a service

Directly call a JSON REST Service

The direct approach just uses the CallService() method directly.

loClient = CREATEOBJECT("wwJsonServiceClient")

lcUrl = "http://localhost:23332/localizationService.ashx?method=GetResourceSets"
lcData = ""      && no data
lcVerb = "GET"   && GET,POST,PUT,DELETE etc.
loResourceSets = loClient.CallService(lcUrl,lcData,lcVerb)

IF loClient.lError
   ? loClient.cErrorMsg
   RETURN
ENDIF
   
*** Result is an array of strings as a wwDotnetBridge ComArray
? loResourceSets.Count     && 8
? loResourceSets.Item(0)   && CustomerForm
* First item which in this case is a string but could be an object

Note that you can also pass data to the service. JSON REST endpoints that expect data typically expect a single JSON parameter - which can be an object with many sub properties - to represent input values. To pass a parameter, simply provide a FoxPro object that maps the structure of the JSON expected by the service and this class will serialize the FoxPro object/value into JSON for you.

Creating a Service Proxy Class

To use, subclass this class and implement your service methods that then using CallService() to call JSON service endpoints.

DEFINE CLASS CustomerServiceClient as wwJsonServiceClient

cServiceBaseUrl = "http://localhost:23332/localizationService.ashx?method="

FUNCTION GetResourceSets()
    LOCAL loResult
    loResult = loClient.CallService(this.cServiceBaseUrl + "GetResourceSets")

    IF (this.lError)
       RETURN .NULL.
    ENDIF
    
    RETURN loResult
ENDFUNC

FUNCTION UpdateResource(loResource)
    LOCAL loResult
    
    *** Service returns update resource
    loResult = loClient.CallService(this.cServiceBaseUrl + "UpdateResource",loResource,"PUT")

    IF (this.lError)
       RETURN NULL
    ENDIF
    
    RETURN loResult
ENDFUNC

ENDDEFINE

To use this you would then just use:

loProxy = CREATEOBJECT("CustomerServiceClient")



loResources = loProxy.GetResources()
if ISNULL(loResources)
   ? loProxy.cErrorMsg
   RETURN
ENDIF   

? loResources.Count
? loResources.Item(0).Locale
? loResources.Item(0).Text


loResource = GetMyResourceToUpdateFromSomewhere()
loResource = loProxy.UpdateResource(loResource)

if !llResult
   ? loProxy.cErrorMsg
   RETURN
ENDIF   

? loResource.ResourceId
? loResource.Locale
? loResource.Text

This latter approach is preferrable as it treats service access like a business object where all logic created around the service is created in one place. It allows you to reuse the service calls and isolate your error handling and future maintenance all in one place.

Class Members

MemberDescription

CallService

This base class method handles making an HTTP service call on your behalf and handles all HTTP related errors. You pass in an optional FoxPro value or object that is automatically serialized to JSON and then sent as the payload to the service. The resulting JSON response is deserialized into a FoxPro value or object.

o.CallService(lcUrl,lvData,lcVerb)

CreateSerializer

This method acts as a factory method for the JSON serializer instance and can be used to explicitly assign a .oJsonSerializer instance that's customized. This is useful to provide custom serialization parameters like overriding property names, or date handling etc. that are used by all instances. You can also pass in a fully configured serializer.

o.CreateSerializer(loSerializer)

CreatewwHttp

Allows overriding the default wwHTTP instance used to send a request to the server. Use this method if you need to add custom headers, authentication or prefill the post data of the Http request before making your service call.

o.CreatewwHttp(loHttp)

Example


************************************************************************
DEFINE CLASS CustomerServiceClient as wwJsonServiceClient
*********************************************************

************************************************************************
*  GetCustomers
***************
FUNCTION Customers(loParms)
LOCAL loCustomers

loCustomers = THIS.CallService(this.cServiceBaseUrl + "customers.csvc", loParms)

IF THIS.lError
   RETURN null
ENDIF   

RETURN loCustomers
ENDFUNC
*   GetCustomers

************************************************************************
*  UpdateCustomer
*****************
FUNCTION UpdateCustomer(loCustomer)

loCustomer = THIS.CallService(this.cServiceBaseUrl + "customer.csvc",loCustomer,"PUT")

IF THIS.lError
   RETURN null
ENDIF 

RETURN loCustomer
ENDFUNC
*   UpdateCustomer

ENDDEFINE

Requirements

Assembly: wwJsonSerializer.prg

See also:

Class wwJsonServiceClient

© West Wind Technologies, 1996-2017 • Updated: 03/08/17
Comment or report problem with topic