gohttpclient

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2022 License: MIT Imports: 7 Imported by: 7

README

go-http-client

go-http-client

An enhanced http client for Golang

Documentation on go.dev 🔗

Coverage awesome

This package provides you a http client package for your http requests. You can send requests quicly with this package. If you want to contribute this package, please fork and create a pull request.

Installation

$ go get -u github.com/bozd4g/go-http-client/

Example Usage

package main

import (
	"context"
	"log"

	gohttpclient "github.com/bozd4g/go-http-client"
)

type Post struct {
	ID    int    `json:"id"`
	Title string `json:"title"`
}

func main() {
	ctx := context.Background()
	client := gohttpclient.New("https://jsonplaceholder.typicode.com")

	response, err := client.Get(ctx, "/posts/1")
	if err != nil {
		log.Fatalf("error: %v", err)
	}

	var post Post
	if err := response.Unmarshal(&post); err != nil {
		log.Fatalf("error: %v", err)
	}

	log.Printf(post.Title) // sunt aut facere repellat provident occaecati...
}

License

Copyright (c) 2020 Furkan Bozdag

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

View Source
const (
	DEFAULT_TIMEOUT = 10 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client added in v0.1.4

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

Client is a struct who has BaseUrl property

func New

func New(baseUrl string, opts ...ClientOption) *Client

New func returns a Client struct

func (*Client) Connect added in v1.0.0

func (c *Client) Connect(ctx context.Context, endpoint string, opts ...Option) (*Response, error)

func (*Client) Delete added in v0.1.4

func (c *Client) Delete(ctx context.Context, endpoint string, opts ...Option) (*Response, error)

Delete func returns a request

func (*Client) Get added in v0.1.4

func (c *Client) Get(ctx context.Context, endpoint string, opts ...Option) (*Response, error)

Get func returns a request

func (*Client) Options added in v1.0.0

func (c *Client) Options(ctx context.Context, endpoint string, opts ...Option) (*Response, error)

func (*Client) Patch added in v0.1.4

func (c *Client) Patch(ctx context.Context, endpoint string, opts ...Option) (*Response, error)

Patch func returns a request

func (*Client) Post added in v0.1.4

func (c *Client) Post(ctx context.Context, endpoint string, opts ...Option) (*Response, error)

Post func returns a request

func (*Client) PrepareRequest added in v0.1.5

func (c *Client) PrepareRequest(ctx context.Context, method, endpoint string, opts ...Option) (*http.Request, error)

PrepareRequest func returns a request

func (*Client) Put added in v0.1.4

func (c *Client) Put(ctx context.Context, endpoint string, opts ...Option) (*Response, error)

Put func returns a request

func (*Client) Trace added in v1.0.0

func (c *Client) Trace(ctx context.Context, endpoint string, opts ...Option) (*Response, error)

type ClientOption added in v0.1.5

type ClientOption Option

func WithCustomHttpClient added in v1.0.0

func WithCustomHttpClient(client *http.Client) ClientOption

func WithDefaultHeaders added in v0.1.5

func WithDefaultHeaders() ClientOption

func WithTimeout added in v0.1.5

func WithTimeout(timeout time.Duration) ClientOption

type Clienter added in v0.1.5

type Clienter interface{}

Clienter is a interface who calls the methods

type Header struct {
	Value     string
	IsDefault bool
}

type Option added in v0.1.5

type Option func(c *Client)

func WithBody added in v0.1.5

func WithBody(body []byte) Option

func WithHeader added in v0.1.5

func WithHeader(key, value string) Option

func WithQuery added in v0.1.5

func WithQuery(key, value string) Option

type Response added in v0.1.4

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

func (*Response) Body added in v0.1.5

func (r *Response) Body() []byte

func (*Response) Cookies added in v0.1.5

func (r *Response) Cookies() []*http.Cookie

func (*Response) Get added in v0.1.4

func (r *Response) Get() *http.Response

func (*Response) Headers added in v1.0.1

func (r *Response) Headers() http.Header

func (*Response) Ok added in v0.1.5

func (r *Response) Ok() bool

func (*Response) Status added in v0.1.5

func (r *Response) Status() int

func (*Response) Unmarshal added in v1.0.0

func (r *Response) Unmarshal(v any) error

Directories

Path Synopsis
client module

Jump to

Keyboard shortcuts

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