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

200 lines
6.2 KiB
Go
Raw Normal View History

package http
import (
2020-01-14 11:29:50 +02:00
"bytes"
"fmt"
"io"
2020-01-14 11:29:50 +02:00
"mime/multipart"
"net/http"
2020-01-14 11:29:50 +02:00
"os"
"strings"
"time"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/pkg/errors"
2020-01-14 11:29:50 +02:00
"github.com/sirupsen/logrus"
)
// Client defines an http client object
type Client struct {
timeout time.Duration
username string
password string
token string
2020-01-14 11:29:50 +02:00
logger *logrus.Entry
}
// ClientOptions defines the options to be set on the client
type ClientOptions struct {
Timeout time.Duration
Username string
Password string
Token string
}
2020-01-14 11:29:50 +02:00
// Sender provides an interface to the piper http client for uid/pwd and token authenticated requests
type Sender interface {
SendRequest(method, url string, body io.Reader, header http.Header, cookies []*http.Cookie) (*http.Response, error)
SetOptions(options ClientOptions)
}
2020-01-14 11:29:50 +02:00
// Uploader provides an interface to the piper http client for uid/pwd and token authenticated requests with upload capabilities
type Uploader interface {
SendRequest(method, url string, body io.Reader, header http.Header, cookies []*http.Cookie) (*http.Response, error)
2020-01-22 15:22:04 +02:00
UploadRequest(method, url, file, fieldName string, header http.Header, cookies []*http.Cookie) (*http.Response, error)
2020-01-14 11:29:50 +02:00
UploadFile(url, file, fieldName string, header http.Header, cookies []*http.Cookie) (*http.Response, error)
SetOptions(options ClientOptions)
}
// UploadFile uploads a file's content as multipart-form POST request to the specified URL
func (c *Client) UploadFile(url, file, fieldName string, header http.Header, cookies []*http.Cookie) (*http.Response, error) {
2020-01-22 15:22:04 +02:00
return c.UploadRequest(http.MethodPost, url, file, fieldName, header, cookies)
}
// UploadRequest uploads a file's content as multipart-form with given http method request to the specified URL
func (c *Client) UploadRequest(method, url, file, fieldName string, header http.Header, cookies []*http.Cookie) (*http.Response, error) {
if method != http.MethodPost && method != http.MethodPut {
return nil, errors.New(fmt.Sprintf("Http method %v is not allowed. Possible values are %v or %v", method, http.MethodPost, http.MethodPut))
}
2020-01-14 11:29:50 +02:00
httpClient := c.initialize()
bodyBuffer := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuffer)
fileWriter, err := bodyWriter.CreateFormFile(fieldName, file)
if err != nil {
return &http.Response{}, errors.Wrapf(err, "error creating form file %v for field %v", file, fieldName)
}
fileHandle, err := os.Open(file)
if err != nil {
return &http.Response{}, errors.Wrapf(err, "unable to locate file %v", file)
}
defer fileHandle.Close()
_, err = io.Copy(fileWriter, fileHandle)
if err != nil {
return &http.Response{}, errors.Wrapf(err, "unable to copy file content of %v into request body", file)
}
err = bodyWriter.Close()
request, err := c.createRequest(method, url, bodyBuffer, &header, cookies)
if err != nil {
c.logger.Debugf("New %v request to %v", method, url)
return &http.Response{}, errors.Wrapf(err, "error creating %v request to %v", method, url)
}
startBoundary := strings.Index(bodyWriter.FormDataContentType(), "=") + 1
boundary := bodyWriter.FormDataContentType()[startBoundary:]
request.Header.Add("Content-Type", "multipart/form-data; boundary=\""+boundary+"\"")
request.Header.Add("Connection", "Keep-Alive")
response, err := httpClient.Do(request)
if err != nil {
return response, errors.Wrapf(err, "HTTP %v request to %v failed with error", method, url)
}
return c.handleResponse(response)
}
// SendRequest sends an http request with a defined method
func (c *Client) SendRequest(method, url string, body io.Reader, header http.Header, cookies []*http.Cookie) (*http.Response, error) {
2020-01-14 11:29:50 +02:00
httpClient := c.initialize()
2020-01-14 11:29:50 +02:00
request, err := c.createRequest(method, url, body, &header, cookies)
if err != nil {
c.logger.Debugf("New %v request to %v", method, url)
return &http.Response{}, errors.Wrapf(err, "error creating %v request to %v", method, url)
}
2020-01-14 11:29:50 +02:00
response, err := httpClient.Do(request)
if err != nil {
return response, errors.Wrapf(err, "error opening %v", url)
}
return c.handleResponse(response)
}
// SetOptions sets options used for the http client
func (c *Client) SetOptions(options ClientOptions) {
c.timeout = options.Timeout
c.username = options.Username
c.password = options.Password
c.token = options.Token
c.logger = log.Entry().WithField("package", "SAP/jenkins-library/pkg/http")
}
func (c *Client) initialize() *http.Client {
c.applyDefaults()
var httpClient = &http.Client{
Timeout: c.timeout,
}
2020-01-14 11:29:50 +02:00
c.logger.Debugf("Timeout set to %v", c.timeout)
return httpClient
}
2020-01-14 11:29:50 +02:00
func (c *Client) createRequest(method, url string, body io.Reader, header *http.Header, cookies []*http.Cookie) (*http.Request, error) {
c.logger.Debugf("New %v request to %v", method, url)
request, err := http.NewRequest(method, url, body)
if err != nil {
2020-01-14 11:29:50 +02:00
return &http.Request{}, err
}
if header != nil {
2020-01-14 11:29:50 +02:00
for name, headers := range *header {
for _, h := range headers {
request.Header.Add(name, h)
}
}
}
if cookies != nil {
for _, cookie := range cookies {
request.AddCookie(cookie)
}
}
if len(c.username) > 0 && len(c.password) > 0 {
request.SetBasicAuth(c.username, c.password)
2020-01-14 11:29:50 +02:00
c.logger.Debug("Using Basic Authentication ****/****")
}
2020-01-14 11:29:50 +02:00
if len(c.token) > 0 {
request.Header.Add("Authorization", c.token)
}
2020-01-14 11:29:50 +02:00
return request, nil
}
func (c *Client) handleResponse(response *http.Response) (*http.Response, error) {
// 2xx codes do not create an error
if response.StatusCode >= 200 && response.StatusCode < 300 {
return response, nil
}
switch response.StatusCode {
case http.StatusUnauthorized:
2020-01-14 11:29:50 +02:00
c.logger.WithField("HTTP Error", "401 (Unauthorized)").Error("Credentials invalid, please check your user credentials!")
case http.StatusForbidden:
2020-01-14 11:29:50 +02:00
c.logger.WithField("HTTP Error", "403 (Forbidden)").Error("Permission issue, please check your user permissions!")
case http.StatusNotFound:
2020-01-14 11:29:50 +02:00
c.logger.WithField("HTTP Error", "404 (Not Found)").Error("Requested resource could not be found")
case http.StatusInternalServerError:
2020-01-14 11:29:50 +02:00
c.logger.WithField("HTTP Error", "500 (Internal Server Error)").Error("Unknown error occured.")
}
2020-01-14 11:29:50 +02:00
return response, fmt.Errorf("Request to %v returned with HTTP Code %v", response.Request.URL, response.StatusCode)
}
func (c *Client) applyDefaults() {
if c.timeout == 0 {
c.timeout = time.Second * 10
}
}