sbapi

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2025 License: MIT Imports: 4 Imported by: 0

README

Go API wrapper for Silverbullet

This is a small API wrapper that implements the basic CRUD methods.

You can see examples in the main.go

Examples

// Create client
url := os.Getenv("SB_URL")
token := os.Getenv("SB_TOKEN")
sbclient := sbapi.NewClient(url, token)
// GET method gets the page text in markdown
main, err := sbclient.Get("main.md")
if err != nil {
  panic(err)
}
// PUT method creates a new page with given data
new, err := sbclient.Put("ApiTest/tester2.md", "# Hello")
if err != nil {
  panic(err)
}
// APPEND method is based on GET and PUT methods, adds data in a new line to the already existing page
append, err := sbclient.Append("ApiTest/tester.md", "# World", "\n")
if err != nil {
  panic(err)
}
// DELETE method is removing the given file. If that's the only file in the folder, the folder will automatically get deleted
del, err := sbclient.Delete("ApiTest/tester2.md")
if err != nil {
  panic(err)
}

Author: Márton Gombócz (Mrton0121)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SBClient

type SBClient struct {
	Endpoint   string
	Token      string
	HttpClient *http.Client
}

SBClient holds the necessary data to send http requests to the silverbullet API

func NewClient

func NewClient(endpoint string, token string) *SBClient

NewClient function creates and returns a new client

func (*SBClient) Append

func (client *SBClient) Append(page string, data string, separator string) (body string, err error)

Append uses GET and PUT methods. First gets the current contents of the file After that, appends the new data after the separator. current + separator + new data Takes page, data and separator strings Returns the response body and error

func (*SBClient) CreateRequest

func (client *SBClient) CreateRequest(method string, page string, body io.Reader) (req *http.Request, err error)

CreateRequest creates a http.Request with the given parameters and sets the necessary headers for the silverbullet API Takes url, token, method strings and a body io.Reader returns *http.Request, error

func (*SBClient) Delete

func (client *SBClient) Delete(page string) (body string, err error)

Delete removes the given page Takes a page parameter Returns the response body and error

func (*SBClient) Get

func (client *SBClient) Get(page string) (body string, err error)

Get sends a get request to the silverbullet API using the custom client Takes a page parameter Returns the content of the page and error

func (*SBClient) Put

func (client *SBClient) Put(page string, data string) (body string, err error)

Put sets the page contents to the given data Takes a page and a data parameter Returns the response body and error

Jump to

Keyboard shortcuts

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