...

Source file src/github.com/xanzy/go-gitlab/request_options.go

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, Sander van Harmelen
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package gitlab
    18  
    19  import (
    20  	"context"
    21  	"net/url"
    22  
    23  	retryablehttp "github.com/hashicorp/go-retryablehttp"
    24  )
    25  
    26  // RequestOptionFunc can be passed to all API requests to customize the API request.
    27  type RequestOptionFunc func(*retryablehttp.Request) error
    28  
    29  // WithContext runs the request with the provided context
    30  func WithContext(ctx context.Context) RequestOptionFunc {
    31  	return func(req *retryablehttp.Request) error {
    32  		*req = *req.WithContext(ctx)
    33  		return nil
    34  	}
    35  }
    36  
    37  // WithHeader takes a header name and value and appends it to the request headers.
    38  func WithHeader(name, value string) RequestOptionFunc {
    39  	return func(req *retryablehttp.Request) error {
    40  		req.Header.Set(name, value)
    41  		return nil
    42  	}
    43  }
    44  
    45  // WithHeaders takes a map of header name/value pairs and appends them to the
    46  // request headers.
    47  func WithHeaders(headers map[string]string) RequestOptionFunc {
    48  	return func(req *retryablehttp.Request) error {
    49  		for k, v := range headers {
    50  			req.Header.Set(k, v)
    51  		}
    52  		return nil
    53  	}
    54  }
    55  
    56  // WithKeysetPaginationParameters takes a "next" link from the Link header of a
    57  // response to a keyset-based paginated request and modifies the values of each
    58  // query parameter in the request with its corresponding response parameter.
    59  func WithKeysetPaginationParameters(nextLink string) RequestOptionFunc {
    60  	return func(req *retryablehttp.Request) error {
    61  		nextUrl, err := url.Parse(nextLink)
    62  		if err != nil {
    63  			return err
    64  		}
    65  		q := req.URL.Query()
    66  		for k, values := range nextUrl.Query() {
    67  			q.Del(k)
    68  			for _, v := range values {
    69  				q.Add(k, v)
    70  			}
    71  		}
    72  		req.URL.RawQuery = q.Encode()
    73  		return nil
    74  	}
    75  }
    76  
    77  // WithSudo takes either a username or user ID and sets the SUDO request header.
    78  func WithSudo(uid interface{}) RequestOptionFunc {
    79  	return func(req *retryablehttp.Request) error {
    80  		user, err := parseID(uid)
    81  		if err != nil {
    82  			return err
    83  		}
    84  		req.Header.Set("SUDO", user)
    85  		return nil
    86  	}
    87  }
    88  
    89  // WithToken takes a token which is then used when making this one request.
    90  func WithToken(authType AuthType, token string) RequestOptionFunc {
    91  	return func(req *retryablehttp.Request) error {
    92  		switch authType {
    93  		case JobToken:
    94  			req.Header.Set("JOB-TOKEN", token)
    95  		case OAuthToken:
    96  			req.Header.Set("Authorization", "Bearer "+token)
    97  		case PrivateToken:
    98  			req.Header.Set("PRIVATE-TOKEN", token)
    99  		}
   100  		return nil
   101  	}
   102  }
   103  

View as plain text