api

package
v1.0.0-alpha.4 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2022 License: Apache-2.0 Imports: 9 Imported by: 0

README

Micro API Client godoc

This is the Go client to access APIs on the Micro Platform

Usage

package main

import (
    "fmt"
    "os"

    "github.com/micro/micro/v3/client/api"
)

type Request struct {
	Name string `json:"name"`
}

type Response struct {
	Msg string `json:"msg"`
}

var (
	token = os.Getenv("TOKEN")
)

func main() {
	c := api.NewClient(nil)

	// set your api token
	c.SetToken(token)

   	req := &Request{
		Name: "John",
	}
	
	var rsp Response

	if err := c.Call("helloworld", "call", req, &rsp); err != nil {
		fmt.Println(err)
		return
	}
	
	fmt.Println(rsp)
}

Streaming

The client supports streaming

package main

import (
	"fmt"

	"github.com/micro/micro/v3/client/api"
)

type Request struct {
	Count string `json:"count"`
}

type Response struct {
	Count string `json:"count"`
}

var (
	token, _ = os.Getenv("TOKEN")
)

func main() {
	c := api.NewClient(nil)

	// set your api token
	c.SetToken(token)
	
	stream, err := c.Stream("streams", "subscribe", Request{Count: "10"})
	if err != nil {
		fmt.Println(err)
		return
	}

	for {
		var rsp Response
		if err := stream.Recv(&rsp); err != nil {
			fmt.Println(err)
			return
		}
		fmt.Println("got", rsp.Count)
	}
}

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 generic calls to micro

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) Handle

func (client *Client) Handle(service, endpoint string) http.Handler

Handle is a http handler for serving requests to the API

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 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 Stream

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

func (*Stream) Recv

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

func (*Stream) Send

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

Jump to

Keyboard shortcuts

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