go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/r2/opt_query.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package r2
     9  
    10  import (
    11  	"net/url"
    12  )
    13  
    14  // OptRawQuery sets the request url query string.
    15  func OptRawQuery(rawQuery string) Option {
    16  	return func(r *Request) error {
    17  		if r.req.URL == nil {
    18  			r.req.URL = new(url.URL)
    19  		}
    20  		r.req.URL.RawQuery = rawQuery
    21  		return nil
    22  	}
    23  }
    24  
    25  // OptRawQueryValues sets the request url query string.
    26  func OptRawQueryValues(q url.Values) Option {
    27  	return func(r *Request) error {
    28  		if r.req.URL == nil {
    29  			r.req.URL = new(url.URL)
    30  		}
    31  		r.req.URL.RawQuery = q.Encode()
    32  		return nil
    33  	}
    34  }
    35  
    36  // OptQueryAdd adds a header value on a request.
    37  func OptQueryAdd(key, value string) Option {
    38  	return func(r *Request) error {
    39  		if r.req.URL == nil {
    40  			r.req.URL = new(url.URL)
    41  		}
    42  		existing := r.req.URL.Query()
    43  		existing.Add(key, value)
    44  		r.req.URL.RawQuery = existing.Encode()
    45  		return nil
    46  	}
    47  }
    48  
    49  // OptQuery is an alias to `r2.OptQuerySet` and sets a
    50  // query string value on the request url by key and value.
    51  func OptQuery(key, value string) Option {
    52  	return OptQuerySet(key, value)
    53  }
    54  
    55  // OptQuerySet sets a header value on a request.
    56  func OptQuerySet(key, value string) Option {
    57  	return func(r *Request) error {
    58  		if r.req.URL == nil {
    59  			r.req.URL = new(url.URL)
    60  		}
    61  		existing := r.req.URL.Query()
    62  		existing.Set(key, value)
    63  		r.req.URL.RawQuery = existing.Encode()
    64  		return nil
    65  	}
    66  }
    67  
    68  // OptQueryDel deletes a header by key on a request.
    69  func OptQueryDel(key string) Option {
    70  	return func(r *Request) error {
    71  		if r.req.URL == nil {
    72  			r.req.URL = new(url.URL)
    73  		}
    74  		existing := r.req.URL.Query()
    75  		existing.Del(key)
    76  		r.req.URL.RawQuery = existing.Encode()
    77  		return nil
    78  	}
    79  }