api

package
Version: v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

README

API

The M3O API consists of a set of many public apis all consumed through a single gateway.

Overview

M3O APIs are a standard set of http/json APIs which act as programmable building blocks for rapid development of any product or services. Pick up and use one or more APIs as easily as importing a library and making a function call from a library.

Examples

Here's a simple helloworld

Curl
curl -H "Authorization: Bearer $M3O_API_TOKEN" \
     -H "Content-Type: application/json" \
     -d '{"name": "John"}' \
     https://api.m3o.com/v1/helloworld/call

Find all the shell examples in m3o-sh

Go

Import packages from go.m3o.com

import "go.m3o.com/helloworld"

Create a new client with your API token and call it

helloworldService := helloworld.NewHelloworldService(os.Getenv("M3O_API_TOKEN"))

rsp, err := helloworldService.Call(&helloworld.CallRequest{
	"Name": "Alice",
})

fmt.Println(rsp.Message)

Find all the Go examples in m3o-go

JS

Install the m3o package

npm install m3o

Call helloworld like so

const { HelloworldService } = require("m3o/helloworld");

const helloworldService = new HelloworldService(process.env.M3O_API_TOKEN);

// Call returns a personalised "Hello $name" response
async function callTheHelloworldService() {
  const rsp = await helloworldService.call({
    name: "John",
  });
  console.log(rsp);
}

callTheHelloworldService();

Find more JS examples in m3o-js

See the examples for more use cases.

API Endpoint

The canonical API endpoint is

https://api.m3o.com/v1/

All service endpoints are append like so.

# /v1/[service]/[endpoint]
https://api.m3o.com/v1/helloworld/Call

Authorization

All API calls require a valid API token. New tokens can be generated on the API keys page.

An API token should be passed in the Authorization: Bearer header

Authorization: Bearer $MICRO_API_TOKEN

Data Format

All request/responses are in JSON format and require a Content-Type: application/json header to be passed on each request.

OpenAPI Specs

Find the OpenAPI specs in the spec directory

Public APIs

A list of public APIs can be found on m3o.com/explore

Documentation

Overview

Package api provides a micro api client

Index

Constants

View Source
const (
	// Address for api
	DefaultAddress = "http://localhost:8080"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client enables calls to the micro api

func NewClient

func NewClient(options *Options) *Client

NewClient returns a generic micro client that connects to live by default

func (*Client) Call

func (client *Client) Call(service, endpoint string, request, response interface{}) error

Call enables you to access any endpoint of any service on Micro

func (*Client) SetAddress

func (client *Client) SetAddress(a string)

SetAddress sets the api address

func (*Client) SetTimeout

func (client *Client) SetTimeout(d time.Duration)

SetTimeout sets the http client's timeout

func (*Client) SetToken

func (client *Client) SetToken(t string)

SetToken sets the api auth token

func (*Client) Stream

func (client *Client) Stream(service, endpoint string, request interface{}) (*Stream, error)

Stream enables the ability to stream via websockets

type Handler

type Handler func(ctx context.Context, req interface{}, rsp interface{}) error

type Options

type Options struct {
	// JWT token for authentication
	Token string
	// Address of the micro api
	Address string
	// set a request timeout
	Timeout time.Duration
}

Options of the Client

type Request

type Request struct {
	// eg. "helloworld"
	Service string `json:"service"`
	// eg. "Call"
	Endpoint string `json:"endpoint"`
	// json and then base64 encoded body
	Body string `json:"body"`
}

Request is the request of the generic `api-client` call

type Response

type Response struct {
	// json and base64 encoded response body
	Body string `json:"body"`
	// error fields. Error json example
	// {"id":"go.micro.client","code":500,"detail":"malformed method name: \"\"","status":"Internal Server Error"}
	Code   int    `json:"code"`
	ID     string `json:"id"`
	Detail string `json:"detail"`
	Status string `json:"status"`
}

Response is the response of the generic `api-client` call.

type Service

type Service struct {
	// name of service
	Name string
}

Service handles api requests

func NewService

func NewService(name string) *Service

NewService returns a new api service

func (*Service) Handle

func (service *Service) Handle(endpoint string, handler Handler)

Handle is a http handler for serving requests to the API

func (*Service) Run

func (service *Service) Run(address string) error

type Stream

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

Stream is for streaming request/response

func (*Stream) Recv

func (s *Stream) Recv(v interface{}) error

func (*Stream) Send

func (s *Stream) Send(v interface{}) error

Source Files

Jump to

Keyboard shortcuts

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