package build import ( "bytes" "io/ioutil" "net/http" "net/http/cookiejar" piperhttp "github.com/SAP/jenkins-library/pkg/http" "github.com/pkg/errors" ) // Connector : Connector Utility Wrapping http client type Connector struct { Client piperhttp.Sender DownloadClient piperhttp.Downloader Header map[string][]string Baseurl string } // ******** technical communication calls ******** // GetToken : Get the X-CRSF Token from ABAP Backend for later post func (conn *Connector) GetToken(appendum string) error { url := conn.Baseurl + appendum conn.Header["X-CSRF-Token"] = []string{"Fetch"} response, err := conn.Client.SendRequest("HEAD", url, nil, conn.Header, nil) if err != nil { if response == nil { return errors.Wrap(err, "Fetching X-CSRF-Token failed") } defer response.Body.Close() errorbody, _ := ioutil.ReadAll(response.Body) return errors.Wrapf(err, "Fetching X-CSRF-Token failed: %v", string(errorbody)) } defer response.Body.Close() token := response.Header.Get("X-CSRF-Token") conn.Header["X-CSRF-Token"] = []string{token} return nil } // Get : http get request func (conn Connector) Get(appendum string) ([]byte, error) { url := conn.Baseurl + appendum response, err := conn.Client.SendRequest("GET", url, nil, conn.Header, nil) if err != nil { if response == nil || response.Body == nil { return nil, errors.Wrap(err, "Get failed") } defer response.Body.Close() errorbody, _ := ioutil.ReadAll(response.Body) return errorbody, errors.Wrapf(err, "Get failed: %v", string(errorbody)) } defer response.Body.Close() body, err := ioutil.ReadAll(response.Body) return body, err } // Post : http post request func (conn Connector) Post(appendum string, importBody string) ([]byte, error) { url := conn.Baseurl + appendum var response *http.Response var err error if importBody == "" { response, err = conn.Client.SendRequest("POST", url, nil, conn.Header, nil) } else { response, err = conn.Client.SendRequest("POST", url, bytes.NewBuffer([]byte(importBody)), conn.Header, nil) } if err != nil { if response == nil { return nil, errors.Wrap(err, "Post failed") } defer response.Body.Close() errorbody, _ := ioutil.ReadAll(response.Body) return errorbody, errors.Wrapf(err, "Post failed: %v", string(errorbody)) } defer response.Body.Close() body, err := ioutil.ReadAll(response.Body) return body, err } // Download : download a file via http func (conn Connector) Download(appendum string, downloadPath string) error { url := conn.Baseurl + appendum err := conn.DownloadClient.DownloadFile(url, downloadPath, nil, nil) return err } // InitAAKaaS : initializie Connector for communication with AAKaaS backend func (conn *Connector) InitAAKaaS(aAKaaSEndpoint string, username string, password string, inputclient piperhttp.Sender) { conn.Client = inputclient conn.Header = make(map[string][]string) conn.Header["Accept"] = []string{"application/json"} conn.Header["Content-Type"] = []string{"application/json"} cookieJar, _ := cookiejar.New(nil) conn.Client.SetOptions(piperhttp.ClientOptions{ Username: username, Password: password, CookieJar: cookieJar, }) conn.Baseurl = aAKaaSEndpoint } // UploadSarFile : upload *.sar file func (conn Connector) UploadSarFile(appendum string, sarFile []byte) error { url := conn.Baseurl + appendum response, err := conn.Client.SendRequest("PUT", url, bytes.NewBuffer(sarFile), conn.Header, nil) if err != nil { defer response.Body.Close() errorbody, _ := ioutil.ReadAll(response.Body) return errors.Wrapf(err, "Upload of SAR file failed: %v", string(errorbody)) } defer response.Body.Close() return nil }