2017-11-21 13 views
2

저는 golang을 처음 사용하여 gowsdl으로 비누를 사용하려고합니다.골란으로 SOAP 호출하기

wsdl 코드를 생성하여 패키지로 설치했습니다. 그러나 나는 그것으로부터 메소드를 호출하기위한 구문을 이해하기 위해 고심하고있다.

type AccountUser struct { 
    XMLName xml.Name `xml:"http://exacttarget.com/wsdl/partnerAPI AccountUser"` 

    *APIObject 

    AccountUserID    int32   `xml:"AccountUserID,omitempty"` 
    UserID     string  `xml:"UserID,omitempty"` 
    Password     string  `xml:"Password,omitempty"` 
    Name      string  `xml:"Name,omitempty"` 
    Email      string  `xml:"Email,omitempty"` 
    MustChangePassword  bool   `xml:"MustChangePassword,omitempty"` 
    ActiveFlag    bool   `xml:"ActiveFlag,omitempty"` 
    ChallengePhrase   string  `xml:"ChallengePhrase,omitempty"` 
    ChallengeAnswer   string  `xml:"ChallengeAnswer,omitempty"` 
    UserPermissions   []*UserAccess `xml:"UserPermissions,omitempty"` 
    Delete     int32   `xml:"Delete,omitempty"` 
    LastSuccessfulLogin  time.Time  `xml:"LastSuccessfulLogin,omitempty"` 
    IsAPIUser     bool   `xml:"IsAPIUser,omitempty"` 
    NotificationEmailAddress string  `xml:"NotificationEmailAddress,omitempty"` 
    IsLocked     bool   `xml:"IsLocked,omitempty"` 
    Unlock     bool   `xml:"Unlock,omitempty"` 
    BusinessUnit    int32   `xml:"BusinessUnit,omitempty"` 
    DefaultBusinessUnit  int32   `xml:"DefaultBusinessUnit,omitempty"` 
    DefaultApplication  string  `xml:"DefaultApplication,omitempty"` 
    Locale     *Locale  `xml:"Locale,omitempty"` 
    TimeZone     *TimeZone  `xml:"TimeZone,omitempty"` 
    DefaultBusinessUnitObject *BusinessUnit `xml:"DefaultBusinessUnitObject,omitempty"` 

    AssociatedBusinessUnits struct { 
     BusinessUnit []*BusinessUnit `xml:"BusinessUnit,omitempty"` 
    } `xml:"AssociatedBusinessUnits,omitempty"` 

    Roles struct { 
     Role []*Role `xml:"Role,omitempty"` 
    } `xml:"Roles,omitempty"` 

    LanguageLocale *Locale `xml:"LanguageLocale,omitempty"` 

    SsoIdentities struct { 
     SsoIdentity []*SsoIdentity `xml:"SsoIdentity,omitempty"` 
    } `xml:"SsoIdentities,omitempty"` 
} 

그리고 SOAP를 호출하는 방법이다 : 나는 패키지를 검토 할 때는

이 나는 ​​비누 본문에 원하는

입니다 내가로 패키지를 가져온

func (s *SOAPClient) Call(soapAction string, request, response interface{}) error { 
    envelope := SOAPEnvelope{ 
    //Header:  SoapHeader{}, 
    } 

    envelope.Body.Content = request 
    buffer := new(bytes.Buffer) 

    encoder := xml.NewEncoder(buffer) 
    //encoder.Indent(" ", " ") 

    if err := encoder.Encode(envelope); err != nil { 
     return err 
    } 

    if err := encoder.Flush(); err != nil { 
     return err 
    } 

    log.Println(buffer.String()) 

    req, err := http.NewRequest("POST", s.url, buffer) 
    if err != nil { 
     return err 
    } 
    if s.auth != nil { 
     req.SetBasicAuth(s.auth.Login, s.auth.Password) 
    } 

    req.Header.Add("Content-Type", "text/xml; charset=\"utf-8\"") 
    if soapAction != "" { 
     req.Header.Add("SOAPAction", soapAction) 
    } 

    req.Header.Set("User-Agent", "gowsdl/0.1") 
    req.Close = true 

    tr := &http.Transport{ 
     TLSClientConfig: &tls.Config{ 
      InsecureSkipVerify: s.tls, 
     }, 
     Dial: dialTimeout, 
    } 

    client := &http.Client{Transport: tr} 
    res, err := client.Do(req) 
    if err != nil { 
     return err 
    } 
    defer res.Body.Close() 

    rawbody, err := ioutil.ReadAll(res.Body) 
    if err != nil { 
     return err 
    } 
    if len(rawbody) == 0 { 
     log.Println("empty response") 
     return nil 
    } 

    log.Println(string(rawbody)) 
    respEnvelope := new(SOAPEnvelope) 
    respEnvelope.Body = SOAPBody{Content: response} 
    err = xml.Unmarshal(rawbody, respEnvelope) 
    if err != nil { 
     return err 
    } 

    fault := respEnvelope.Body.Fault 
    if fault != nil { 
     return fault 
    } 

    return nil 
} 

내 가서 파일을 호출하는 방법에 대한 포인터를 사랑합니다.

답변

2

생성 된 코드를 사용하려면 생성 된 "생성자"함수 NewSOAPClient 또는 NewSOAPClientWithTLSConfig 중 하나를 사용하여 먼저 비누 클라이언트를 초기화해야합니다.

그 후 당신이 Call 방법에 대한 요청 및 응답 인수로 사용할 수 있습니다 값을 준비해야합니다, 그들은 비누 요청/응답 페이로드의 본문 내용을 나타냅니다.

두 값의 유형은 어떤 종류의 호출을 할 것인지에 따라 달라집니다. 예를 들어, 가상 호출 create_account, update_account 및 delete_account는 일반적으로 다른 유형을 필요로합니다. 기본적으로, request 값의 유형은 SOAP 서비스의 문서화와 일치하는 XML에서 unmarshalable해야 marshalable 지정된 조치의 비누 서비스에 의해 예상되는 XML 일치하는 XML에하고 response의 유형을해야한다 지정된 조치에 대한 응답.


이 인위적인 예를 생각해

는 사용자를 만들 수있는 SOAP 서비스가있다. 서비스가있는 사용자를 만들려면 전자 메일과 암호를 보내야하며 모든 것이 정상이면 ID를 반환합니다. 이 경우 당신이 요청/응답 유형은 다음과 같을 것이다 :

type CreateUserRequest struct { 
    Email string `xml:"Email,omitempty"` 
    Password string `xml:"Password,omitempty"` 
} 

type CreateUserResponse struct { 
    ID string `xml:"ID"` 
} 

그런 다음 클라이언트 코드는 다음과 같습니다

client := NewSOAPClient("https://soap.example.com/call", true, nil) 

req := &CreateUserRequest{ 
    Email: "[email protected]", 
    Password: "1234567890", 
} 
res := &CreateUserResponse{} 
if err := client.Call("create_user", req, res); err != nil { 
    panic(err) 
} 

// if everything went well res.ID should have its 
// value set with the one returned by the service. 
fmt.Println(res.ID)