OC-SOAP: access to SOAP for OCaml

OC-SOAP allows you to make calls to SOAP services from Objective CAML programs.

The main program takes WSDL interface description files and converts them into stub functions and types which can be called and used from your programs.

We have tested it extensively against the Google Adwords API and it is very solid and reliable.

Note

This project is the new home of a former OC-SOAP project locate at merjis

An older version of this library is called simplesoap. This is no longer maintained, but may be useful if you need access to pre-document/literal services.

Requirements

The following libraries are required:

If you are running Debian or a Debian-derivative like Ubuntu then you can just "apt-get install" the following packages:

sudo apt-get install libpxp-ocaml-dev libpcre-ocaml-dev libequeue-ocaml-dev \
  libextlib-ocaml-dev libnetclient-ocaml-dev libcalendar-ocaml-dev \
  cduce ocaml-native-compilers

License

This library is distributed under the GNU Library General Public License with the special OCaml linking exception.

Download

Download version 0.7.0

Online documentation

Example: Using the Adwords API

This example shows us calling the Google Adwords API. It is rather complex because Google requires us to add special headers to every request containing authentication info (note the add_headers function).

(* Example of using the Adwords API.
 * $Id: example.ml,v 1.1 2005/06/28 09:22:54 rich Exp $
 *)

open Printf

open CampaignService
open CampaignService_types

(* Set these to your account details. *)
let email = "xxx"
let password = "xxx"
let client_email = None (* or Some "clientemail@example.com" *)
let token = "xxx"
let useragent = "Merjis client"

(* This is the HTTP object used for making connections to the remote service.*)
let connection =
  let connection = OCSoap.default_connection () in
  connection#set_proxy "localhost" 8081;
  connection

(* Add the headers required by Google in every outgoing SOAP call. *)
let add_headers svc context =
  let make_header name value =
    OCSoap.Pxp_helpers.element context name [
      OCSoap.Pxp_helpers.data context value
    ]
  in
  let headers =
    [ make_header "fn:email" email;
      make_header "fn:password" password;
      make_header "fn:token" token;
      make_header "fn:useragent" useragent ] in
  match client_email with
    | None -> headers
    | Some client_email ->
	(make_header "fn:clientEmail" client_email) :: headers

(* Campaign service SOAP object.  This must be passed as the first parameter
 * to every SOAP call which uses the campaign service.
 *)
let campaign_svc =
  OCSoap.service
    ~connection ~add_headers
    ~location:"http://adwords.google.com/api/adwords/v2/CampaignService"
    ~namespace:"https://adwords.google.com/api/adwords/v2"
    ()

let () =
  try
    (* Get the list of campaigns from Google. *)
    let campaigns = getAllAdWordsCampaigns campaign_svc 0l in

    (* Print out the campaigns. *)
    printf "You have %d campaigns:\n" (List.length campaigns);
    List.iter (
      fun campaign ->
	printf "campaign #%ld %s (%s)\n"
	  campaign.campaign_id
	  (Option.get campaign.campaign_name)
	  (string_of_campaignStatus (Option.get campaign.campaign_status))
    ) campaigns
  with
      Http_client.Http_error (code, msg) ->
	eprintf "HTTP error code: %d\nError message: %s\n" code msg;
	()

Note: stunnel

The current version of ocamlnet doesn't support HTTPS connections (but Gerd is working on it as we speak). In the meantime, the code above is set to proxy connections through stunnel running on port 8081. If, when you run the code, you see this error:

Fatal error: exception Unix.Unix_error(63, "read", "")

then please run stunnel as follows:

sudo stunnel -c -d 8081 -r adwords.google.com:https

About SOAP

By the way, WSDL and SOAP are hideously complex, ill-defined "standards". Take a look at:

(Links supplied by Patrick Chanezon at Google).

Source code

You can browse the source code of this library here