ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - zpatrick/rclient: Minimalistic REST client for Go applications
Minimalistic REST client for Go applications. Contribute to zpatrick/rclient development by creating an account on GitHub.
Visit Site

GitHub - zpatrick/rclient: Minimalistic REST client for Go applications

GitHub - zpatrick/rclient: Minimalistic REST client for Go applications

RClient

MIT License Go Report Card Go Doc

Getting Started

Checkout the Examples folder for some working examples. The following snippet shows RClient interacting with Github's API:

package main

import (
        "github.com/zpatrick/rclient"
        "log"
)

type Repository struct {
        Name        string `json:"name"`
}

func main() {
        client := rclient.NewRestClient("https://api.github.com")

        var repos []Repository
        if err := client.Get("/users/zpatrick/repos", &repos); err != nil {
                log.Fatal(err)
        }

        log.Println(repos)
}

Request Options

Requests can be configured using a Request Option. A RequestOption is simply a function that manipulates an http.Request. You can create request options like so:

setProto := func(req *http.Request) error {
    req.Proto = "HTTP/1.0"
    return nil
}

client.Get("/path", &v, setProto)

The built-in request options are described below.

Header / Headers

The Header() and Headers() options add header(s) to a *http.Request.

// add a single header
client.Get("/path", &v, rclient.Header("name", "val"))

// add multiple headers
client.Get("/path", &v, rclient.Header("name1", "val1"), rclient.Header("name2", "val2"))
client.Get("/path", &v, rclient.Headers(map[string]string{"name1": "val1", "name2":"val2"}))

Basic Auth

The BasicAuth() option adds basic auth to a *http.Request.

client.Get("/path", &v, rclient.BasicAuth("user", "pass"))

Query

The Query() options adds a query to a *http.Request.

query := url.Values{}
query.Add("name", "John")
query.Add("age", "35")

client.Get("/path", &v, rclient.Query(query))

NOTE: This can also be accomplished by adding the raw query to the path argument

client.Get("/path?name=John&age=35", &v)

Client Configuration

The RestClient can be configured using the Client Options described below.

Doer

The Doer() option sets the RequestDoer field on the RestClient. This is the http.DefaultClient by default, and it can be set to anything that satisfies the RequestDoer interface.

client, err := rclient.NewRestClient("https://api.github.com", rclient.Doer(&http.Client{}))

Request Options

The RequestOptions() option sets the RequestOptions field on the RestClient. This will manipulate each request made by the RestClient. This can be any of the options described in the Request Options section. A typical use-case would be adding headers for each request.

options := []rclient.RequestOption{
    rclient.Header("name", "John Doe").
    rclient.Header("token", "abc123"),
}

client, err := rclient.NewRestClient("https://api.github.com", rclient.RequestOptions(options...))

Builder

The Builder() option sets the RequestBuilder field on the RestClient. This field is responsible for building *http.Request objects. This is the BuildJSONRequest function by default, and it can be set to any RequestBuilder function.

builder := func(method, url string, body interface{}, options ...RequestOption) (*http.Request, error){
    req, _ := http.NewRequest(method, url, nil)
    for _, option := range options {
		if err := option(req); err != nil {
			return nil, err
		}
	}
	
    return nil, errors.New("I forgot to add a body to the request!")
}

client, err := rclient.NewRestClient("https://api.github.com", rclient.Builder(builder))

Reader

The Reader() option sets the ResponseReader field on the RestClient. This field is responsible for reading *http.Response objects. This is the ReadJSONResponse function by default, and it can be set to any ResponseReader function.

reader := func(resp *http.Response, v interface{}) error{
    defer resp.Body.Close()
    return json.NewDecoder(resp.Body).Decode(v)
}

client, err := rclient.NewRestClient("https://api.github.com", rclient.Reader(reader))

License

This work is published under the MIT license.

Please see the LICENSE file for details.

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory