2012-09-04 4 views
11

애드워즈가 Google 제품이고 Go가 Google 제품인 경우, Go에 작성된 애드워즈 API 버전이있을 때까지 얼마나 걸립니까?Go에서 SOAP 호출을 수행하는 방법은 무엇입니까?

그 질문과 관련된 또 다른 : 거기에 아직 이동에 대한 SOAP 라이브러리가 있습니까?

+1

간단히 http 및 xml 패키지를 사용하여 SOAP 호출을 쉽게 수행 할 수 있습니다. –

+0

@dystroy : 당신은 좋은 지적을합니다. 그러나 저는 Go에서 시작했습니다. 아직 해결책이 아닙니다. – bugmagnet

+0

Go에서 SOAP 호출을 쉽게 수행하는 방법에 대해 자세히 설명해 주시겠습니까? –

답변

1

Google APIs for Go은 진행중인 작업입니다.

+0

아직 애드워즈에 대한 언급이 없습니다. 현재 Google API의 적용을 받고 있습니까? – Timm

32

애드워즈 API에 대한 답변은 회사의 발표가 없으며 출시가 외부에서 거의 수행되기 전에 시간을 예측할 수 있습니다.

두 번째 질문에 대답하겠습니다.

내가 가서 어떤 SOAP 라이브러리를 모른다 (go-lang.cat-v.org 하나를 참조하지 않는 것)하지만 대부분의 언어에서와 같이 간단한 SOAP 메시지를 처리하는 방법은 기본 httpxml 라이브러리를 사용하는 것입니다.

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

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

2)가 원하는 구조로 xml.NewDecoder을 사용하여 디코딩 :

개의 중요 연산은 POST 쿼리를 수행하여 응답을 얻을

1

)에 있습니다

다음은 Go (simplified from this)에서 수행 된 SOAP 쿼리의 전체 설명입니다 :

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 
} 
+0

위 예제에서 하드 코드 된 XML을 사용하고 있습니다. 비누 xml – user2383973

+1

을 생성 할 수있는 패키지가 있습니다.이 템플릿은 생성 된 XML을 하드 코딩 된 XML 이상으로 생성했습니다. encoding/xml을 사용하여 XML을 만들려고 시도 할 수 있지만 모든 서버가 다른 것을 원한다는 것을 빨리 알 수 있습니다. 따라서 내 경험에 따르면 서버에서 응답 할 때까지 XML을 수동으로 조작하는 것이 좋습니다. 라이브러리를 생성하고 라이브러리를 수정하여 일관되게 원하는 것을 생성 할 필요없이 원하는 코드를 하드 코딩해야합니다. – semi

관련 문제