client

package
Version: v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

README

M3O API Client godoc

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

Usage

package main

import (
    "fmt"
    "os"

    "m3o.dev/api/client"
)

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

type Response struct {
	Message string `json:"message"`
}

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

func main() {
	c := client.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"

	"m3o.dev/api/client"
)

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

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

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

func main() {
	c := client.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 client provides a micro api client

Index

Constants

View Source
const (
	// Address for api
	DefaultAddress = "https://api.m3o.com/v1"
)

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

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