LoginSignup
16
17

More than 5 years have passed since last update.

Cybozu ガルーン API を golang で叩いてみる

Last updated at Posted at 2014-06-18

golangで叩く

前回は手動で叩いたので、golangで叩く部分を書いてみる

golangには良さげなSOAPライブラリが無いので、
XMLの組み立ては手を抜いてtemplateエンジンを使うことに。

リクエストbodyのテンプレートを用意

const SOAPRequest = `<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
 xmlns:base_services="http://wsdl.cybozu.co.jp/base/2008">
 <SOAP-ENV:Header>
  <Action SOAP-ENV:mustUnderstand="1" xmlns="http://schemas.xmlsoap.org/ws/2003/03/addressing">{{.Action}}</Action>
  <Security xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility"
   SOAP-ENV:mustUnderstand="1"
   xmlns="http://schemas.xmlsoap.org/ws/2002/12/secext">
   <UsernameToken wsu:Id="id"><Username>{{.Username}}</Username><Password>{{.Password}}</Password></UsernameToken>
  </Security>
  <Timestamp SOAP-ENV:mustUnderstand="1" Id="id"
   xmlns="http://schemas.xmlsoap.org/ws/2002/07/utility">
   <Created>2037-08-12T14:45:00Z</Created>
   <Expires>2037-08-12T14:45:00Z</Expires>
  </Timestamp>
  <Locale>jp</Locale>
  </SOAP-ENV:Header><SOAP-ENV:Body>
  <{{.Action}}>
  {{.Parameters}}
 </{{.Action}}>
</SOAP-ENV:Body></SOAP-ENV:Envelope>
`

type APIParameters struct {
        Username, Password, Action, Parameters string
}

Postしてみる

func main() {
        var api_parameters = APIParameters{"ID", "パスワード", "ScheduleGetEvents", `<parameters start="2014-01-01T08:00:00" end="2014-01-30T20:00:00" ></parameters>`}
        var url = "https://ほげほげほげ/grn.cgi/cbpapi/schedule/api?"

        //リクエストbodyのテンプレートを適用
        t := template.Must(template.New("SOAPRequest").Parse(SOAPRequest))
        body := bytes.NewBufferString("")
        err := t.Execute(body, api_parameters)
        if err != nil {
                log.Panicf("template.Execute: %#v", err)
        }

        // Postする
        response, err := http.Post(url, "text/xml; charset=utf-8", body)
        if err != nil {
                log.Panicf("http.Post: %#v", err)
        }

        //レスポンスを読み込んで表示
        b, err := ioutil.ReadAll(response.Body)
        response.Body.Close()
        if err != nil {
                log.Panicf("ioutil.ReadAll: %#v", err)
        }
        fmt.Printf(string(b))
        return
}

取れた。後はXMLをパースするだけ

16
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
17