apifast

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2024 License: MIT Imports: 6 Imported by: 0

README

APIFast Go Library

This library provides a simple API client for building HTTP requests with the fasthttp library in Go. This library supports GET, POST, PATCH, and DELETE requests with optional Basic Authentication, Bearer Token Authentication, custom headers, and more.

Table of Contents

  1. Installation
  2. Basic Usage Example (GET Request)
  3. Making a POST Request
  4. Using Basic Authentication
  5. Using Bearer Token Authentication
  6. Adding Custom Headers

Installation

To install the package, run:

go get github.com/eantaru/apifast

Usage

Basic Usage Example (GET Request)

package main

import (
    "fmt"
    "time"
    "github.com/eantaru/apifast"
)

func main() {
    response, err := apifast.Build().
        Uri("https://jsonplaceholder.typicode.com/posts").
        Timeout(5 * time.Second). // Optional timeout
        Get()

    if err != nil {
        fmt.Println("Error:", err)
        return
    }

    fmt.Println("Response Code:", response.Code)
    fmt.Println("Response Body:", string(response.Body.([]byte)))
}

Making a POST Request

package main

import (
    "fmt"
    "time"
    "github.com/eantaru/apifast"
)

func main() {
    payload := []byte(`{"title": "foo", "body": "bar", "userId": 1}`)

    response, err := apifast.Build().
        Uri("https://jsonplaceholder.typicode.com/posts").
        Timeout(5 * time.Second).
        Payload(payload).
        Post()

    if err != nil {
        fmt.Println("Error:", err)
        return
    }

    fmt.Println("Response Code:", response.Code)
    fmt.Println("Response Body:", string(response.Body.([]byte)))
}

Using Basic Authentication

If the API requires Basic Authentication, you can provide the username and password like this:

package main

import (
    "fmt"
    "time"
    "github.com/eantaru/apifast"
)

func main() {
    auth := apifast.Auth{
        Username: "myUsername",
        Password: "myPassword",
    }

    response, err := apifast.Build().
        Uri("https://example.com/api").
        Timeout(5 * time.Second).
        Auth(auth).
        Get()

    if err != nil {
        fmt.Println("Error:", err)
        return
    }

    fmt.Println("Response Code:", response.Code)
    fmt.Println("Response Body:", string(response.Body.([]byte)))
}
Using Bearer Token Authentication

For APIs requiring Bearer Token authentication, you can provide the token like this:

package main

import (
    "fmt"
    "time"
    "github.com/eantaru/apifast"
)

func main() {
    auth := apifast.Auth{
        Token: "your-bearer-token",
    }

    response, err := apifast.Build().
        Uri("https://example.com/api").
        Timeout(5 * time.Second).
        Auth(auth).
        Get()

    if err != nil {
        fmt.Println("Error:", err)
        return
    }

    fmt.Println("Response Code:", response.Code)
    fmt.Println("Response Body:", string(response.Body.([]byte)))
}
Adding Custom Headers

You can pass custom headers with the request by providing a list of headers:

package main

import (
    "fmt"
    "time"
    "github.com/eantaru/apifast"
)

func main() {
    headers := []apifast.Header{
        {Tag: "Content-Type", Value: "application/json"},
        {Tag: "X-Custom-Header", Value: "CustomValue"},
    }

    response, err := apifast.Build().
        Uri("https://example.com/api").
        Timeout(5 * time.Second).
        Headers(headers).
        Get()

    if err != nil {
        fmt.Println("Error:", err)
        return
    }

    fmt.Println("Response Code:", response.Code)
    fmt.Println("Response Body:", string(response.Body.([]byte)))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Auth

type Auth struct {
	Username string
	Password string
	Token    string
}

type FastBuilder

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

func Build

func Build() *FastBuilder

Build initializes a new FastBuilder instance

func (*FastBuilder) Auth

func (b *FastBuilder) Auth(auth Auth) *FastBuilder

Auth sets the authentication options

func (*FastBuilder) Delete

func (b *FastBuilder) Delete() (*Response, error)

Delete initiates a DELETE request

func (*FastBuilder) Get

func (b *FastBuilder) Get() (*Response, error)

Get initiates a GET request

func (*FastBuilder) Headers

func (b *FastBuilder) Headers(headers []Header) *FastBuilder

Headers sets custom headers for the request

func (*FastBuilder) Patch

func (b *FastBuilder) Patch() (*Response, error)

Patch initiates a PATCH request

func (*FastBuilder) Payload

func (b *FastBuilder) Payload(payload []byte) *FastBuilder

Payload sets the request payload (body)

func (*FastBuilder) Post

func (b *FastBuilder) Post() (*Response, error)

Post initiates a POST request

func (*FastBuilder) Result

func (b *FastBuilder) Result(result interface{}) *FastBuilder

Result specifies where to store the response result

func (*FastBuilder) Timeout

func (b *FastBuilder) Timeout(timeout time.Duration) *FastBuilder

Timeout sets the request timeout

func (*FastBuilder) Uri

func (b *FastBuilder) Uri(url string) *FastBuilder

Uri sets the request URL

type Header struct {
	Tag   string
	Value interface{}
}

type RequestOptions

type RequestOptions struct {
	Timeout time.Duration // Request timeout duration

	Headers []Header
	Auth    Auth
	// contains filtered or unexported fields
}

RequestOptions represents optional parameters for making API requests

type Response

type Response struct {
	Code int    // HTTP code
	Msg  string // Status message
	Body interface{}
}

Jump to

Keyboard shortcuts

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