jac

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2024 License: MIT Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

🎃 JAC — JSON API service connector building base

Go Reference codecov Go Report Card

JAC is a building base for custom JSON API service connectors.

Usage example

package examples

import (
	"encoding/json"
	"github.com/zspkg/jac"
)

// FooServiceConnector is your custom service connector
type FooServiceConnector struct {
	jac.Jac
	createFooEndpoint string
}

// Foo is the service object
type Foo struct {
	bar int
}

// FooCreateResponse is the service response when creating Foo
type FooCreateResponse struct {
	id  int
	bar int
}

// NewFooServiceConnector is your custom connector to FooService where
// you can define your own data transformations and operations and
// then use jac.Jac's methods to easily send POST/GET/DELETE methods
func NewFooServiceConnector(baseEndpoint string, jwt *string) *FooServiceConnector {
	return &FooServiceConnector{
		jac.NewJac(baseEndpoint, jwt),
		"foo/create",
	}
}

// CreateFoo is an example of simple connector function
// which uses jac.Jac to create new Foo instance via connector
func (c *FooServiceConnector) CreateFoo(foo Foo) (*FooCreateResponse, error) {
	// rawing our Foo model
	rawFoo, err := json.Marshal(foo)
	if err != nil {
		// your custom error handling
	}

	// creating response variable
	var response FooCreateResponse

	// sending POST request to our service via connector
	// to create new Foo instance
	apiErrs, err := c.Post(c.createFooEndpoint, rawFoo, &response)
	if err != nil {
		// your custom error handling
	}
	if len(apiErrs) != 0 {
		// you can handle API errs as you wish or just ignore them
	}

	// if err and apiErrs == nil, then you got a desired response from your service.
	// Now you can simply return it
	return &response, nil
}

Note that you can configure Jac directly from config using JACer. It uses Getter which is responsible for retrieving info from config files and must implement next interface:

type Getter interface {
    GetStringMap(key string) (map[string]interface{}, error)
}

JACer has next methods to configure Jac connector or to simply retrieve Jac configuration:

// JacConfig contains configurable data of a Jac
type JacConfig struct {
	URL string  `fig:"url,required"`
	JWT *string `fig:"jwt"`
}

// NewJACer returns an instance of JACer structure that configures Jac
func NewJACer(getter kv.Getter) JACer {
	return &jacer{getter: getter}
}

// GetJacConfig returns Jac configuration info based on a provided config from kv.Getter
func (c *jacer) GetJacConfig(configKey *string) JacConfig {
	return c.once.Do(func() interface{} {
		if configKey == nil {
			configKey = &jacDefaultConfigKey
		}

		var (
			config = JacConfig{}
			raw    = kv.MustGetStringMap(c.getter, *configKey)
		)

		if err := figure.Out(&config).From(raw).Please(); err != nil {
			panic(errors.Wrap(err, "failed to figure out jac"))
		}

		return config
	}).(JacConfig)
}

// ConfigureJac returns configured Jac based on a provided config from kv.Getter
func (c *jacer) ConfigureJac(configKey *string) Jac {
	cfg := c.GetJacConfig(configKey)
	return NewJac(cfg.URL, cfg.JWT)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JACer

type JACer interface {
	// GetJacConfig returns Jac configuration info based on config information
	// that can be found by specified key. It is useful when you need
	// to configure multiple connectors to different services.
	// If jacConfigKey is nil, default config key “jac“ is used.
	GetJacConfig(jacConfigKey *string) JacConfig
	// ConfigureJac returns configured Jac based on config information
	// that can be found by specified key. It is useful when you need
	// to configure multiple connectors to different services.
	// If jacConfigKey is nil, default config key “jac“ is used.
	ConfigureJac(jacConfigKey *string) Jac
}

JACer is the interface that connector configurator should implement

func NewJACer

func NewJACer(getter kv.Getter) JACer

NewJACer returns an instance of JACer structure that configures Jac based on a provided config from kv.Getter

type Jac

type Jac interface {
	// Get sends GET request and reads response body into destination.
	// Returns a slice of API error objects according to JSON API or
	// error if some happened during the operation.
	Get(params RequestParams, destination any) ([]*jsonapi.ErrorObject, error)
	// Post sends POST request with provided data as a request body
	// and reads response body if some data is expected to return.
	// Returns a slice of API error objects according to JSON API or
	// error if some happened during the operation.
	Post(params RequestParams, destination any) ([]*jsonapi.ErrorObject, error)
	// Put sends PUT request with provided data as a request body
	// and reads response body if some data is expected to return.
	// Returns a slice of API error objects according to JSON API or
	// error if some happened during the operation.
	Put(params RequestParams, destination any) ([]*jsonapi.ErrorObject, error)
	// Patch sends PATCH request with provided data as a request body
	// and reads response body if some data is expected to return.
	// Returns a slice of API error objects according to JSON API or
	// error if some happened during the operation.
	Patch(params RequestParams, destination any) ([]*jsonapi.ErrorObject, error)
	// Delete sends DELETE request.
	// Returns a slice of API error objects according to JSON API or
	// error if some happened during the operation.
	Delete(params RequestParams) ([]*jsonapi.ErrorObject, error)
	// Exists checks if object exists by provided endpoint.
	// Returns error if non-2xx status differs from 404 or
	// something happened during the operation.
	Exists(params RequestParams) (bool, error)
	// NotExists checks if object is not exist by provided endpoint.
	// Returns error if non-2xx status differs from 404 or
	// something happened during the operation.
	NotExists(params RequestParams) (bool, error)
}

Jac is the interface that connector should implement

func NewJac

func NewJac(baseUrl string) Jac

NewJac returns new jac instance that implements Jac interface

type JacConfig

type JacConfig struct {
	URL string `fig:"url,required"`
}

JacConfig contains configurable data of a Jac

type RequestParams

type RequestParams struct {
	Endpoint string
	Body     []byte
	Query    map[string]string
	Header   map[string]string
	// contains filtered or unexported fields
}

RequestParams is a structure for performing different requests

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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