httpclient

package
v0.0.0-...-a88ee92 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2020 License: GPL-3.0 Imports: 11 Imported by: 0

README

bookish-spork http client

This directory contains simple HTTP API client in Go for bookish-spork in-memory cache server

NOTE! It's located in the same repository with the server just for the sake of simplicity.

Installation

go get github.com/dstdfx/bookish-spork/httpclient

Example usage:

package main

import (
	"context"
	"fmt"

	bs "github.com/dstdfx/bookish-spork/httpclient"
)

func main() {
	// Init client
	cli := bs.NewClient("http://0.0.0.0:63100/v1")

	// Init common context
	ctx := context.Background()

	// Set key-value to cache
	_, err := cli.Set(ctx, bs.SetBody{
		Key:   "some-key",
		Value: "some-value",
		TTL:   10, // TTL in seconds
	})
	if err != nil {
		panic(err)
	}

	// Get value from cache
	v, _, err := cli.Get(ctx, "some-key")
	if err != nil {
		panic(err)
	}

	fmt.Printf("%+v\n", v)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// HTTPClient represents an initialized HTTP client that will be used to do requests.
	HTTPClient *http.Client

	// Endpoint represents an endpoint that will be used in all requests.
	Endpoint string
}

Client stores details that are needed to work with bookish-spork.

func NewClient

func NewClient(endpoint string) *Client

NewClient initializes a new client for bookish-spork API.

func NewClientCustomHTTP

func NewClientCustomHTTP(customHTTPClient *http.Client, endpoint string) *Client

NewClientCustomHTTP initializes a new client for bookish-spork API. using custom HTTP client. If customHTTPClient is nil - default HTTP client will be used.

func (*Client) Get

func (client *Client) Get(ctx context.Context, key string) (interface{}, *ResponseResult, error)

Get returns value by key in cache.

func (*Client) HGet

func (client *Client) HGet(ctx context.Context, key, hkey string) (interface{}, *ResponseResult, error)

HGet returns a value by hash map key.

func (*Client) HSet

func (client *Client) HSet(ctx context.Context, body HSetBody) (*ResponseResult, error)

HSet adds key-value pairs to hash map or create a new one.

func (*Client) Keys

func (client *Client) Keys(ctx context.Context) ([]string, *ResponseResult, error)

Keys returns slice of all keys in cache.

func (*Client) LIndex

func (client *Client) LIndex(ctx context.Context, key string, index int) (interface{}, *ResponseResult, error)

LIndex returns value by index in list.

func (*Client) RPush

func (client *Client) RPush(ctx context.Context, body RPushBody) (*ResponseResult, error)

RPush adds value to a list or create a new one.

func (*Client) Remove

func (client *Client) Remove(ctx context.Context, key string) (*ResponseResult, error)

Get returns value by key in cache.

func (*Client) Set

func (client *Client) Set(ctx context.Context, body SetBody) (*ResponseResult, error)

Get returns value by key in cache.

type ErrGeneric

type ErrGeneric struct {
	Error string `json:"error"`
}

ErrGeneric represents a generic error of an HTTP response.

type ErrNotFound

type ErrNotFound struct {
	Error string `json:"error"`
}

ErrNotFound represents 404 status code error of an HTTP response.

type HSetBody

type HSetBody struct {
	Key   string                 `json:"key"`
	Value map[string]interface{} `json:"value"`
	TTL   int                    `json:"ttl"`
}

HSetBody represents hset request body.

type RPushBody

type RPushBody struct {
	Key   string      `json:"key"`
	Value interface{} `json:"value"`
	TTL   int         `json:"ttl"`
}

RPushBody represents rpush request body.

type ResponseResult

type ResponseResult struct {
	*http.Response

	*ErrNotFound

	*ErrGeneric

	// Err contains an error that can be provided to a caller.
	Err error
}

ResponseResult represents a result of an HTTP request. It embeds standard http.Response and adds custom API error representations.

type SetBody

type SetBody struct {
	Key   string      `json:"key"`
	Value interface{} `json:"value"`
	TTL   int         `json:"ttl"`
}

SetBody represents set request body.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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