gowandbox

package module
v0.0.0-...-3d87dee Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2021 License: MIT Imports: 9 Imported by: 0

README

GoWandBox


A simple wrapper for the WandBox API, written in Golang!

Documentation can be found at classpythonaddike.github.io/gowandbox/

Note: This wrapper supports most of the WandBox API's endpoints, except for the POST /permlink. This was because, I couldn't figure out how to work with the endpoint - I kept winding up with 500 - Internal Server Errors. I'm not sure whether it was a bug in the test data that I was using, or if its a bug in the API itself. Either way, that endpoint has not been implemented in this wrapper, and I apologise for any inconveniences. Feel free to make a PR, or open an issue if you're able to figure it out!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var WandBoxUrl string = defaultUrl

Url to WandBox API

Functions

func ChangeWandBoxUrl

func ChangeWandBoxUrl(url string)

Changes the default WandBox URL, useful to use with your own instance of WandBox

func ResetWandBoxUrl

func ResetWandBoxUrl()

Resets the WandBox URL used

Types

type GWBLanguage

type GWBLanguage struct {
	CompilerOptionRaw bool `json:"compiler-option-raw"`
	RuntimeOptionRaw  bool `json:"runtime-option-raw"`

	DisplayCompileCommand string `json:"display-compile-command"`

	Switches []struct {
		Default      interface{} `json:"default"`
		Name         string      `json:"name"`
		DisplayFlags string      `json:"display-flags,omitempty"`
		DisplayName  string      `json:"display-name,omitempty"`
	} `json:"switches"`

	Name        string   `json:"name"`
	Version     string   `json:"version"`
	Language    string   `json:"language"`
	DisplayName string   `json:"display-name"`
	Templates   []string `json:"templates"`
}

Language returned by the `/list.json` endpoint.

CompilerOptionRaw and RuntimeOptionRaw are booleans stating whether they support passing of these options to `/compile.json` or `/compile.ndjson`.

DisplayCompileCommand shows the command that will be run to execute it.

Name, Version, Language, DisplayName, Templates show information about the language.

Switches is a list of switches that can be used during compile time.

func GetLanguages

func GetLanguages(ctx context.Context) ([]GWBLanguage, error)

Returns a list of languages, maps to the `/list.json` endpoint

type GWBNDMessage

type GWBNDMessage struct {
	Data string `json:"data"`
	Type string `json:"type"`
}

Data returned by the `/compile.ndjson` endpoint.

type GWBNDProgram

type GWBNDProgram struct {
	Compiler string `json:"compiler"`

	Code  string    `json:"code"`
	Codes []Program `json:"codes"`

	Options           string `json:"options"`
	CompilerOptionRaw string `json:"compiler-option-raw"`
	RuntimeOptionRaw  string `json:"runtime-option-raw"`

	Stdin string `json:"stdin"`
}

GWBNDProgram struct, to be used when posting to `/compile.ndjson` The fields are very similar to GWBProgram, but without the SaveCode option

func NewGWBNDProgram

func NewGWBNDProgram() *GWBNDProgram

Returns new GWBProgram struct

func (*GWBNDProgram) Execute

func (g *GWBNDProgram) Execute(ctx context.Context) (GWBNDReader, error)

Method to execute a GWBProgram

If no errors ocurred, the result is returned in the form of a GWBNDReader struct. If the response code is not 200, an error is returned.

Maps to the `/compile.ndjson` endpoint

type GWBNDReader

type GWBNDReader struct {
	// contains filtered or unexported fields
}

Reader to stream data from the `/compile.ndjson` endpoint. Data can be obtained by calling the `Next()` method.

func (*GWBNDReader) Next

func (r *GWBNDReader) Next() (GWBNDMessage, error)

Method to read ndjson. Returns a GWNDBMessage struct, which provides the type of the message, and data. On reaching the end, an `io.EOF` error is returned.

type GWBPermLink struct {
	Parameter struct {
		Compiler string `json:"compiler"`

		Code  string    `json:"code"`
		Codes []Program `json:"codes"`

		Options string `json:"options"`

		Stdin             string `json:"stdin"`
		CompilerOptionRaw string `json:"compiler-option-raw"`
		RuntimeOptionRaw  string `json:"runtime-option-raw"`

		CreatedAt int64 `json:"created_at"`
	} `json:"parameter"`

	Result struct {
		Status string `json:"status"`
		Signal string `json:"signal"`

		CompilerOutput  string `json:"compiler_output"`
		CompilerError   string `json:"compiler_error"`
		CompilerMessage string `json:"compiler_message"`

		ProgramOutput  string `json:"program_output"`
		ProgramError   string `json:"program_error"`
		ProgramMessage string `json:"program_message"`
	} `json:"result"`
}

Provides information about a permlink

Parameter displays the parameters provided at runtime Note that `CreatedAt` represents the time in ISO-8601 format.

Result shows the output of the compile.

func GetPermLink(link string, ctx context.Context) (GWBPermLink, error)

Returns information about a program that was run, given the permlink. Maps to the `/permlink/link` endpoint. If the permlink is not found, and 500 error is returned.

type GWBProgram

type GWBProgram struct {
	Compiler string `json:"compiler"`

	Code  string    `json:"code"`
	Codes []Program `json:"codes"`

	Options           string `json:"options"`
	CompilerOptionRaw string `json:"compiler-option-raw"`
	RuntimeOptionRaw  string `json:"runtime-option-raw"`

	Stdin string `json:"stdin"`

	SaveCode bool `json:"save"`
}

GWBProgram struct, to be used when posting to `/compile.json`

Full compiler list can be seen by using `GetLanguages()`

Code is a string containing the main code that will be run. It will be the entry point to your program.

Codes may contain other files to include while running/compiling

Options, CompilerOptionRaw, RuntimeOptionRaw are strings containing comma separated values. See github.com/melpon/wandbox/blob/master/kennel2/API.rst#sample-1 for more ino

Stdin is a string of newline separated values, which will be passed in as input.

Passing `true` to SaveCode will generate a permlink that will be returned, and the program will be saved to WandBox at that url. Default value is false.

func NewGWBProgram

func NewGWBProgram() *GWBProgram

func (*GWBProgram) Execute

func (g *GWBProgram) Execute(ctx context.Context) (GWBResult, error)

Method to execute a GWBProgram

If no errors ocurred, the result is returned in the form of a GWBResult struct. If the response code is not 200, an error is returned.

Maps to the `/compile.json` endpoint

type GWBResult

type GWBResult struct {
	Status string `json:"status"`
	Signal string `json:"signal"`

	CompilerOutput  string `json:"compiler_output"`
	CompilerError   string `json:"compiler_error"`
	CompilerMessage string `json:"compiler_message"`

	ProgramOutput  string `json:"program_output"`
	ProgramError   string `json:"program_error"`
	ProgramMessage string `json:"program_message"`

	Permlink string `json:"permlink"`
	Url      string `json:"url"`
}

Result of compiling a program (/compile.json endpoint).

Status and Signal provide information about how the program exited.

CompilerOutput provides output during compile time, and CompilerError provides any errors that occured during compile time. CompilerMessage is a combination of both.

ProgramOutput provides output during runtime, and ProgramError provides any errors that occured during run time. ProgramMessage is a combination of both.

Permlink - permlink of the code, only provided if SaveCode was set to true when compiling. Url - Url to view the code and output in the browser.

type GWBTemplate

type GWBTemplate struct {
	Code string `json:"code"`
}

func GetTemplate

func GetTemplate(language string, ctx context.Context) (GWBTemplate, error)

Returns the template for a given language. If the language is not found, an error is returned (bad template). Maps to the `/template/:template` endpoint

type GWBUser

type GWBUser struct {
	Login    bool   `json:"login"`
	Username string `json:"username"`
}

Represents a WandBox user. Provides the username, and whether the user is logged in or not.

func GetUser

func GetUser(sessionKey string, ctx context.Context) (GWBUser, error)

Returns a user, given the session key (provided by WandBox). If the session key is invalid, a blank string is returned for the username. Maps to the `/user.json` endpoint.

type Program

type Program struct {
	File string `json:"file"`
	Code string `json:"code"`
}

Program Struct, used while compiling Takes in the filename, and code

Jump to

Keyboard shortcuts

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