client

package
v0.2.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const RedactedValue = "[REDACTED]"

RedactedValue is the replacement string for redacted PII values.

Variables

View Source
var UserAgent = "mb-cli/" + version.Version

Functions

func IsPIISemanticType added in v0.1.3

func IsPIISemanticType(semanticType string) bool

IsPIISemanticType returns true if the given Metabase semantic type is considered PII.

func ParseModels

func ParseModels(s string) []string

ParseModels splits a comma-separated models string into a slice.

func RedactFieldValues added in v0.1.3

func RedactFieldValues(values *FieldValues)

RedactFieldValues replaces all values in a FieldValues struct with RedactedValue.

func RedactQueryResult added in v0.1.3

func RedactQueryResult(result *QueryResult)

RedactQueryResult replaces values in PII columns with RedactedValue.

Types

type Card

type Card struct {
	ID                    int            `json:"id"`
	Name                  string         `json:"name"`
	Description           string         `json:"description,omitempty"`
	DatabaseID            int            `json:"database_id"`
	Display               string         `json:"display"`
	QueryType             string         `json:"query_type,omitempty"`
	CollectionID          *int           `json:"collection_id,omitempty"`
	Archived              bool           `json:"archived"`
	DatasetQuery          *DatasetQuery  `json:"dataset_query,omitempty"`
	ResultMetadata        []Field        `json:"result_metadata,omitempty"`
	VisualizationSettings map[string]any `json:"visualization_settings,omitempty"`
}

Card represents a Metabase saved question (card).

type Client

type Client struct {
	BaseURL    string
	HTTPClient HTTPDoer
	APIKey     string
	Verbose    bool
	RedactPII  bool
}

Client represents the Metabase API client.

func NewClient

func NewClient(cfg *config.Config) *Client

NewClient creates a new Metabase API client from the provided config.

func (*Client) DecodeJSON

func (c *Client) DecodeJSON(resp *http.Response, v any) error

DecodeJSON decodes a JSON response body into the provided value.

func (*Client) Do

func (c *Client) Do(req *http.Request) (*http.Response, error)

Do executes an HTTP request with authentication headers and error handling.

func (*Client) EnrichSemanticTypes added in v0.1.3

func (c *Client) EnrichSemanticTypes(result *QueryResult, databaseID int)

EnrichSemanticTypes fills in missing semantic types on result columns by looking up field metadata from the database. This is needed for native SQL queries where Metabase does not return semantic types in the result columns.

func (*Client) ExportNativeQuery

func (c *Client) ExportNativeQuery(databaseID int, sql string, format string) ([]byte, error)

ExportNativeQuery executes a native SQL query and returns the result in the specified export format.

func (*Client) ExportStructuredQuery added in v0.1.2

func (c *Client) ExportStructuredQuery(databaseID, tableID int, filters [][]any, limit int, format string) ([]byte, error)

ExportStructuredQuery executes an MBQL structured query and returns the result in the specified export format.

func (*Client) Get

func (c *Client) Get(endpoint string, params url.Values) (*http.Response, error)

Get performs a GET request to the given endpoint with optional query parameters.

func (*Client) GetCard

func (c *Client) GetCard(id int) (*Card, error)

GetCard retrieves a single card by ID.

func (*Client) GetDashboard added in v0.2.0

func (c *Client) GetDashboard(id int) (*Dashboard, error)

GetDashboard retrieves a single dashboard by ID.

func (*Client) GetDashboardParamValues added in v0.2.0

func (c *Client) GetDashboardParamValues(dashboardID int, paramKey string) (*ParameterValues, error)

GetDashboardParamValues retrieves valid values for a dashboard parameter.

func (*Client) GetDatabase

func (c *Client) GetDatabase(id int) (*Database, error)

GetDatabase retrieves a single database by ID.

func (*Client) GetDatabaseFields

func (c *Client) GetDatabaseFields(id int) ([]Field, error)

GetDatabaseFields retrieves all fields in a database.

func (*Client) GetDatabaseMetadata

func (c *Client) GetDatabaseMetadata(id int) (*DatabaseMetadata, error)

GetDatabaseMetadata retrieves full metadata (tables + fields) for a database.

func (*Client) GetDatabaseSchema

func (c *Client) GetDatabaseSchema(id int, schema string) ([]Table, error)

GetDatabaseSchema retrieves tables in a specific schema of a database.

func (*Client) GetField

func (c *Client) GetField(id int) (*Field, error)

GetField retrieves a single field by ID.

func (*Client) GetFieldSummary

func (c *Client) GetFieldSummary(id int) ([]FieldSummary, error)

GetFieldSummary retrieves summary statistics for a field. The API returns pairs like [["count",2],["distincts",2]].

func (*Client) GetFieldValues

func (c *Client) GetFieldValues(id int) (*FieldValues, error)

GetFieldValues retrieves distinct values for a field.

func (*Client) GetTable

func (c *Client) GetTable(id int) (*Table, error)

GetTable retrieves a single table by ID.

func (*Client) GetTableData

func (c *Client) GetTableData(id int) (*QueryResult, error)

GetTableData retrieves raw data for a table.

func (*Client) GetTableFKs

func (c *Client) GetTableFKs(id int) ([]ForeignKey, error)

GetTableFKs retrieves foreign key relationships for a table.

func (*Client) GetTableMetadata

func (c *Client) GetTableMetadata(id int) (*TableMetadata, error)

GetTableMetadata retrieves table metadata with field details.

func (*Client) ListCards

func (c *Client) ListCards() ([]Card, error)

ListCards retrieves all saved questions (cards).

func (*Client) ListDashboards added in v0.2.0

func (c *Client) ListDashboards() ([]Dashboard, error)

ListDashboards retrieves all dashboards.

func (*Client) ListDatabaseSchemas

func (c *Client) ListDatabaseSchemas(id int) ([]string, error)

ListDatabaseSchemas retrieves schema names for a database.

func (*Client) ListDatabases

func (c *Client) ListDatabases(includeTables bool) ([]Database, error)

ListDatabases retrieves all databases. If includeTables is true, tables are included.

func (*Client) ListTables

func (c *Client) ListTables() ([]Table, error)

ListTables retrieves all tables.

func (*Client) Post

func (c *Client) Post(endpoint string, body any) (*http.Response, error)

Post performs a POST request to the given endpoint with a JSON body.

func (*Client) RunCard

func (c *Client) RunCard(id int) (*QueryResult, error)

RunCard executes a saved question and returns the query result.

func (*Client) RunCardWithParams added in v0.2.0

func (c *Client) RunCardWithParams(id int, params map[string]string) (*QueryResult, error)

RunCardWithParams executes a saved question with parameter values.

func (*Client) RunDashboardCard added in v0.2.0

func (c *Client) RunDashboardCard(dashboardID, dashcardID, cardID int, params map[string]string) (*QueryResult, error)

RunDashboardCard executes a dashboard card with parameter values.

func (*Client) RunNativeQuery

func (c *Client) RunNativeQuery(databaseID int, sql string) (*QueryResult, error)

RunNativeQuery executes a native SQL query against the specified database.

func (*Client) RunStructuredQuery added in v0.1.2

func (c *Client) RunStructuredQuery(databaseID, tableID int, filters [][]any, limit int) (*QueryResult, error)

RunStructuredQuery executes an MBQL structured query with filters.

func (*Client) Search

func (c *Client) Search(query string, models []string) ([]SearchResult, error)

Search searches across all Metabase items.

func (*Client) SearchDashboardParamValues added in v0.2.0

func (c *Client) SearchDashboardParamValues(dashboardID int, paramKey string, query string) (*ParameterValues, error)

SearchDashboardParamValues searches dashboard parameter values.

type DashCard added in v0.2.0

type DashCard struct {
	ID                int                    `json:"id"`
	CardID            *int                   `json:"card_id,omitempty"`
	Card              *Card                  `json:"card,omitempty"`
	TabID             *int                   `json:"dashboard_tab_id,omitempty"`
	ParameterMappings []DashParameterMapping `json:"parameter_mappings,omitempty"`
}

DashCard represents a card placed on a dashboard.

type DashParameter added in v0.2.0

type DashParameter struct {
	ID   string `json:"id"`
	Name string `json:"name"`
	Slug string `json:"slug"`
	Type string `json:"type"`
}

DashParameter represents a dashboard filter parameter.

type DashParameterMapping added in v0.2.0

type DashParameterMapping struct {
	CardID      int    `json:"card_id,omitempty"`
	ParameterID string `json:"parameter_id"`
	Target      []any  `json:"target,omitempty"`
}

DashParameterMapping describes how a dashboard parameter maps to a card target.

type DashTab added in v0.2.0

type DashTab struct {
	ID   int    `json:"id"`
	Name string `json:"name"`
}

DashTab represents a dashboard tab.

type Dashboard added in v0.2.0

type Dashboard struct {
	ID          int             `json:"id"`
	Name        string          `json:"name"`
	Description string          `json:"description,omitempty"`
	DashCards   []DashCard      `json:"dashcards,omitempty"`
	Parameters  []DashParameter `json:"parameters,omitempty"`
	Tabs        []DashTab       `json:"tabs,omitempty"`
	Archived    bool            `json:"archived"`
}

Dashboard represents a Metabase dashboard.

type Database

type Database struct {
	ID      int     `json:"id"`
	Name    string  `json:"name"`
	Engine  string  `json:"engine"`
	Details any     `json:"details,omitempty"`
	Tables  []Table `json:"tables,omitempty"`
}

Database represents a Metabase database.

type DatabaseMetadata

type DatabaseMetadata struct {
	ID     int             `json:"id"`
	Name   string          `json:"name"`
	Engine string          `json:"engine"`
	Tables []TableMetadata `json:"tables,omitempty"`
}

DatabaseMetadata represents full database metadata including tables and fields.

type DatasetQuery

type DatasetQuery struct {
	Database int              `json:"database"`
	Type     string           `json:"type"`
	Native   *NativeQuery     `json:"native,omitempty"`
	Query    *StructuredQuery `json:"query,omitempty"`
}

DatasetQuery represents a query request to the Metabase dataset API.

type FKFieldRef

type FKFieldRef struct {
	ID    int        `json:"id"`
	Name  string     `json:"name"`
	Table FKTableRef `json:"table"`
}

FKFieldRef represents a field reference in a foreign key.

type FKTableRef

type FKTableRef struct {
	ID   int    `json:"id"`
	Name string `json:"name"`
}

FKTableRef represents a table reference in a foreign key.

type Field

type Field struct {
	ID           int    `json:"id"`
	Name         string `json:"name"`
	DisplayName  string `json:"display_name"`
	BaseType     string `json:"base_type"`
	DatabaseType string `json:"database_type"`
	SemanticType string `json:"semantic_type,omitempty"`
	TableID      int    `json:"table_id"`
	TableName    string `json:"table_name,omitempty"`
}

Field represents a Metabase field (column).

type FieldSummary

type FieldSummary struct {
	Type  string `json:"type"`
	Value any    `json:"value"`
}

FieldSummary represents summary statistics for a field.

type FieldValues

type FieldValues struct {
	FieldID int     `json:"field_id"`
	Values  [][]any `json:"values"`
}

FieldValues represents distinct values for a field.

type ForeignKey

type ForeignKey struct {
	Relationship string     `json:"relationship"`
	Origin       FKFieldRef `json:"origin"`
	Destination  FKFieldRef `json:"destination"`
}

ForeignKey represents a foreign key relationship.

type HTTPDoer

type HTTPDoer interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPDoer is an interface for executing HTTP requests, enabling test injection.

type NativeQuery

type NativeQuery struct {
	Query        string                 `json:"query"`
	TemplateTags map[string]TemplateTag `json:"template-tags,omitempty"`
}

NativeQuery represents the native SQL query part of a dataset query.

type ParameterValue added in v0.2.0

type ParameterValue struct {
	Value any    `json:"value"`
	Label string `json:"label,omitempty"`
}

ParameterValue represents a value and optional display label for a parameter.

func (*ParameterValue) UnmarshalJSON added in v0.2.0

func (p *ParameterValue) UnmarshalJSON(data []byte) error

UnmarshalJSON supports Metabase parameter value tuples: [value] or [value, label].

type ParameterValues added in v0.2.0

type ParameterValues struct {
	Values        []ParameterValue `json:"values"`
	HasMoreValues bool             `json:"has_more_values"`
}

ParameterValues represents valid values for a dashboard parameter.

type QueryParameter added in v0.2.0

type QueryParameter struct {
	ID     string `json:"id"`
	Type   string `json:"type,omitempty"`
	Target []any  `json:"target,omitempty"`
	Value  any    `json:"value"`
}

QueryParameter represents a parameter passed to a card or dashboard query.

type QueryResult

type QueryResult struct {
	Data QueryResultData `json:"data"`
}

QueryResult represents the result of a dataset query.

type QueryResultData

type QueryResultData struct {
	Columns []ResultColumn `json:"cols"`
	Rows    [][]any        `json:"rows"`
}

QueryResultData holds the columns and rows of a query result.

type ResultColumn

type ResultColumn struct {
	Name         string `json:"name"`
	DisplayName  string `json:"display_name"`
	BaseType     string `json:"base_type"`
	SemanticType string `json:"semantic_type,omitempty"`
}

ResultColumn describes a column in a query result.

type SearchResult

type SearchResult struct {
	ID           int    `json:"id"`
	Name         string `json:"name"`
	Description  string `json:"description,omitempty"`
	Model        string `json:"model"`
	DatabaseID   int    `json:"database_id,omitempty"`
	TableID      int    `json:"table_id,omitempty"`
	CollectionID *int   `json:"collection_id,omitempty"`
	Archived     bool   `json:"archived"`
}

SearchResult represents an item returned by the Metabase search API.

type StructuredQuery added in v0.1.2

type StructuredQuery struct {
	SourceTable  any   `json:"source-table"`
	SourceCardID *int  `json:"source-card,omitempty"`
	Filter       []any `json:"filter,omitempty"`
	Limit        int   `json:"limit,omitempty"`
}

StructuredQuery represents an MBQL structured query.

type Table

type Table struct {
	ID          int    `json:"id"`
	Name        string `json:"name"`
	DisplayName string `json:"display_name"`
	Schema      string `json:"schema"`
	DBId        int    `json:"db_id"`
	EntityType  string `json:"entity_type,omitempty"`
}

Table represents a Metabase table.

type TableMetadata

type TableMetadata struct {
	ID          int     `json:"id"`
	Name        string  `json:"name"`
	DisplayName string  `json:"display_name"`
	Schema      string  `json:"schema"`
	DBId        int     `json:"db_id"`
	Fields      []Field `json:"fields,omitempty"`
}

TableMetadata represents table metadata including field details.

type TemplateTag added in v0.2.0

type TemplateTag struct {
	ID          string `json:"id,omitempty"`
	Name        string `json:"name,omitempty"`
	DisplayName string `json:"display-name,omitempty"`
	Type        string `json:"type,omitempty"`
	WidgetType  string `json:"widget-type,omitempty"`
	Required    bool   `json:"required,omitempty"`
}

TemplateTag represents a native query template tag.

Jump to

Keyboard shortcuts

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