Go SDK#

Client for the Rustbox judge API. Standard library only, a context.Context on every call, typed results and errors. Go 1.21 or newer.

Install#

go get github.com/orkait/rustbox-sdk/[email protected]

Quickstart#

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	rustbox "github.com/orkait/rustbox-sdk/go"
)

func main() {
	client, err := rustbox.New(os.Getenv("RUSTBOX_API_KEY"))
	if err != nil {
		log.Fatal(err)
	}
	execution, err := client.Run(context.Background(), rustbox.RunRequest{Language: "python", Code: "print(6 * 7)"})
	if err != nil {
		log.Fatal(err)
	}
	if execution.Result.Verdict != nil && *execution.Result.Verdict == rustbox.VerdictAccepted {
		fmt.Print(*execution.Output.Stdout)
	}
}

Run returns an Execution once the run has finished: Status, Result.Verdict, Result.ExitCode, Output.Stdout, Output.Stderr, Metrics and Evidence. Missing values are nil.

Judge against test cases#

judged, err := client.Judge(ctx, rustbox.JudgeRequest{
	Language: "python",
	Code:     "a, b = map(int, input().split())\nprint(a + b)",
	Cases: []rustbox.JudgeCase{
		{Stdin: "1 2", Expected: "3"},
		{Stdin: "2 2", Expected: "4"},
	},
})

Queue now, read later#

key := "6a0e7c1d-2b3f-4a5e-8d9c-0b1a2c3d4e5f"
accepted, err := client.Submit(ctx, rustbox.RunRequest{Language: "python", Code: "print(1)"}, &key)
execution, err := client.GetResult(ctx, accepted.ID)

With an idempotency key the submit is retried on transient failures; with nil it is sent once.

Errors#

_, err := client.Run(ctx, run)
switch {
case errors.Is(err, rustbox.ErrAuth):
	// 401/403: check the API key
case errors.Is(err, rustbox.ErrRateLimit):
	// 429: see RetryAfterSecs on the *rustbox.Error
case errors.Is(err, rustbox.ErrServer):
	// 5xx after the SDK's retries
case errors.Is(err, rustbox.ErrTimeout):
	// a request timed out
case errors.Is(err, rustbox.ErrStillRunning):
	// Run used up its polling budget: GetResult(ctx, RunID) later
case errors.Is(err, rustbox.ErrAPI):
	// other 4xx: Status and Message on the *rustbox.Error
}

Use errors.As(err, &target) with var target *rustbox.Error to read Kind, Status, RetryAfterSecs, RunID and Message.

Configuration#

client, err := rustbox.New(apiKey, rustbox.WithRegion("in"), rustbox.WithTimeout(30*time.Second))

WithBaseURL pins an exact endpoint. RUSTBOX_BASE_URL and RUSTBOX_REGION are read when the options don't set them.

Source#

github.com/orkait/rustbox-sdk/go