Is a lightweight Go library designed to simplify HTTP requests by providing an easy-to-use interface, built-in support for various HTTP methods, accepting retry handling, and more.
To use can install it via go get
:
go get github.com/AndresXLP/ravenTree
package main
import (
"context"
"fmt"
"log"
"net/http"
"time"
"github.com/AndresXLP/ravenTree"
)
func main() {
tree := ravenTree.NewRavensTree()
options := &ravenTree.Options{
Host: "http://localhost:8080",
Path: "/api/resource",
Method: http.MethodGet,
QueryParams: map[string]string{"code": "123"},
Headers: map[string]string{"Authorization": "Bearer 1234"},
Timeout: 5 * time.Second,
RetryCount: 3,
Backoff: ravenTree.NewBackoff(
ravenTree.WithStrategy(ravenTree.Exponential),
ravenTree.WithBackoffDelay(3*time.Second),
ravenTree.WithMaxDelay(10*time.Second),
),
}
resp, err := tree.SendRaven(context.Background(), options)
if err != nil {
log.Fatal(err)
}
fmt.Println(resp.ParseBodyToString())
}
SendRaven: This method sends an HTTP request based on the provided Options. It supports different HTTP methods such as GET, POST, PUT, DELETE, etc.
The Body field in the Options struct can accept any type of data that can be marshaled into JSON. The library automatically handles the marshaling of the Body when sending the request.
By default, the Content-Type header is set to application/json.
You can add additional headers and query parameters using the Headers and QueryParams fields in the Options struct.
- Timeout: Specifies the maximum duration for a request. If the request takes longer than this duration, it will be
aborted, and an error will be returned.
- RetryCount: Specifies the number of times to retry the request if it fails. This is useful for handling transient
errors or network issues. The library will automatically retry the request up to the specified number of attempts.
The Backoff
struct defines the strategy for implementing backoff delays in retry operations with the following fields:
- BackoffDelay: Specifies the duration to wait before the next retry.
- MaxDelay: Specifies the maximum duration for backoff delays.
- Strategy: Determines the type of backoff (Default, Linear, or Exponential).
Use the NewBackoff
function to create a new Backoff
with optional parameters:
- If no options are provided, it defaults to:
BackoffDelay
: 0 secondsMaxDelay
: 10 secondsStrategy
: Default
- Note: If
MaxDelay
is set to a value less thanBackoffDelay
,MaxDelay
will be updated to matchBackoffDelay
to ensure valid configuration.
package main
import (
"time"
"github.com/AndresXLP/ravenTree"
)
func main() {
options := &ravenTree.Options{
Backoff: ravenTree.NewBackoff(
WithStrategy(Linear),
WithBackoffDelay(2*time.Second),
WithMaxDelay(30*time.Second),
),
}
}
Always check for errors after calling SendRaven. If the request fails, the error will provide information about what went wrong.
The name Raven Tree reflects the connection to the mystical ravens that serve as messengers in both Game of Thrones and Norse mythology, symbolizing communication, wisdom, and the passage of information.
Just as these ravens carry messages across great distances, Raven Tree aims to facilitate seamless communication between your application and external APIs.
Contributions are welcome! Please open an issue or submit a pull request for any features or fixes you want to add.
The project is licensed under the MIT License