15

Given that Adwords is a Google thing, and Go is a Google thing, how long until there's a version of the Adwords API written in Go?

Associated with that question, another: are there any SOAP libraries for Go yet?

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
bugmagnet
  • 7,631
  • 8
  • 69
  • 131

2 Answers2

37

I can' answer about the adwords API as I saw no announcement from the company and predicting the time before a release can hardly be done from outside.

So I'll answer your second question.

I don't know any SOAP library in Go (go-lang.cat-v.org doesn't seem to reference one) but as in most languages, a way to deal with simple SOAP messages is to use the basic http and xml libraries.

The two important operations are

1) to get an answer by doing a POST query :

resp, err := httpClient.Post(query, "text/xml; charset=utf-8", someXMLasBytes)

2) to decode it using a xml.NewDecoder into the desired structure :

parser := xml.NewDecoder(bytes.NewBufferString(in)) 
err = parser.DecodeElement(&envelope, nil)

Here's a complete and commented example of a SOAP query done in Go (simplified from this) :

package main   

import (
    "bytes"
    "encoding/xml"
    "fmt"
    "io"
    "io/ioutil"
    "net/http"
    "strings"
)

// The URL of the SOAP server
const MH_SOAP_URL = "http://sp.mountyhall.com/SP_WebService.php"

// this is just the message I'll send for interrogation, with placeholders
//  for my parameters
const SOAP_VUE_QUERY_FORMAT = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:tns=\"urn:SP_WebService\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" ><SOAP-ENV:Body><mns:Vue xmlns:mns=\"uri:mhSp\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><numero xsi:type=\"xsd:string\">%d</numero><mdp xsi:type=\"xsd:string\">%s</mdp></mns:Vue></SOAP-ENV:Body></SOAP-ENV:Envelope>"

// Here I define Go structures, almost identical to the structure of the
// XML message we'll fetch
// Note that annotations (the string "return>item") allow to have a slightly
//  different structure or different namings

type SoapItem struct {
    Numero    int
    Nom       string
    Type      string
    PositionX int
    PositionY int
    PositionN int
    Monde     int
}
type SoapVue struct {
    Items []SoapItem "return>item" 
}
type SoapFault struct {
    Faultstring string
    Detail      string
}
type SoapBody struct {
    Fault          SoapFault
    ProfilResponse SoapProfil
    VueResponse    SoapVue
}
type SoapEnvelope struct {
    XMLName xml.Name
    Body    SoapBody
}

// Here is the function querying the SOAP server
// It returns the whole answer as a Go structure (a SoapEnvelope)
// You could also return an error in a second returned parameter
func GetSoapEnvelope(query string, numero int, mdp string) (envelope *SoapEnvelope) {
    soapRequestContent := fmt.Sprintf(query, numero, mdp)
    httpClient := new(http.Client)
    resp, err := httpClient.Post(MH_SOAP_URL, "text/xml; charset=utf-8", bytes.NewBufferString(soapRequestContent)) 
    if err != nil {
        // handle error
    }
    b, e := ioutil.ReadAll(resp.Body) // probably not efficient, done because the stream isn't always a pure XML stream and I have to fix things (not shown here)
    if e != nil {
        // handle error
    }
    in := string(b)
    parser := xml.NewDecoder(bytes.NewBufferString(in))
    envelope = new(SoapEnvelope) // this allocates the structure in which we'll decode the XML
    err = parser.DecodeElement(&envelope, nil)
    if err != nil {
        // handle error
    }
    resp.Body.Close()
    return
}
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • In the above example you are using hardcoded xml is there any package where we could generate the soap xml – user Apr 21 '14 at 09:05
  • 1
    I'd call that template generated XML more than hardcoded xml. You could attempt to generate the XML with encoding/xml, but you'll quickly find that every server wants something different, so in my experience at least you're better off just manually fiddling with the XML until you get the server to respond how you want and then just hardcoding it, rather than having a library generate it for you and having to tweak the library until it generates what you want consistently. – semi Aug 22 '14 at 21:41
1

The Google APIs for Go is a work in progress.

peterSO
  • 158,998
  • 31
  • 281
  • 276
  • I don't see any mention of AdWords there yet. Are you sure that is covered by the current Google APIs? – Timm Aug 07 '14 at 15:56