jsonbox

package module
Version: v0.0.0-...-3bec111 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2019 License: MIT Imports: 10 Imported by: 0

README

jsonbox Go Client SDK

GoDoc

Go wrapper / Client SDK for jsonbox

Usage

Import the package

import "github.com/peteretelej/jsonbox"

Use NewClient to get a new jsonbox Client to use

cl,err := jsonbox.NewClient("https://jsonbox.io/")

Create

//  Create a record
val := []byte(`{"name": "Jon Snow", "age": 25}`)
out, err := cl.Create("demobox_6d9e326c183fde7b",val)

// Create multiple records
val := []byte(`[{"name": "Daenerys Targaryen", "age": 25}, {"name": "Arya Stark", "age": 16}]`)
out, err := cl.Create("demobox_6d9e326c183fde7b",val)

Read

out, err := cl.Read("demobox_6d9e326c183fde7b")
fmt.Printf("%s",out)

// Query records for a boxid
out, err := cl.Read("demobox_6d9e326c183fde7b?query_key=name&query_value=arya%20stark")
fmt.Printf("%s",out)

Update

val := []byte(`{"name": "Arya Stark", "age": 18}`)
out, err := cl.Update("demobox_6d9e326c183fde7b","5d776b75fd6d3d6cb1d45c53",val)
fmt.Printf("%s",out)

Delete record from BOXID

err := cl.Delete("demobox_6d9e326c183fde7b","5d776b75fd6d3d6cb1d45c53")

DeleteAll records for a BOX_ID

err := cl.DeleteAll(BOXID)

List IDs for all records for a BOX_ID

ids,err := cl.IDs("demobox_6d9e326c183fde7b")
fmt.Printf("%s",ids)

See full example usage at examples/full

Documentation

Overview

Package jsonbox wraps jsonbox API calls.

Create a new Client for use

cl,err := jsonbox.NewClient("https://jsonbox.io/")

Create or add a record

val := []byte(`{"name": "Jon Snow", "age": 25}`)
out, err := cl.Create("demobox_6d9e326c183fde7b",val)

Read record(s)

out, err := cl.Read("demobox_6d9e326c183fde7b")
fmt.Printf("%s",out)

Query records

out, err := cl.Read("demobox_6d9e326c183fde7b?query_key=name&query_value=arya%20stark")
fmt.Printf("%s",out)

Update record

val := []byte(`{"name": "Arya Stark", "age": 18}`)
out, err := cl.Update("demobox_6d9e326c183fde7b","5d776b75fd6d3d6cb1d45c53",val)
fmt.Printf("%s",out)

Delete record

err := cl.Delete("demobox_6d9e326c183fde7b","5d776b75fd6d3d6cb1d45c53")

Delete All Records

err := cl.Delete(BOX_ID)

List IDs for all records

ids,err := cl.IDs("demobox_6d9e326c183fde7b")
fmt.Printf("%s",ids)

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTODO = errors.New("functionality not implemented")
	ErrName = errors.New("invalid name for either BOX_ID or COLLECTION")
)

Errors returned when working with jsonbox package

View Source
var NameRegExp = regexp.MustCompile("^[a-zA-Z0-9_]*$")

NameRegExp defines valid BOX_ID or COLLECTION names reges

Functions

func GetRecordID

func GetRecordID(dat []byte) (string, error)

GetRecordID returns the id of a record, or the first ID if multiple records

Types

type Client

type Client struct {
	URL     *url.URL
	Timeout time.Duration
	// contains filtered or unexported fields
}

Client defines the structure of a jsonbox client

func NewClient

func NewClient(baseURL string) (*Client, error)

NewClient returns a new jsonbox client

func (Client) Create

func (c Client) Create(boxID string, val []byte) ([]byte, error)

Create creates or adds a record to a boxId

func (Client) Delete

func (c Client) Delete(boxID string, recordID string) error

Delete deletes a boxID record given the record Id

func (Client) DeleteAll

func (c Client) DeleteAll(boxID string) error

DeleteAll deletes all records for boxID

func (Client) IDs

func (c Client) IDs(boxID string) ([]string, error)

IDs returns all IDs in a BoxID collection

func (Client) Read

func (c Client) Read(boxID string) ([]byte, error)

Read reads records for a boxId or boxId with query

func (Client) Request

func (c Client) Request(method, urlPath string, dat []byte) ([]byte, error)

Request makes a request to json box url

func (Client) Update

func (c Client) Update(boxID string, recordID string, val []byte) ([]byte, error)

Update updates a boxID record given the record key and new value

type Meta

type Meta struct {
	ID        string `json:"_id"`
	CreatedOn string `json:"_createdOn"`
}

Meta defines metadata for json stored

func GetRecordMetas

func GetRecordMetas(dat []byte) ([]Meta, error)

GetRecordMetas returns a list of record Metas, fails for single record

Source Files

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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