github.com/blend/go-sdk@v1.20220411.3/r2/doc.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  /*
     9  Package r2 is a rewrite of the sdk http request package that eschews fluent apis in favor of the options pattern.
    10  
    11  The request returned by `r2.New()` i.e. `*r2.Request` holds everything required to send the request, including the http client reference, and a transport reference. If neither are specified, defaults are used (http.DefaultClient for the client, etc.)
    12  
    13  To send a request, simply:
    14  
    15  	resp, err := r2.New("http://example.com/").Do()
    16  	if err != nil {
    17  		// handle error
    18  	}
    19  	defer resp.Body.Close()
    20  	body, err := io.ReadAll(resp.Body)
    21  	// ...
    22  
    23  You can specify additional options as a variadic list of `Opt...` functions:
    24  
    25  	resp, err := r2.New("http://example.com",
    26  		r2.OptPost(),
    27  		r2.OptHeaderValue("X-Foo", "example-string"),
    28  	).Do()
    29  
    30  There are convenience methods on the request type that help with things like decoding types as json:
    31  
    32  	meta, err := r2.New("http://example.com",
    33  		r2.OptPost(),
    34  		r2.OptHeaderValue("X-Foo", "example-string"),
    35  	).JSON(&myObj)
    36  
    37  Note that in the above, the JSON method also returns a closed version of the response for metadata purposes.
    38  
    39  You can also fire and forget the request with the `.Discard()` method:
    40  
    41  	meta, err := r2.New("http://example.com",
    42  		r2.OptPost(),
    43  		r2.OptHeaderValue("X-Foo", "example-string"),
    44  	).Discard()
    45  
    46  
    47  */
    48  package r2 // import "github.com/blend/go-sdk/r2"