All Projects → go-pkgz → requester

go-pkgz / requester

Licence: MIT license
The package provides a very thin wrapper (no external dependencies) for http.Client allowing the use of layers (middleware).

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to requester

Requests
Convenient http client for java, inspired by python request module
Stars: ✭ 459 (+3178.57%)
Mutual labels:  http-client, http-requests
Easygo
基于Kotlin、OkHttp的声明式网络框架,像写HTML界面一样写网络调用代码
Stars: ✭ 40 (+185.71%)
Mutual labels:  http-client, http-requests
Httpu
The terminal-first http client
Stars: ✭ 619 (+4321.43%)
Mutual labels:  http-client, http-requests
Node Request Retry
💂 Wrap NodeJS request module to retry http requests in case of errors
Stars: ✭ 330 (+2257.14%)
Mutual labels:  http-client, http-requests
swish
C++ HTTP requests for humans
Stars: ✭ 52 (+271.43%)
Mutual labels:  http-client, http-requests
Http Shortcuts
Android app to create home screen shortcuts that trigger arbitrary HTTP requests
Stars: ✭ 329 (+2250%)
Mutual labels:  http-client, http-requests
Gout
gout to become the Swiss Army Knife of the http client @^^@---> gout 是http client领域的瑞士军刀,小巧,强大,犀利。具体用法可看文档,如使用迷惑或者API用得不爽都可提issues
Stars: ✭ 749 (+5250%)
Mutual labels:  http-client, http-requests
nativescript-http
The best way to do HTTP requests in NativeScript, a drop-in replacement for the core HTTP with important improvements and additions like proper connection pooling, form data support and certificate pinning
Stars: ✭ 32 (+128.57%)
Mutual labels:  http-client, http-requests
pawn-requests
pawn-requests provides an API for interacting with HTTP(S) JSON APIs.
Stars: ✭ 56 (+300%)
Mutual labels:  http-client, http-requests
EthernetWebServer SSL
Simple TLS/SSL Ethernet WebServer, HTTP Client and WebSocket Client library for for AVR, Portenta_H7, Teensy, SAM DUE, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, nRF52 and RASPBERRY_PI_PICO boards using Ethernet shields W5100, W5200, W5500, ENC28J60 or Teensy 4.1 NativeEthernet/QNEthernet. It now supports Ethernet TLS/SSL Client. The library supports …
Stars: ✭ 40 (+185.71%)
Mutual labels:  http-client, http-requests
robotframework-httprequestlibrary
Robot Framework's library to test REST interfaces utilizing Apache HttpClient
Stars: ✭ 20 (+42.86%)
Mutual labels:  http-client, http-requests
1c http
Подсистема 1С для работы с HTTP
Stars: ✭ 48 (+242.86%)
Mutual labels:  http-client, http-requests
EthernetWebServer
This is simple yet complete WebServer library for AVR, Portenta_H7, Teensy, SAM DUE, SAMD21/SAMD51, nRF52, STM32, RP2040-based, etc. boards running Ethernet shields. The functions are similar and compatible to ESP8266/ESP32 WebServer libraries to make life much easier to port sketches from ESP8266/ESP32. Coexisting now with `ESP32 WebServer` and…
Stars: ✭ 118 (+742.86%)
Mutual labels:  http-client, http-requests
Karin
An elegant promise based HTTP client for the browser and node.js [WIP]
Stars: ✭ 393 (+2707.14%)
Mutual labels:  http-client, http-requests
Redes
High-level network layer abstraction library written in Swift.
Stars: ✭ 16 (+14.29%)
Mutual labels:  http-client, http-requests
Restclient
🦄 Simple HTTP and REST client for Unity based on Promises, also supports Callbacks! 🎮
Stars: ✭ 675 (+4721.43%)
Mutual labels:  http-client, http-requests
centra
Core Node.js HTTP client
Stars: ✭ 52 (+271.43%)
Mutual labels:  http-client, http-requests
Frequest
FRequest - A fast, lightweight and opensource desktop application to make HTTP(s) requests
Stars: ✭ 130 (+828.57%)
Mutual labels:  http-client, http-requests
direwolf
Package direwolf is a convenient and easy to use http client written in Golang.
Stars: ✭ 44 (+214.29%)
Mutual labels:  http-client, http-requests
relay
Relay lets you write HTTP requests as easy to read, structured YAML and dispatch them easily using a CLI. Similar to tools like Postman
Stars: ✭ 22 (+57.14%)
Mutual labels:  http-client, http-requests

Requester

Build Status Coverage Status Go Reference

The package provides a very thin wrapper (no external dependencies) for http.Client, allowing to use of layers (middlewares) on http.RoundTripper level. The goal is to keep the way users leverage stdlib http client but add a few useful extras on top of the standard http.Client.

Pls note: this is not a replacement for http.Client but rather a companion library.

    rq := requester.New(                        // make the requester
        http.Client{Timeout: 5*time.Second},    // set http client
        requester.MaxConcurrent(8),             // maximum number of concurrent requests
        requester.JSON,                         // set json headers
        requester.Header("X-AUTH", "123456789"),// set some auth header
        requester.Logger(requester.StdLogger),  // enable logging to stdout
    )
    
    req := http.NewRequest("GET", "http://example.com/api", nil) // create the usual http.Request
    req.Header.Set("foo", "bar") // do the usual things with request, for example set some custome headers
    resp, err := rq.Do(req) // instead of client.Do call requester.Do

Install and update

go get -u github.com/go-pkgz/requester

Requester middlewares

Overview

  • Header - appends user-defined headers to all requests.
  • JSON - sets headers "Content-Type": "application/json" and "Accept": "application/json"
  • BasicAuth(user, passwd string) - adds HTTP Basic Authentication
  • MaxConcurrent - sets maximum concurrency
  • Repeater - sets repeater to retry failed requests. Doesn't provide repeater implementation but wraps it. Compatible with any repeater (for example go-pkgz/repeater) implementing a single method interface Do(ctx context.Context, fun func() error, errors ...error) (err error) interface.
  • Cache - sets any LoadingCache implementation to be used for request/response caching. Doesn't provide cache, but wraps it. Compatible with any cache (for example a family of caches from go-pkgz/lcw) implementing a single-method interface Get(key string, fn func() (interface{}, error)) (val interface{}, err error)
  • Logger - sets logger, compatible with any implementation of a single-method interface Logf(format string, args ...interface{}), for example go-pkgz/lgr
  • CircuitBreaker - sets circuit breaker, interface compatible with sony/gobreaker

Users can add any custom middleware. All it needs is a handler RoundTripperHandler func(http.RoundTripper) http.RoundTripper. Convenient functional adapter middleware.RoundTripperFunc provided.

See examples of the usage in _example

Logging

Logger should implement Logger interface with a single method Logf(format string, args ...interface{}). For convenience, func type LoggerFunc is provided as an adapter to allow the use of ordinary functions as Logger.

Two basic implementation included:

  • NoOpLogger do-nothing logger (default)
  • StdLogger wrapper for stdlib logger.

logging options:

  • Prefix(prefix string) sets prefix for each logged line
  • WithBody - allows request's body logging
  • WithHeaders - allows request's headers logging

Note: if logging is allowed, it will log URL, method, and may log headers and the request body. It may affect application security. For example, if a request passes some sensitive info as a part of the body or header. In this case, consider turning logging off or provide your own logger suppressing all you need to hide.

MaxConcurrent

MaxConcurrent middleware can be used to limit the concurrency of a given requester and limit overall concurrency for multiple requesters. For the first case, MaxConcurrent(N) should be created in the requester chain of middlewares, for example, rq := requester.New(http.Client{Timeout: 3 * time.Second}, middleware.MaxConcurrent(8)). To make it global, MaxConcurrent should be created once, outside the chain and passed into each requester, i.e.

mc := middleware.MaxConcurrent(16)
rq1 := requester.New(http.Client{Timeout: 3 * time.Second}, mc)
rq2 := requester.New(http.Client{Timeout: 1 * time.Second}, middleware.JSON, mc)

If request was limited, it will wait till the limit is released.

Cache

Cache expects LoadingCache interface implementing a single method: Get(key string, fn func() (interface{}, error)) (val interface{}, err error). LCW can be used directly, and in order to adopt other caches see provided LoadingCacheFunc.

caching key and allowed requests

By default, only GET calls are cached. This can be changed with Methods(methods ...string) option. The default key composed of the full URL.

Several options are defining what part of the request will be used for the key:

  • KeyWithHeaders - adds all headers to a key
  • KeyWithHeadersIncluded(headers ...string) - adds only requested headers
  • KeyWithHeadersExcluded(headers ...string) - adds all headers excluded
  • KeyWithBody - adds request's body, limited to the first 16k of the body
  • KeyFunc - any custom logic provided by the caller

example: cache.New(lruCache, cache.Methods("GET", "POST"), cache.KeyFunc() {func(r *http.Request) string {return r.Host})

cache and streaming response

Cache is not compatible with http streaming mode. Practically, this is rare and exotic, but allowing Cache will effectively transform the streaming response to a "get all" typical response. It is due to cache has to read response body fully to save it, so technically streaming will be working, but the client will get all the data at once.

Repeater

Repeater expects a single method interface Do(fn func() error, stopOnErrs ...error) (err error). repeater can be used directly.

By default, the repeater will retry on any error and any status code. However, user can pass stopOnErrs codes to eliminate retries, for example: Repeater(repeaterSvc, 500, 400) won't repeat on 500 and 400 statuses.

User-Defined Middlewares

Users can add any additional handlers (middleware) to the chain. Each middleware provides middleware.RoundTripperHandler and can alter the request or implement any other custom functionality.

Example of a handler resetting particular header:

maskHeader := func(http.RoundTripper) http.RoundTripper {
    fn := func(req *http.Request) (*http.Response, error) {
        req.Header.Del("deleteme")
        return next(req)
    }
    return middleware.RoundTripperFunc(fn)
}

rq := requester.New(http.Client{}, maskHeader)

Adding middleware to requester

There are 3 ways to add middleware(s):

  • Pass it to New constructor, i.e. requester.New(http.Client{}, middleware.MaxConcurrent(8), middleware.Header("foo", "bar"))
  • Add after construction with Use method
  • Create new, inherited requester by using With:
rq := requester.New(http.Client{}, middleware.Header("foo", "bar")) // make requester enforcing header foo:bar
resp, err := rq.Do(some_http_req) // send a request

rqLimited := rq.With(middleware.MaxConcurrent(8)) // make requester from rq (foo:bar enforced) and add 8 max concurrency
resp, err := rqLimited.Do(some_http_req)

Getting http.Client with all middlewares

For convenience requester.Client() returns *http.Client with all middlewares injected in. From this point user can call Do of this client, and it will invoke the request with all the middlewares.

Helpers and adapters

  • CircuitBreakerFunc func(req func() (interface{}, error)) (interface{}, error) - adapter to allow the use of an ordinary functions as CircuitBreakerSvc.
  • logger.Func func(format string, args ...interface{}) - functional adapter for logger.Service.
  • cache.ServiceFunc func(key string, fn func() (interface{}, error)) (interface{}, error) - functional adapter for cache.Service.
  • RoundTripperFunc func(*http.Request) (*http.Response, error) - functional adapter for RoundTripperHandler
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].