bapp

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 13 Imported by: 0

README

BAPP Auto API Client — Go

Official Go client for the BAPP Auto API. Provides a simple, consistent interface for authentication, entity CRUD, and task execution.

Zero dependencies — uses only the standard library.

Getting Started

1. Install
go get github.com/bapp-open/sdk-go
2. Create a client
import bapp "github.com/bapp-open/sdk-go"

client := bapp.NewClient(bapp.WithToken("your-api-key"))
3. Make your first request
// List with filters
countries, _ := client.List("core.country", url.Values{"page": {"1"}})

// Get by ID
country, _ := client.Get("core.country", "42")

// Create
data := map[string]interface{}{"name": "Romania", "code": "RO"}
created, _ := client.Create("core.country", data)

// Patch (partial update)
client.Patch("core.country", "42", map[string]interface{}{"code": "RO"})

// Delete
client.Delete("core.country", "42")

Authentication

The client supports Token (API key) and Bearer (JWT / OAuth) authentication. Token auth already includes a tenant binding, so you don't need to specify tenant separately.

// Static API token (tenant is included in the token)
client := bapp.NewClient(bapp.WithToken("your-api-key"))

// Bearer (JWT / OAuth)
client := bapp.NewClient(bapp.WithBearer("eyJhbG..."), bapp.WithTenant("1"))

Configuration

tenant and app can be changed at any time after construction:

client.Tenant = "2"
client.App = "wms"

API Reference

Client options
Option Description Default
token Static API token (Token <value>) — includes tenant
bearer Bearer / JWT token
host API base URL https://panel.bapp.ro/api
tenant Tenant ID (x-tenant-id header) None
app App slug (x-app-slug header) "account"
timeout HTTP request timeout (seconds) 30
max_retries Max retries on transient errors (5xx, 429, connection) 3
Methods
Method Description
me() Get current user profile
get_app(app_slug) Get app configuration by slug
list(content_type, **filters) List entities (paginated)
get(content_type, id) Get a single entity
create(content_type, data) Create an entity
update(content_type, id, data) Full update (PUT)
patch(content_type, id, data) Partial update (PATCH)
delete(content_type, id) Delete an entity
list_introspect(content_type) Get list view metadata
detail_introspect(content_type) Get detail view metadata
get_document_views(record) Extract available views from a record
get_document_url(record, output?, label?, variation?) Build a render/download URL
get_document_content(record, output?, label?, variation?) Fetch document bytes (PDF, HTML, JPG)
download_document(record, dest, output?, label?, variation?) Stream document to file (memory-efficient)
list_tasks() List available task codes
detail_task(code) Get task configuration
run_task(code, payload?) Execute a task
run_task_async(code, payload?) Run a long-running task and poll until done
Paginated responses

list() returns the results directly as a list/array. Pagination metadata is available as extra attributes:

  • count — total number of items across all pages
  • next — URL of the next page (or null)
  • previous — URL of the previous page (or null)

File Uploads

When data contains file objects, the client automatically switches from JSON to multipart/form-data. Mix regular fields and files in the same call:

// Pass bapp.File values — the client auto-switches to multipart/form-data
f, _ := os.Open("report.pdf")
defer f.Close()

client.Create("myapp.document", map[string]interface{}{
    "name": "Report",
    "file": bapp.File{Name: "report.pdf", Reader: f},
})

Document Views

Records may include public_view and/or view_token fields with JWT tokens for rendering documents (invoices, orders, reports, etc.) as HTML, PDF, or images.

The SDK normalises both formats and builds the correct URL automatically:

order, _ := client.Get("company_order.order", "42")

// Get a PDF download URL (auto-detects public_view vs view_token)
url := client.GetDocumentURL(order, "pdf", "", "")

// Pick a specific view by label
url = client.GetDocumentURL(order, "html", "Comanda interna", "")

// Use a variation
url = client.GetDocumentURL(order, "pdf", "", "v4")

// Fetch the actual content as bytes
pdfBytes, _ := client.GetDocumentContent(order, "pdf", "", "")
os.WriteFile("order.pdf", pdfBytes, 0644)

// Enumerate all available views
views := bapp.GetDocumentViews(order)
for _, v := range views {
    fmt.Println(v.Label, v.Type, v.Variations)
}

get_document_views() returns a list of normalised view entries with label, token, type ("public_view" or "view_token"), variations, and default_variation. Use it to enumerate available views (e.g. for a dropdown).

Tasks

Tasks are server-side actions identified by a dotted code (e.g. myapp.export_report).

tasks, _ := client.ListTasks()

cfg, _ := client.DetailTask("myapp.export_report")

// Run without payload (GET)
result, _ := client.RunTask("myapp.export_report", nil)

// Run with payload (POST)
result, _ := client.RunTask("myapp.export_report", map[string]interface{}{"format": "csv"})
Long-running tasks

Some tasks run asynchronously on the server. When triggered, they return an id that can be polled via bapp_framework.taskdata. Use run_task_async() to handle this automatically — it polls until finished is true and returns the final task data (which includes a file URL when the task produces a download).

License

MIT

Documentation

Overview

Package bapp provides a client for the BAPP Auto API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Host   string
	Tenant string
	App    string
	// contains filtered or unexported fields
}

Client is a BAPP Auto API client.

func NewClient

func NewClient(opts ...Option) *Client

NewClient creates a new BAPP API client.

func (*Client) Create

func (c *Client) Create(contentType string, data interface{}) (map[string]interface{}, error)

Create creates a new entity.

func (*Client) Delete

func (c *Client) Delete(contentType, id string) (map[string]interface{}, error)

Delete deletes an entity.

func (*Client) DetailIntrospect

func (c *Client) DetailIntrospect(contentType string, pk string) (map[string]interface{}, error)

DetailIntrospect returns detail introspect for a content type.

func (*Client) DetailTask

func (c *Client) DetailTask(code string) (map[string]interface{}, error)

DetailTask returns task configuration.

func (*Client) DownloadDocument added in v0.5.0

func (c *Client) DownloadDocument(record map[string]interface{}, w io.Writer, output, label, variation string, download bool) error

DownloadDocument streams document content to w without buffering it in memory. Returns an error when the record has no view tokens.

func (*Client) DownloadDocumentToFile added in v0.5.0

func (c *Client) DownloadDocumentToFile(record map[string]interface{}, path, output, label, variation string, download bool) error

DownloadDocumentToFile is a convenience wrapper around DownloadDocument that writes directly to a file path.

func (*Client) Get

func (c *Client) Get(contentType, id string) (map[string]interface{}, error)

Get returns a single entity.

func (*Client) GetApp

func (c *Client) GetApp(appSlug string) (map[string]interface{}, error)

GetApp returns app configuration by slug.

func (*Client) GetDocumentContent added in v0.4.1

func (c *Client) GetDocumentContent(record map[string]interface{}, output, label, variation string, download bool) ([]byte, error)

GetDocumentContent fetches document content (PDF, HTML, JPG, etc.) as bytes. Builds the URL via GetDocumentURL and performs a plain GET request. Returns (nil, nil) when the record has no view tokens.

func (*Client) GetDocumentURL added in v0.4.1

func (c *Client) GetDocumentURL(record map[string]interface{}, output, label, variation string, download bool) string

GetDocumentURL builds a document render/download URL from a record. Prefers public_view when both formats are present.

output: "html", "pdf", "jpg", or "context"
label: select a specific view by label (empty string = first available)
variation: variation code for public_view entries (empty string = use default)
download: when true the response is sent as an attachment

func (*Client) List

func (c *Client) List(contentType string, filters url.Values) (*PagedList, error)

List lists entities for a content type. Returns a PagedList with Results, Count, Next, Previous.

func (*Client) ListIntrospect

func (c *Client) ListIntrospect(contentType string) (map[string]interface{}, error)

ListIntrospect returns list introspect for a content type.

func (*Client) ListTasks

func (c *Client) ListTasks() ([]interface{}, error)

ListTasks returns all available task codes.

func (*Client) Me

func (c *Client) Me() (map[string]interface{}, error)

Me returns the current user profile.

func (*Client) Patch

func (c *Client) Patch(contentType, id string, data interface{}) (map[string]interface{}, error)

Patch performs a partial update.

func (*Client) RunTask

func (c *Client) RunTask(code string, payload interface{}) (map[string]interface{}, error)

RunTask executes a task. Pass nil payload for GET, non-nil for POST.

func (*Client) RunTaskAsync

func (c *Client) RunTaskAsync(code string, payload interface{}, pollInterval, timeout time.Duration) (map[string]interface{}, error)

RunTaskAsync runs a long-running task and polls until finished. Returns the final task data which includes "file" when the task produces a download.

func (*Client) Update

func (c *Client) Update(contentType, id string, data interface{}) (map[string]interface{}, error)

Update performs a full update.

type DocumentView added in v0.4.1

type DocumentView struct {
	Label            string
	Token            string
	Type             string // "public_view" or "view_token"
	Variations       []map[string]interface{}
	DefaultVariation string
}

DocumentView represents a normalized document view entry extracted from a record.

func GetDocumentViews added in v0.4.1

func GetDocumentViews(record map[string]interface{}) []DocumentView

GetDocumentViews extracts available document views from a record. Works with both public_view (new) and view_token (legacy) formats.

type File

type File struct {
	Name   string    // filename
	Reader io.Reader // file content
}

File represents a file upload.

type Option

type Option func(*Client)

Option configures the client.

func WithApp

func WithApp(slug string) Option

WithApp sets the default app slug.

func WithBearer

func WithBearer(token string) Option

WithBearer sets Bearer token authentication.

func WithMaxRetries added in v0.5.0

func WithMaxRetries(n int) Option

WithMaxRetries sets the maximum number of retries on transient errors.

func WithTenant

func WithTenant(id string) Option

WithTenant sets the default tenant ID.

func WithTimeout added in v0.5.0

func WithTimeout(d time.Duration) Option

WithTimeout sets the HTTP client timeout.

func WithToken

func WithToken(token string) Option

WithToken sets Token-based authentication.

func WithUserAgent added in v0.4.3

func WithUserAgent(ua string) Option

WithUserAgent sets a custom User-Agent header.

type PagedList

type PagedList struct {
	Results  []map[string]interface{} `json:"results"`
	Count    int                      `json:"count"`
	Next     string                   `json:"next"`
	Previous string                   `json:"previous"`
}

PagedList holds the results slice plus pagination metadata.

Jump to

Keyboard shortcuts

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