restish

command module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 26, 2020 License: MIT Imports: 3 Imported by: 0

README

Restish Logo

Restish is a CLI for interacting with REST-ish HTTP APIs with some nice features built-in, like always having the latest API resources, fields, and operations available when they go live on the API without needing to install or update anything.

Features include:

Why use this?

Every API deserves a CLI for quick access and for power users to script against the service. Building CLIs from scratch is a pain. Restish provides one tool your users can install that just works for multiple APIs and is always up to date, because the interface is defined by the server.

This project started life as a fork of OpenAPI CLI Generator.

Installation

Grab a release, otherwise if you have Go installed:

# Download / build / install
$ go get -u github.com/danielgtaylor/restish

Usage

Generic HTTP verbs require no setup and are easy to use. If no verb is supplied then a GET is assumed. The https:// is also optional as it is the default.

# Perform an HTTP GET request
$ restish jsonplaceholder.typicode.com/users/1

# Above is equivalent to:
$ restish get https://jsonplaceholder.typicode.com/users/1

You will see a response like:

HTTP/2.0 200 OK
Content-Encoding: br
Content-Type: application/json; charset=utf-8
Date: Wed, 20 May 2020 05:50:52 GMT

{
  "address": {
    "city": "Gwenborough",
    "geo": {
      "lat": "-37.3159",
      "lng": "81.1496"
    },
    "street": "Kulas Light",
    "suite": "Apt. 556",
    "zipcode": "92998-3874"
  },
  "company": {
    "bs": "harness real-time e-markets",
    "catchPhrase": "Multi-layered client-server neural-net",
    "name": "Romaguera-Crona"
  },
  "email": "Sincere@april.biz",
  "id": 1,
  "name": "Leanne Graham",
  "phone": "1-770-736-8031 x56442",
  "username": "Bret",
  "website": "hildegard.org"
}

Various inputs can be passed in as needed:

# Pass a query param (either way)
$ restish example.com?search=foo
$ restish -q search=foo example.com

# Pass a header
$ restish -H MyHeader:value example.com

# Pass in a body via a file
$ restish post example.com/users <user.json

# Pass in body via CLI shorthand
$ restish post example.com/users name: Kari, tags[]: admin

Headers and query params can also be set via environment variables, for example:

# Set via env vars
$ export RSH_HEADER=header1:value1,header2:value2
$ restish example.com/users

If you have persistent headers or query params you'd like to set, then consider registering the API endpoint with Restish rather than exporting environment variables. Read on to find out how.

Registering an API Endpoint

APIs can be registered in order to provide API description auto-discovery with convenience commands and authentication.

Each registered API can have a number of named auth profiles which can be selected via the -p or --rsh-profile argument. The default profile is called default.

Each profile can have a number of preset headers or query params, a type of auth, and any auth-specific params. The following auth types are available:

Type Inputs
http-basic username, password
oauth-client-credentials client_id, client_secret, token_url, scopes
oauth-authorization-code client_id, authorize_url, token_url, scopes

Register a new API like this, which launches an interactive configuration interface to set up base URIs, headers & query params, and auth.

# Register a new API named `example`
$ restish api configure example

# Call the API with a specific profile
$ restish -p myProfile example list-items

# Set an environment variable for the profile
$ export RSH_PROFILE=myProfile
$ restish example list-items

Registered APIs are stored in ~/.restish/apis.json. A very basic example config for local service testing with Huma or FastAPI and named local might look like:

{
  "local": {
    "base": "http://localhost:8888"
  }
}

API Endpoint Usage

The registered API short name can be used instead of the domain, for example:

# Supposed we have an API called `ex`. Show what the API can do:
$ restish ex --help

# If there is e.g. an OpenAPI spec linked, call one of the operations:
$ restish ex list-items -q search=active

# If not, you can still use the shorthand in URLs:
$ restish ex/items?search=active

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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