Skip to content

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.

License

Notifications You must be signed in to change notification settings

AndresXLP/ravenTree

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Raven Tree


Latest Version Coverage Status


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.

Installation

To use can install it via go get:

go get github.com/AndresXLP/ravenTree

Usage

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())
}

Methods Provided

SendRaven: This method sends an HTTP request based on the provided Options. It supports different HTTP methods such as GET, POST, PUT, DELETE, etc.

Body Management

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.

Headers and Query Parameters

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 and Retry Options

  • 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.

Backoff Options

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).

Creating a New Backoff

Use the NewBackoff function to create a new Backoff with optional parameters:

  • If no options are provided, it defaults to:
    • BackoffDelay: 0 seconds
    • MaxDelay: 10 seconds
    • Strategy: Default

  • Note: If MaxDelay is set to a value less than BackoffDelay, MaxDelay will be updated to match BackoffDelay to ensure valid configuration.

Example Usage

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),
		),
	}

}

Error Handling

Always check for errors after calling SendRaven. If the request fails, the error will provide information about what went wrong.

Thematic Inspiration

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.


Authors


Contributing

Contributions are welcome! Please open an issue or submit a pull request for any features or fixes you want to add.

License

The project is licensed under the MIT License

About

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.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages