rest

package
v2.0.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2025 License: Apache-2.0 Imports: 15 Imported by: 1

README

Cosmos SDK REST API

This document describes how to use a service that exposes endpoints based on Cosmos SDK Protobuf message types. Each endpoint responds with data in JSON format.

General Description

The service allows querying the blockchain using any type of Protobuf message available in the Cosmos SDK application through HTTP POST requests. Each endpoint corresponds to a Cosmos SDK protocol message (proto), and responses are returned in JSON format.

Example

1. QueryBalanceRequest

This endpoint allows querying the balance of an account given an address and a token denomination.

  • URL: localhost:8080/cosmos.bank.v2.QueryBalanceRequest

  • Method: POST

  • Headers:

    • Content-Type: application/json
  • Body (JSON):

    {
        "address": "<ACCOUNT_ADDRESS>",
        "denom": "<TOKEN_DENOMINATION>"
    }
    
    • address: Account address on the Cosmos network.
    • denom: Token denomination (e.g., stake).
  • Request Example:

    POST localhost:8080/cosmos.bank.v2.QueryBalanceRequest
    Content-Type: application/json
    
    {
        "address": "cosmos16tms8tax3ha9exdu7x3maxrvall07yum3rdcu0",
        "denom": "stake"
    }
    
  • Response Example (JSON):

    {
        "balance": {
            "denom": "stake",
            "amount": "1000000"
        }
    }
    

    The response shows the balance of the specified token for the given account.

Using Tools

1. Using curl

To make a request using curl, you can run the following command:

curl -X POST localhost:8080/cosmos.bank.v2.QueryBalanceRequest \
  -H "Content-Type: application/json" \
  -d '{
    "address": "cosmos16tms8tax3ha9exdu7x3maxrvall07yum3rdcu0",
    "denom": "stake"
  }'

Documentation

Index

Constants

View Source
const (
	ContentTypeJSON = "application/json"
	MaxBodySize     = 1 << 20 // 1 MB
)
View Source
const (
	ServerName = "rest"
)

Variables

This section is empty.

Functions

func NewDefaultHandler

func NewDefaultHandler[T transaction.Tx](appManager appmanager.AppManager[T]) http.Handler

Types

type CfgOption

type CfgOption func(*Config)

func Disable

func Disable() CfgOption

Disable the rest server by default (default enabled).

func OverwriteDefaultConfig

func OverwriteDefaultConfig(newCfg *Config) CfgOption

OverwriteDefaultConfig overwrites the default config with the new config.

type Config

type Config struct {
	// Enable defines if the REST server should be enabled.
	Enable bool `mapstructure:"enable" toml:"enable" comment:"Enable defines if the REST server should be enabled."`
	// Address defines the API server to listen on
	Address string `mapstructure:"address" toml:"address" comment:"Address defines the REST server address to bind to."`
}

Config defines configuration for the REST server.

func DefaultConfig

func DefaultConfig() *Config

type DefaultHandler

type DefaultHandler[T transaction.Tx] struct {
	// contains filtered or unexported fields
}

func (*DefaultHandler[T]) ServeHTTP

func (h *DefaultHandler[T]) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Server

type Server[T transaction.Tx] struct {
	// contains filtered or unexported fields
}

func New

func New[T transaction.Tx](
	logger log.Logger,
	appManager appmanager.AppManager[T],
	cfg server.ConfigMap,
	cfgOptions ...CfgOption,
) (*Server[T], error)

func NewWithConfigOptions

func NewWithConfigOptions[T transaction.Tx](opts ...CfgOption) *Server[T]

NewWithConfigOptions creates a new REST server with the provided config options. It is *not* a fully functional server (since it has been created without dependencies) The returned server should only be used to get and set configuration.

func (*Server[T]) Config

func (s *Server[T]) Config() any

func (*Server[T]) Name

func (s *Server[T]) Name() string

func (*Server[T]) Start

func (s *Server[T]) Start(ctx context.Context) error

func (*Server[T]) Stop

func (s *Server[T]) Stop(ctx context.Context) error

Jump to

Keyboard shortcuts

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