connexions

package module
v0.1.21 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2023 License: MIT Imports: 23 Imported by: 0

README

Connexions

CI Endpoint Badge GoReportCard GoDoc License

Connexions is a library originally inspired by Connexion.
Connexion allows you to set up a REST API with Swagger documentation and OAuth2 authentication with minimal effort.

Connexions takes this one step further by allowing you to define multiple APIs not limited to only Swagger and(or) OpenAPI.
You can define single response for any arbitrary path on the fly.

Goals

  • provide a simple tool to work with API mocks
  • combine multiple APIs into one
  • generate meaningful responses

Features

  • Randomized response contents, allowing you to redefine the response for any path in a locale of your choice
  • Mimic error responses and status codes
  • Configurable latency in responses

Simple start

docker run -it --rm \
  -p 2200:2200 \
  -v connexions:/app/resources/data \
  cubahno/connexions api

Read full documentation at cubahno.github.io/connexions.

OpenAPI Specification

License

Copyright (c) 2023-present

Licensed under the MIT License

===================

OpenAPI Specification
OpenAPI 3.0 Style Values
Operation Object
YAML format

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnexpectedFormDataType       = errors.New("expected map[string]any for multipart/form-data")
	ErrUnexpectedFormURLEncodedType = errors.New("expected map[string]any for x-www-form-urlencoded")
	ErrGettingFileFromURL           = errors.New("error getting file from url")
)

Functions

func CleanupServiceFileStructure

func CleanupServiceFileStructure(servicePath string) error

CleanupServiceFileStructure removes empty directories from the service directory.

func CopyDirectory

func CopyDirectory(src, dest string) error

CopyDirectory copies a directory recursively.

func CopyFile

func CopyFile(srcPath, destPath string) error

CopyFile copies a file from srcPath to destPath. If the destination directory doesn't exist, it will be created.

func ExtractZip

func ExtractZip(zipReader *zip.Reader, targetDir string, onlyPrefixes []string) error

ExtractZip extracts a zip archive to a target directory. onlyPrefixes is a list of prefixes that are allowed to be extracted.

func GenerateContentArray

func GenerateContentArray(schema *openapi.Schema, valueReplacer replacers.ValueReplacer, state *replacers.ReplaceState) any

GenerateContentArray generates content from the given schema with type `array`.

func GenerateContentFromFileProperties added in v0.1.19

func GenerateContentFromFileProperties(filePath, contentType string, valueReplacer replacers.ValueReplacer) []byte

func GenerateContentFromSchema

func GenerateContentFromSchema(schema *openapi.Schema, valueReplacer replacers.ValueReplacer, state *replacers.ReplaceState) any

GenerateContentFromSchema generates content from the given schema.

func GenerateContentObject

func GenerateContentObject(schema *openapi.Schema, valueReplacer replacers.ValueReplacer, state *replacers.ReplaceState) any

GenerateContentObject generates content from the given schema with type `object`.

func GenerateQuery

func GenerateQuery(valueReplacer replacers.ValueReplacer, params openapi.Parameters) string

GenerateQuery generates query string from the given parameters.

func GenerateRequestHeaders

func GenerateRequestHeaders(parameters openapi.Parameters, valueReplacer replacers.ValueReplacer) map[string]any

GenerateRequestHeaders generates GeneratedRequest headers from the given parameters.

func GenerateResponseHeaders

func GenerateResponseHeaders(headers openapi.Headers, valueReplacer replacers.ValueReplacer) http.Header

GenerateResponseHeaders generates response headers from the given headers.

func GenerateURLFromSchemaParameters

func GenerateURLFromSchemaParameters(path string, valueResolver replacers.ValueReplacer, params openapi.Parameters) string

GenerateURLFromSchemaParameters generates URL from the given path and parameters.

func GetFileContentsFromURL added in v0.1.5

func GetFileContentsFromURL(client *http.Client, url string) ([]byte, string, error)

func GetFileHash

func GetFileHash(file io.Reader) string

GetFileHash gets the SHA256 hash of a file.

func IsEmptyDir

func IsEmptyDir(path string) bool

IsEmptyDir checks if a directory is empty.

func IsJsonType

func IsJsonType(content []byte) bool

IsJsonType checks if the content is a valid JSON document.

func IsYamlType

func IsYamlType(content []byte) bool

IsYamlType checks if the content is a valid YAML document.

func NewCacheOperationAdapter

func NewCacheOperationAdapter(service string, operation openapi.Operation, storage CacheStorage) openapi.Operation

NewCacheOperationAdapter creates a new CacheOperationAdapter instance.

func NewDocumentFromFileFactory

func NewDocumentFromFileFactory(provider config.SchemaProvider) func(filePath string) (openapi.Document, error)

NewDocumentFromFileFactory returns a function that creates a new Document from a file.

func NewOpenAPIValidator added in v0.1.18

func NewOpenAPIValidator(doc openapi.Document) openapi.Validator

NewOpenAPIValidator returns a new Validator based on the Document provider.

func NewRequestFromFixedResource added in v0.1.19

func NewRequestFromFixedResource(path, method, contentType string, valueReplacer replacers.ValueReplacer) *openapi.GeneratedRequest

func NewRequestFromOperation

func NewRequestFromOperation(pathPrefix, path, method string, operation openapi.Operation, valueReplacer replacers.ValueReplacer) *openapi.GeneratedRequest

NewRequestFromOperation creates a new GeneratedRequest from an Operation. It used to pre-generate payloads from the UI or provide service to generate such. It's not part of OpenAPI endpoint handler.

func NewResponseFromFixedResource added in v0.1.19

func NewResponseFromFixedResource(filePath, contentType string, valueReplacer replacers.ValueReplacer) *openapi.GeneratedResponse

func NewResponseFromOperation

func NewResponseFromOperation(req *http.Request, operation openapi.Operation, valueReplacer replacers.ValueReplacer) *openapi.GeneratedResponse

NewResponseFromOperation creates a new response from an Operation. It used to pre-generate payloads from the UI or provide service to generate such.

func SaveFile

func SaveFile(filePath string, data []byte) error

SaveFile saves a file to the specified path. If the destination directory doesn't exist, it will be created.

Types

type CacheOperationAdapter

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

CacheOperationAdapter is an adapter that caches the result of the wrapped Operation. Implements Operation interface.

func (*CacheOperationAdapter) GetParameters

func (a *CacheOperationAdapter) GetParameters() openapi.Parameters

GetParameters returns the parameters for the Operation.

func (*CacheOperationAdapter) GetRequestBody

func (a *CacheOperationAdapter) GetRequestBody() (*openapi.Schema, string)

GetRequestBody returns the GeneratedRequest body for the Operation.

func (*CacheOperationAdapter) GetResponse

func (a *CacheOperationAdapter) GetResponse() *openapi.Response

GetResponse returns the response for the Operation.

func (*CacheOperationAdapter) ID

func (a *CacheOperationAdapter) ID() string

ID returns the ID of the Operation.

func (*CacheOperationAdapter) WithParseConfig

func (a *CacheOperationAdapter) WithParseConfig(parseConfig *config.ParseConfig) openapi.Operation

WithParseConfig sets the ParseConfig for the Operation.

type CacheStorage

type CacheStorage interface {
	Set(key string, value any) error
	Get(key string) (any, bool)
}

CacheStorage is an interface that describes a cache storage.

type FileProperties

type FileProperties struct {
	// ServiceName is the name of the service that the file belongs to.
	// It represents the first directory in the file path.
	ServiceName string

	// IsOpenAPI indicates whether the file is an OpenAPI specification.
	IsOpenAPI bool

	// Method is the HTTP method of the resource, which this file describes.
	Method string

	// Prefix is the path prefix of the resource, which this file describes.
	// This is service name with a leading slash.
	Prefix string

	// Resource is the path of the resource, which this file describes without prefix.
	Resource string

	// FilePath is the full path to the file.
	FilePath string

	// FileName is the name of the file with the extension.
	FileName string

	// Extension is the extension of the file, with the leading dot.
	Extension string

	// ContentType is the MIME type of the file.
	ContentType string

	// Spec is the OpenAPI specification of the file if the file iis an OpenAPI specification.
	Spec openapi.Document `json:"-"`
}

FileProperties contains inferred properties of a file that is being loaded from service directory.

func GetPropertiesFromFilePath

func GetPropertiesFromFilePath(filePath string, appCfg *config.AppConfig) (*FileProperties, error)

GetPropertiesFromFilePath gets properties of a file from its path.

func (*FileProperties) IsEqual

func (f *FileProperties) IsEqual(other *FileProperties) bool

IsEqual compares two FileProperties structs. Spec is not compared.

type MemoryStorage

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

MemoryStorage is a cache storage that stores data in memory.

func NewMemoryStorage

func NewMemoryStorage() *MemoryStorage

NewMemoryStorage creates a new MemoryStorage instance.

func (*MemoryStorage) Get

func (s *MemoryStorage) Get(key string) (any, bool)

Get returns the value for the given key.

func (*MemoryStorage) Set

func (s *MemoryStorage) Set(key string, value any) error

Set sets the value for the given key.

type SchemaWithContentType

type SchemaWithContentType struct {
	Schema      *openapi.Schema
	ContentType string
}

SchemaWithContentType is a schema with a content type. It is used to cache the result of GetRequestBody and wrap 2 values together.

type UploadedFile

type UploadedFile struct {
	// Content is the content of the file.
	Content []byte

	// Filename is the name of the file.
	Filename string

	// Extension is the extension of the file with the leading dot.
	Extension string

	// Size is the size of the file in bytes.
	Size int64
}

UploadedFile represents an uploaded file.

func GetRequestFile

func GetRequestFile(r *http.Request, fieldName string) (*UploadedFile, error)

GetRequestFile gets an uploaded file from a GeneratedRequest.

Jump to

Keyboard shortcuts

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