axios4go

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 31, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

README

axios4go

Go Reference Go Report Card License

axios4go is a Go HTTP client library inspired by Axios, providing a simple and intuitive API for making HTTP requests. It offers features like JSON handling, configurable instances, and support for various HTTP methods.

Features

  • Simple and intuitive API
  • Support for GET, POST, PUT, DELETE, HEAD, OPTIONS, and PATCH methods
  • JSON request and response handling
  • Configurable client instances
  • Timeout and redirect management
  • Basic authentication support
  • Customizable request options
  • Promise-like asynchronous requests

Installation

To install axios4go, use go get:

go get github.com/rezmoss/axios4go

Usage

Making a Simple Request
package main

import (
    "fmt"
    "github.com/rezmoss/axios4go"
)

func main() {
    resp, err := axios4go.Get("https://api.example.com/data")
    if err != nil {
        fmt.Printf("Error: %v\n", err)
        return
    }
    fmt.Printf("Status Code: %d\n", resp.StatusCode)
    fmt.Printf("Body: %s\n", string(resp.Body))
}
Using Request Options
resp, err := axios4go.Get("https://api.example.com/data", &axios4go.requestOptions{
    timeout: 5000,  // 5 seconds
    headers: map[string]string{
        "Authorization": "Bearer token",
    },
})
Making POST Requests
body := map[string]interface{}{
    "name": "John Doe",
    "age":  30,
}
resp, err := axios4go.Post("https://api.example.com/users", body)
Using Async Requests
axios4go.GetAsync("https://api.example.com/data").
    Then(func(response *axios4go.Response) {
        fmt.Printf("Status Code: %d\n", response.StatusCode)
        fmt.Printf("Body: %s\n", string(response.Body))
    }).
    Catch(func(err error) {
        fmt.Printf("Error: %v\n", err)
    }).
    Finally(func() {
        fmt.Println("Request completed")
    })
Creating a Custom Client
client := axios4go.NewClient("https://api.example.com")
resp, err := client.Request(&axios4go.requestOptions{
    method: "GET",
    url:    "/users",
})

Configuration Options

axios4go supports various configuration options through the requestOptions struct:

  • method: HTTP method (GET, POST, etc.)
  • url: Request URL
  • baseURL: Base URL for the request
  • params: URL parameters
  • body: Request body
  • headers: Custom headers
  • timeout: Request timeout in milliseconds
  • auth: Basic authentication credentials
  • maxRedirects: Maximum number of redirects to follow
  • maxContentLength: Maximum allowed response content length
  • maxBodyLength: Maximum allowed request body length

Contributing

Contributions to axios4go are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the Apache License, Version 2.0 - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetBaseURL

func SetBaseURL(baseURL string)

Types

type Client

type Client struct {
	BaseURL    string
	HTTPClient *http.Client
}

func NewClient

func NewClient(baseURL string) *Client

func (*Client) Request

func (c *Client) Request(options *requestOptions) (*Response, error)

type Promise

type Promise struct {
	// contains filtered or unexported fields
}

func DeleteAsync

func DeleteAsync(urlStr string, options ...*requestOptions) *Promise

func GetAsync

func GetAsync(urlStr string, options ...*requestOptions) *Promise

func HeadAsync

func HeadAsync(urlStr string, options ...*requestOptions) *Promise

func NewPromise

func NewPromise() *Promise

func OptionsAsync

func OptionsAsync(urlStr string, options ...*requestOptions) *Promise

func PatchAsync

func PatchAsync(urlStr string, body interface{}, options ...*requestOptions) *Promise

func PostAsync

func PostAsync(urlStr string, body interface{}, options ...*requestOptions) *Promise

func PutAsync

func PutAsync(urlStr string, body interface{}, options ...*requestOptions) *Promise

func RequestAsync

func RequestAsync(method, urlStr string, options ...*requestOptions) *Promise

func (*Promise) Catch

func (p *Promise) Catch(fn func(error)) *Promise

func (*Promise) Finally

func (p *Promise) Finally(fn func())

func (*Promise) Then

func (p *Promise) Then(fn func(*Response)) *Promise

type Response

type Response struct {
	StatusCode int
	Headers    http.Header
	Body       []byte
}

func Delete

func Delete(urlStr string, options ...*requestOptions) (*Response, error)

func Get

func Get(urlStr string, options ...*requestOptions) (*Response, error)
func Head(urlStr string, options ...*requestOptions) (*Response, error)

func Options

func Options(urlStr string, options ...*requestOptions) (*Response, error)

func Patch

func Patch(urlStr string, body interface{}, options ...*requestOptions) (*Response, error)

func Post

func Post(urlStr string, body interface{}, options ...*requestOptions) (*Response, error)

func Put

func Put(urlStr string, body interface{}, options ...*requestOptions) (*Response, error)

func Request

func Request(method, urlStr string, options ...*requestOptions) (*Response, error)

func (*Response) JSON

func (r *Response) JSON(v interface{}) error

Source Files

  • client.go

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL