-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
go-swagger example from source #3
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Package classification Cosmos Unchained. | ||
// | ||
// Documentation of our Cosmos Unchained API. | ||
// | ||
// Schemes: http | ||
// BasePath: / | ||
// Version: 1.0.0 | ||
// Host: localhost:1660 | ||
// | ||
// Consumes: | ||
// - application/json | ||
// | ||
// Produces: | ||
// - application/json | ||
// swagger:meta | ||
package docs |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package docs | ||
|
||
import ( | ||
"github.com/shapeshift/unchained-cosmos/service" | ||
) | ||
|
||
// swagger:route GET /account/{pubkey} account-tag accountEndpointId | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These can live anywhere that is a child of the main.go. It could be placed next to each endpoint but the example I was looking at kept it in this separate file. It is a bit cleaner this way but I dont have much preference |
||
// Gets account information | ||
// responses: | ||
// 200: accountResponse | ||
// 500: accountResponseError | ||
|
||
// Account response contains informations about account (balance, sequence, etc) | ||
// swagger:response accountResponse | ||
type accountResponseWrapper struct { | ||
// in:body | ||
Body service.Account | ||
} | ||
|
||
// Account response error | ||
// swagger:response accountResponseError | ||
type accountResponseErrorWrapper struct { | ||
// in:body | ||
Body struct { | ||
// Example: error reading account for cosmos1fx4jwv3aalxqwmrpymn34l582lnehr3eqwuz9e | ||
Error string `json:"error"` | ||
} | ||
} | ||
|
||
// Account url param to specify pubkey (address) to get account info (balance, sequence, etc) | ||
// swagger:parameters accountEndpointId | ||
type accountParams struct { | ||
// Pubkey (address) to get account details for | ||
// in:path | ||
Pubkey string `json:"pubkey"` | ||
} | ||
|
||
// swagger:route GET /account/{pubkey}/txs txs-tag txsEndpointId | ||
// Handles getting TX History for an account by pubkey (address) | ||
// responses: | ||
// 200: txsResponse | ||
// 500: txsResponseError | ||
|
||
// Tsxxs response contains tx history | ||
// swagger:response txsResponse | ||
type txsResponseWrapper struct { | ||
// in:body | ||
Body service.TxHistory | ||
} | ||
|
||
// Param to specify pubkey (address) for tx history | ||
// swagger:parameters txsEndpointId | ||
type txsParams struct { | ||
// Pubkey (address) to get tx history for | ||
// in:path | ||
Pubkey string `json:"pubkey"` | ||
} | ||
|
||
// Txs response error | ||
// swagger:response txsResponseError | ||
type txsResponseErrorWrapper struct { | ||
// in:body | ||
Body struct { | ||
// Example: error reading txhistory for recipient cosmos1fx4jwv3aalxqwmrpymn34l582lnehr3eqwuz9e | ||
Error string `json:"error"` | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"fmt" | ||
"net/http" | ||
|
||
"github.com/gorilla/handlers" | ||
"github.com/gorilla/mux" | ||
"github.com/shapeshift/unchained-cosmos/service" | ||
|
||
|
@@ -44,15 +45,19 @@ func (s *UnchainedRestServer) start() { | |
router.StrictSlash(true) | ||
|
||
router.HandleFunc("/account/{pubkey}", s.GetAccount) | ||
|
||
router.HandleFunc("/account/{pubkey}/txs", s.GetTxHistory) | ||
|
||
listenAddr := s.config.RestListenAddr | ||
if listenAddr == "" { | ||
listenAddr = "localhost:1660" | ||
} | ||
|
||
credentials := handlers.AllowCredentials() | ||
methods := handlers.AllowedMethods([]string{"*"}) | ||
origins := handlers.AllowedOrigins([]string{"*"}) | ||
log.Infof("invoking ListenAndServe on %s", listenAddr) | ||
log.Errorf("ListenAndServe returned: %s", http.ListenAndServe(listenAddr, router)) | ||
log.Errorf("ListenAndServe returned: %s", http.ListenAndServe(listenAddr, handlers.CORS(credentials, methods, origins)(router))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to add cors stuff so calls from the swagger doc server would work |
||
} | ||
|
||
func writeError(writer http.ResponseWriter, errRes ErrorResponse) error { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,18 +12,25 @@ import ( | |
) | ||
|
||
type Account struct { | ||
Pubkey string `json:"pubkey"` | ||
AccountNumber uint64 `json:"accountNumber,string"` | ||
Sequence uint64 `json:"sequence,string"` | ||
Balance string `json:"balance"` | ||
Tokens []TokenAmount `json:"tokens"` | ||
Delegations []Delegation `json:"delegations"` | ||
// Example: cosmos1fx4jwv3aalxqwmrpymn34l582lnehr3eqwuz9e | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is how example values are populated in go-swagger |
||
Pubkey string `json:"pubkey"` | ||
// Example: 9810 | ||
AccountNumber uint64 `json:"accountNumber,string"` | ||
// Example: 43211 | ||
Sequence uint64 `json:"sequence,string"` | ||
// Example: 14250 | ||
Balance string `json:"balance"` | ||
Tokens []TokenAmount `json:"tokens"` | ||
Delegations []Delegation `json:"delegations"` | ||
} | ||
|
||
type Delegation struct { | ||
// Example: cosmosvaloper156gqf9837u7d4c4678yt3rl4ls9c5vuursrrzf | ||
Validator string `json:"validator"` | ||
Amount string `json:"amount"` | ||
Shares string `json:"shares"` | ||
// Example: 45531 | ||
Amount string `json:"amount"` | ||
// Example: 86544 | ||
Shares string `json:"shares"` | ||
} | ||
|
||
func (c *CosmosService) readDelegations(address string) ([]stakingtypes.DelegationResponse, error) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have to import this in main even if its unused so that go-swagger can see it