jac

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2023 License: MIT Imports: 12 Imported by: 0

README

JSON API service connector building base

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(endpoint string, 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(endpoint string, data []byte, 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(endpoint string, data []byte, 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(endpoint string) ([]*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(endpoint string) (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(endpoint string) (bool, error)
}

Jac is the interface that connector should implement

func NewJac

func NewJac(baseUrl string, jwt *string) Jac

NewJac returns new jac instance that implements Jac interface

type JacConfig

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

JacConfig contains configurable data of a Jac

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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