go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/r2/opt_url.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  	"fmt"
    12  	"net/url"
    13  )
    14  
    15  // OptURL sets the underlying request url directly.
    16  func OptURL(u *url.URL) Option {
    17  	return func(r *Request) error {
    18  		r.req.URL = u
    19  		return nil
    20  	}
    21  }
    22  
    23  // OptURLParsed sets the url of a request by parsing
    24  // the given raw url.
    25  func OptURLParsed(rawURL string) Option {
    26  	return func(r *Request) error {
    27  		if r.req == nil {
    28  			return ErrRequestUnset
    29  		}
    30  		var err error
    31  		r.req.URL, err = url.Parse(rawURL)
    32  		return err
    33  	}
    34  }
    35  
    36  // OptScheme sets the url scheme of a request.
    37  func OptScheme(scheme string) Option {
    38  	return func(r *Request) error {
    39  		if r.req == nil {
    40  			return ErrRequestUnset
    41  		}
    42  		r.req.URL.Scheme = scheme
    43  		return nil
    44  	}
    45  }
    46  
    47  // OptHost sets the url host of a request.
    48  func OptHost(host string) Option {
    49  	return func(r *Request) error {
    50  		if r.req == nil {
    51  			return ErrRequestUnset
    52  		}
    53  		r.req.URL.Host = host
    54  		return nil
    55  	}
    56  }
    57  
    58  // OptPath sets the url path of a request.
    59  func OptPath(path string) Option {
    60  	return func(r *Request) error {
    61  		if r.req == nil {
    62  			return ErrRequestUnset
    63  		}
    64  		r.req.URL.Path = path
    65  		return nil
    66  	}
    67  }
    68  
    69  // OptPathf sets the url path of a request.
    70  func OptPathf(format string, args ...any) Option {
    71  	return func(r *Request) error {
    72  		if r.req == nil {
    73  			return ErrRequestUnset
    74  		}
    75  		r.req.URL.Path = fmt.Sprintf(format, args...)
    76  		return nil
    77  	}
    78  }