notcodersdk

package
v1.0.0-rc.0 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package notcodersdk contains manually-vendored code from github.com/coder/coder/v2/codersdk.

This code is currently required for sending workspace build logs to coder. It was manually vendored to avoid dependency issues.

If the direct integration is moved outside of envbuilder, this package can safely be removed. See the below issues for context: - https://github.com/coder/envbuilder/issues/178 - https://github.com/coder/coder/issues/11342 - https://github.com/coder/envbuilder/issues/193

Index

Constants

View Source
const (
	SessionTokenHeader = "Coder-Session-Token"
)

Variables

View Source
var ExternalLogSourceID = uuid.MustParse("3b579bf4-1ed8-4b99-87a8-e9a1e3410410")

ExternalLogSourceID is the statically-defined ID of a log-source that appears as "External" in the dashboard.

This is to support legacy API-consumers that do not create their own log-source. This should be removed in the future.

Functions

func ExpectJSONMime

func ExpectJSONMime(res *http.Response) error

ExpectJSONMime is a helper function that will assert the content type of the response is application/json.

func LogsSender deprecated

func LogsSender(sourceID uuid.UUID, patchLogs func(ctx context.Context, req PatchLogs) error, logger slog.Logger, opts ...func(*logsSenderOptions)) (sendLog func(ctx context.Context, log ...Log) error, flushAndClose func(context.Context) error)

LogsSender will send agent startup logs to the server. Calls to sendLog are non-blocking and will return an error if flushAndClose has been called. Calling sendLog concurrently is not supported. If the context passed to flushAndClose is canceled, any remaining logs will be discarded.

Deprecated: Use NewLogSender instead, based on the v2 Agent API.

func ReadBodyAsError

func ReadBodyAsError(res *http.Response) error

ReadBodyAsError reads the response as a codersdk.Response, and wraps it in a codersdk.Error type for easy marshaling.

This will always return an error, so only call it if the response failed your expectations. Usually via status code checking. nolint:staticcheck

Types

type AgentSubsystem

type AgentSubsystem string
const (
	AgentSubsystemEnvbuilder AgentSubsystem = "envbuilder"
)

type Client

type Client struct {
	HTTPClient *http.Client
	URL        *url.URL

	// SessionTokenHeader is an optional custom header to use for setting tokens. By
	// default 'Coder-Session-Token' is used.
	SessionTokenHeader string

	// PlainLogger may be set to log HTTP traffic in a human-readable form.
	// It uses the LogBodies option.
	PlainLogger io.Writer
	// contains filtered or unexported fields
}

Client wraps `notcodersdk.Client` with specific functions scoped to a workspace agent.

func New

func New(serverURL *url.URL) *Client

New returns a client that is used to interact with the Coder API from a workspace agent.

func (*Client) PatchLogs deprecated

func (c *Client) PatchLogs(ctx context.Context, req PatchLogs) error

PatchLogs writes log messages to the agent startup script. Log messages are limited to 1MB in total.

Deprecated: use the DRPCAgentClient.BatchCreateLogs instead

func (*Client) Request

func (c *Client) Request(ctx context.Context, method, path string, body interface{}, opts ...RequestOption) (*http.Response, error)

Request performs a HTTP request with the body provided. The caller is responsible for closing the response body.

func (*Client) SessionToken

func (c *Client) SessionToken() string

SessionToken returns the currently set token for the client.

func (*Client) SetSessionToken

func (c *Client) SetSessionToken(token string)

SetSessionToken returns the currently set token for the client.

type Error

type Error struct {
	Response

	Helper string
	// contains filtered or unexported fields
}

Error represents an unaccepted or invalid request to the API. @typescript-ignore Error

func (*Error) Error

func (e *Error) Error() string

func (*Error) Friendly

func (e *Error) Friendly() string

func (*Error) Method

func (e *Error) Method() string

func (*Error) StatusCode

func (e *Error) StatusCode() int

func (*Error) URL

func (e *Error) URL() string

type Log

type Log struct {
	CreatedAt time.Time `json:"created_at"`
	Output    string    `json:"output"`
	Level     LogLevel  `json:"level"`
}

type LogLevel

type LogLevel string
const (
	LogLevelTrace LogLevel = "trace"
	LogLevelDebug LogLevel = "debug"
	LogLevelInfo  LogLevel = "info"
	LogLevelWarn  LogLevel = "warn"
	LogLevelError LogLevel = "error"
)

type PatchLogs

type PatchLogs struct {
	LogSourceID uuid.UUID `json:"log_source_id"`
	Logs        []Log     `json:"logs"`
}

type RequestOption

type RequestOption func(*http.Request)

RequestOption is a function that can be used to modify an http.Request.

type Response

type Response struct {
	// Message is an actionable message that depicts actions the request took.
	// These messages should be fully formed sentences with proper punctuation.
	// Examples:
	// - "A user has been created."
	// - "Failed to create a user."
	Message string `json:"message"`
	// Detail is a debug message that provides further insight into why the
	// action failed. This information can be technical and a regular golang
	// err.Error() text.
	// - "database: too many open connections"
	// - "stat: too many open files"
	Detail string `json:"detail,omitempty"`
	// Validations are form field-specific friendly error messages. They will be
	// shown on a form field in the UI. These can also be used to add additional
	// context if there is a set of errors in the primary 'Message'.
	Validations []ValidationError `json:"validations,omitempty"`
}

Response represents a generic HTTP response.

type ValidationError

type ValidationError struct {
	Field  string `json:"field" validate:"required"`
	Detail string `json:"detail" validate:"required"`
}

ValidationError represents a scoped error to a user input.

func (ValidationError) Error

func (e ValidationError) Error() string

Jump to

Keyboard shortcuts

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