collection

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2024 License: MIT Imports: 4 Imported by: 0

README

go-bruno-collection

GoDoc

Go module to import and export Bruno collections.

This package provides a set of structures and import/export utilities for working with Bruno collections in Go. Use this package if you want to work natively with Bruno collection data and want basic data validation.

This project is not endorsed by or otherwise affiliated with Bruno project or company. For more information on the Bruno project, please visit their GitHub repository. We encourage you to support Bruno directly if desired. This project was inspired by, but shares no code with, its Postman equivalent go-postman-collection.

Examples

Collections

Bruno imports and exports requests using a custom JSON format called a collection. These collections are well structured with data integrity rules for validation. This project only addresses working with exported Bruno collections in go and does not address working with Bruno's internal file structure. If you need to work with .bru files directly then you should go to the source as this project is not targeted at that use case.

Read a Bruno Collection
package main

import (
	collection "github.com/speedscale/go-bruno-collection"
)

func main() {
	cs, err := collection.ParseFile("../testdata/demo.json")
	if err != nil {
		panic(err)
	}

	_ = cs
}
Create and save a Bruno Collection
package main

import (
	"os"

	collection "github.com/speedscale/go-bruno-collection"
)

func main() {
	cs := collection.CreateCollection("demo", "Demo collection")
	collection.AddItem(&cs, collection.ItemSchema{
		Type:    "http",
		Name:    "test",
		Request: collection.CreateRequest("http://example.com", "GET"),
	})

	err := collection.WriteFile("test_bruno_collection.json", cs)
	if err != nil {
		panic(err)
	}
	defer os.Remove("test_bruno_collection.json")
}

Contribution Guide

Feel free to open Merge Requests for any topic. This project is supported but not tightly controlled as far as features, bug fixes or enhancements. All contributions will be positively considered.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddItem

func AddItem(cs *CollectionSchema, item ItemSchema)

AddItem appends an item (ItemSchema) to the existing Items slice. This is how you add a new request to the Collection.

CollectionSchema is passed as a pointer so changes are preserved on the heap.

func Validate

func Validate(cs CollectionSchema) error

Validate performs data integrity checking based on the schema definition provided by the Bruno project.

func WriteFile

func WriteFile(filename string, cs CollectionSchema) error

ToFile is a simple helper function that marshals the CollectionSchema and writes it to a file.

Types

type AuthAwsV4Schema

type AuthAwsV4Schema struct {
	AccessKeyId     *string `validate:"omitempty"`
	SecretAccessKey *string `validate:"omitempty"`
	SessionToken    *string `validate:"omitempty"`
	Service         *string `validate:"omitempty"`
	Region          *string `validate:"omitempty"`
	ProfileName     *string `validate:"omitempty"`
}

AuthAwsV4Schema represents the schema for AWS V4 authentication

type AuthBasicSchema

type AuthBasicSchema struct {
	Username *string `validate:"omitempty"`
	Password *string `validate:"omitempty"`
}

AuthBasicSchema represents the schema for basic authentication

type AuthBearerSchema

type AuthBearerSchema struct {
	Token *string `validate:"omitempty"`
}

AuthBearerSchema represents the schema for bearer authentication

type AuthDigestSchema

type AuthDigestSchema struct {
	Username *string `validate:"omitempty"`
	Password *string `validate:"omitempty"`
}

AuthDigestSchema represents the schema for digest authentication

type AuthSchema

type AuthSchema struct {
	Mode   string            `validate:"required,oneof=inherit none awsv4 basic bearer digest oauth2"`
	AwsV4  *AuthAwsV4Schema  `validate:"omitempty"`
	Basic  *AuthBasicSchema  `validate:"omitempty"`
	Bearer *AuthBearerSchema `validate:"omitempty"`
	Digest *AuthDigestSchema `validate:"omitempty"`
	Oauth2 *Oauth2Schema     `validate:"omitempty"`
}

AuthSchema represents the schema for authentication

type CollectionSchema

type CollectionSchema struct {
	Version              string `validate:"required,oneof=1"`
	UID                  UIDSchema
	Name                 string                 `validate:"required,min=1"`
	Items                []ItemSchema           `validate:"dive"`
	ActiveEnvironmentUID *string                `validate:"omitempty,len=21,alphanum"`
	Environments         []EnvironmentSchema    `validate:"dive"`
	Pathname             *string                `validate:"omitempty"`
	RunnerResult         map[string]interface{} `validate:"omitempty"`
	CollectionVariables  map[string]interface{} `validate:"omitempty"`
	BrunoConfig          map[string]interface{} `validate:"omitempty"`
}

CollectionSchema represents the schema for collection

func CreateCollection

func CreateCollection(name string, desc string) CollectionSchema

CreateCollection returns a new Collection.

func Parse

func Parse(b []byte) (CollectionSchema, error)

Parse is a simple helper function that unmarshals the input bytes into a CollectionSchema whille also validating for data integrity. The caller can ignore validation errors if desired as the CollectionSchema is returned regardless.

func ParseFile

func ParseFile(filename string) (CollectionSchema, error)

ParseFile is a simple helper funtion that reads a file, unmarshals the results into a CollectionSchema and then performs basic data validation. The caller can ignore validation errors if desired as the CollectionSchema is returned regardless.

type EnvironmentSchema

type EnvironmentSchema struct {
	UID       UIDSchema
	Name      string                       `validate:"required,min=1"`
	Variables []EnvironmentVariablesSchema `validate:"required,dive"`
}

EnvironmentSchema represents the schema for environment

type EnvironmentVariablesSchema

type EnvironmentVariablesSchema struct {
	UID     UIDSchema
	Name    *string `validate:"omitempty"`
	Value   *string `validate:"omitempty"`
	Type    string  `validate:"required,oneof=text"`
	Enabled bool    `validate:"required"`
	Secret  *bool   `validate:"omitempty"`
}

EnvironmentVariablesSchema represents the schema for environment variables

type GraphqlBodySchema

type GraphqlBodySchema struct {
	Query     *string `validate:"omitempty"`
	Variables *string `validate:"omitempty"`
}

GraphqlBodySchema represents the schema for GraphQL body

type ItemSchema

type ItemSchema struct {
	UID         UIDSchema
	Type        string         `validate:"required,oneof=http graphql http-request graphql-request folder js"`
	Seq         int            `validate:"omitempty,min=1"`
	Name        string         `validate:"required,min=1"`
	Request     *RequestSchema `validate:"required_if=Type http-request Type graphql-request'"`
	FileContent *string        `validate:"omitempty"`
	Items       []ItemSchema   `validate:"omitempty,dive"`
	Filename    *string        `validate:"omitempty"`
	Pathname    *string        `validate:"omitempty"`
}

ItemSchema represents the schema for item (aka a Request)

type KeyValueSchema

type KeyValueSchema struct {
	UID         UIDSchema
	Name        *string `validate:"omitempty"`
	Value       *string `validate:"omitempty"`
	Description *string `validate:"omitempty"`
	Enabled     *bool   `validate:"omitempty"`
}

KeyValueSchema represents the schema for key-value pairs

type MultipartFormSchema

type MultipartFormSchema struct {
	UID         UIDSchema
	Type        string  `validate:"required,oneof=file text"`
	Name        *string `validate:"omitempty"`
	Value       *string `validate:"omitempty"`
	Description *string `validate:"omitempty"`
	Enabled     *bool   `validate:"omitempty"`
}

MultipartFormSchema represents the schema for multipart form

type Oauth2Schema

type Oauth2Schema struct {
	GrantType        string  `validate:"required,oneof=client_credentials password authorization_code"`
	Username         *string `validate:"omitempty"`
	Password         *string `validate:"omitempty"`
	CallbackUrl      *string `validate:"omitempty"`
	AuthorizationUrl *string `validate:"omitempty"`
	AccessTokenUrl   *string `validate:"omitempty"`
	ClientId         *string `validate:"omitempty"`
	ClientSecret     *string `validate:"omitempty"`
	Scope            *string `validate:"omitempty"`
	State            *string `validate:"omitempty"`
	Pkce             *bool   `validate:"omitempty"`
}

Oauth2Schema represents the schema for OAuth2

type RequestBodySchema

type RequestBodySchema struct {
	Mode           string                `validate:"required,oneof=none json text xml formUrlEncoded multipartForm graphql sparql"`
	JSON           *string               `validate:"omitempty"`
	Text           *string               `validate:"omitempty"`
	XML            *string               `validate:"omitempty"`
	Sparql         *string               `validate:"omitempty"`
	FormUrlEncoded []KeyValueSchema      `validate:"omitempty,dive"`
	MultipartForm  []MultipartFormSchema `validate:"omitempty,dive"`
	Graphql        *GraphqlBodySchema    `validate:"omitempty"`
}

RequestBodySchema represents the schema for request body

type RequestParamsSchema

type RequestParamsSchema struct {
	UID         UIDSchema
	Name        *string `validate:"omitempty"`
	Value       *string `validate:"omitempty"`
	Description *string `validate:"omitempty"`
	Type        string  `validate:"required,oneof=query path"`
	Enabled     *bool   `validate:"omitempty"`
}

RequestParamsSchema represents the schema for request parameters

type RequestSchema

type RequestSchema struct {
	URL        string                `validate:"required"`
	Method     string                `validate:"required,oneof=GET POST PUT DELETE PATCH HEAD OPTIONS"`
	Headers    []KeyValueSchema      `validate:"required,dive"`
	Params     []RequestParamsSchema `validate:"dive"` // Bruno does not always output these
	Auth       AuthSchema            `validate:"required"`
	Body       RequestBodySchema     `validate:"required"`
	Script     ScriptSchema          `validate:"required"`
	Vars       *VarsReqResSchema     `validate:"omitempty"`
	Assertions []KeyValueSchema      `validate:"omitempty,dive"`
	Tests      *string               `validate:"omitempty"`
	Docs       *string               `validate:"omitempty"`
}

RequestSchema represents the schema for request

func CreateRequest

func CreateRequest(url string, method string) *RequestSchema

CreateRequest returns a minimally populated RequestSchema. The return value will pass data validation because the minimum set of fields is filled in with default values.

type ScriptSchema

type ScriptSchema struct {
	Req *string `validate:"omitempty"`
	Res *string `validate:"omitempty"`
}

ScriptSchema represents the schema for script

type UIDSchema

type UIDSchema string

UIDSchema represents the UID schema

type VarsReqResSchema

type VarsReqResSchema struct {
	Req []VarsSchema `validate:"omitempty,dive"`
	Res []VarsSchema `validate:"omitempty,dive"`
}

VarsReqResSchema represents the schema for vars in request and response

type VarsSchema

type VarsSchema struct {
	UID         UIDSchema
	Name        *string `validate:"omitempty"`
	Value       *string `validate:"omitempty"`
	Description *string `validate:"omitempty"`
	Enabled     *bool   `validate:"omitempty"`
	Local       *bool   `validate:"omitempty"`
}

VarsSchema represents the schema for vars

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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