comparison pkg/soap/soap.go @ 3324:96dbfcc614be

Backed out changeset bd1385c00b59
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 20 May 2019 12:10:44 +0200
parents bd1385c00b59
children d38b20ccb6f9
comparison
equal deleted inserted replaced
3323:de0d0685a17b 3324:96dbfcc614be
228 228
229 func (s *SOAPClient) AddHeader(header interface{}) { 229 func (s *SOAPClient) AddHeader(header interface{}) {
230 s.headers = append(s.headers, header) 230 s.headers = append(s.headers, header)
231 } 231 }
232 232
233 func shorten(s string) string {
234 if len(s) < 40 {
235 return s
236 }
237 return fmt.Sprintf("%.40q...", s)
238 }
239
240 func (s *SOAPClient) Call(soapAction string, request, response interface{}) error { 233 func (s *SOAPClient) Call(soapAction string, request, response interface{}) error {
241 envelope := SOAPEnvelope{} 234 envelope := SOAPEnvelope{}
242 235
243 if s.headers != nil && len(s.headers) > 0 { 236 if s.headers != nil && len(s.headers) > 0 {
244 soapHeader := &SOAPHeader{Items: make([]interface{}, len(s.headers))} 237 soapHeader := &SOAPHeader{Items: make([]interface{}, len(s.headers))}
300 293
301 res, err := client.Do(req) 294 res, err := client.Do(req)
302 if err != nil { 295 if err != nil {
303 return err 296 return err
304 } 297 }
298 if res.StatusCode < http.StatusOK || res.StatusCode > 299 {
299 return fmt.Errorf(
300 "HTTP error: %d (%s)", res.StatusCode, http.StatusText(res.StatusCode))
301 }
305 defer res.Body.Close() 302 defer res.Body.Close()
306 303
307 rawbody, err := ioutil.ReadAll(res.Body) 304 rawbody, err := ioutil.ReadAll(res.Body)
308 if err != nil { 305 if err != nil {
309 return err 306 return err
310 } 307 }
311 308 if len(rawbody) == 0 {
312 if res.StatusCode < http.StatusOK || res.StatusCode > 299 { 309 log.Println("empty response")
313 short := shorten(string(rawbody)) 310 return nil
314 log.Printf("error: response from SOAP service:\n%s", short) 311 }
315 return fmt.Errorf("Error requesting SOAP service: %d (%s): %s", 312
316 res.StatusCode, http.StatusText(res.StatusCode), short) 313 //log.Println(string(rawbody))
317 }
318
319 respEnvelope := new(SOAPEnvelope) 314 respEnvelope := new(SOAPEnvelope)
320 respEnvelope.Body = SOAPBody{Content: response} 315 respEnvelope.Body = SOAPBody{Content: response}
321 err = xml.Unmarshal(rawbody, respEnvelope) 316 err = xml.Unmarshal(rawbody, respEnvelope)
322 if err != nil { 317 if err != nil {
323 return err 318 return err
324 } 319 }
325 320
326 return respEnvelope.Body.Fault 321 fault := respEnvelope.Body.Fault
327 } 322 if fault != nil {
323 return fault
324 }
325
326 return nil
327 }