1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/abap/build/connector.go

107 lines
3.1 KiB
Go
Raw Normal View History

AAKaaS:1 check Component Versions (#2023) * adding my steps * messy step * Update abapEnvironmentAssembly.go * clean up * change yaml * corrections * Update cloudFoundryDeploy.go * update * delete simulation step * remove simulate * Update PiperGoUtils.groovy * Update PiperGoUtils.groovy * Update CommonStepsTest.groovy * add docu * Update abapEnvironmentAssembly.md * changes due to PR * Update .gitignore * b * CV list * Update abapEnvironmentAssembly.go * testing with simulation * Update abapEnvironmentAssembly.go * remove simulation * renaming * Update mkdocs.yml * moving service key to yaml and fixing code climate * Update abapEnvironmentAssemblePackages.go * Update abapEnvironmentAssemblePackages.go * Update abapEnvironmentAssemblePackages.go * Update abapEnvironmentAssemblePackages.go * change input * Update abapEnvironmentAssemblePackages.go * change json tag * fixed error handling * documentation * Update abapEnvironmentAssemblePackages.md * Update abapEnvironmentAssemblePackages.md * fixing code climate issues * fixing code climate issues * Update abapEnvironmentAssemblePackages.yaml * fixing code climate issues * Update abapEnvironmentAssemblePackages.yaml * adding unittests * adding unittests and improved logging * yaml -> json * change scope of cfServiceKeyName * correct indentation * Update CommonStepsTest.groovy * maintain correct step order * AAKaaS CheckCV step * AAKaaS CheckCV step #2 * AAKaaS CheckCV step #3 * AAKaaS CheckCV step #4 * AAKaaS CheckCV step #5 * AAKaaS CheckCV step #6 * AAKaaS CheckCV step #7 * AAKaaS CheckCV step #8 * AAKaaS CheckCV step #9 * AAKaaS CheckCV step #10 * AAKaaS CheckCV step #11 * AAKaaS CheckCV step #12 * AAKaaS CheckCV step #13 Co-authored-by: rosemarieB <45030247+rosemarieB@users.noreply.github.com> Co-authored-by: Daniel Mieg <56156797+DanielMieg@users.noreply.github.com> Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2020-09-17 11:01:19 +02:00
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 {
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
}