Skip to content

Commit

Permalink
Standardize use of common args in APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronbuchwald committed Aug 6, 2020
1 parent 96cc66c commit 1170989
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 421 deletions.
42 changes: 6 additions & 36 deletions api/admin/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,51 +47,31 @@ func NewService(log logging.Logger, chainManager chains.Manager, httpServer *api
return &common.HTTPHandler{Handler: newServer}
}

// StartCPUProfilerReply are the results from calling StartCPUProfiler
type StartCPUProfilerReply struct {
Success bool `json:"success"`
}

// StartCPUProfiler starts a cpu profile writing to the specified file
func (service *Admin) StartCPUProfiler(_ *http.Request, _ *struct{}, reply *StartCPUProfilerReply) error {
func (service *Admin) StartCPUProfiler(_ *http.Request, _ *struct{}, reply *api.SuccessResponse) error {
service.log.Info("Admin: StartCPUProfiler called")
reply.Success = true
return service.performance.StartCPUProfiler()
}

// StopCPUProfilerReply are the results from calling StopCPUProfiler
type StopCPUProfilerReply struct {
Success bool `json:"success"`
}

// StopCPUProfiler stops the cpu profile
func (service *Admin) StopCPUProfiler(_ *http.Request, _ *struct{}, reply *StopCPUProfilerReply) error {
func (service *Admin) StopCPUProfiler(_ *http.Request, _ *struct{}, reply *api.SuccessResponse) error {
service.log.Info("Admin: StopCPUProfiler called")

reply.Success = true
return service.performance.StopCPUProfiler()
}

// MemoryProfileReply are the results from calling MemoryProfile
type MemoryProfileReply struct {
Success bool `json:"success"`
}

// MemoryProfile runs a memory profile writing to the specified file
func (service *Admin) MemoryProfile(_ *http.Request, _ *struct{}, reply *MemoryProfileReply) error {
func (service *Admin) MemoryProfile(_ *http.Request, _ *struct{}, reply *api.SuccessResponse) error {
service.log.Info("Admin: MemoryProfile called")

reply.Success = true
return service.performance.MemoryProfile()
}

// LockProfileReply are the results from calling LockProfile
type LockProfileReply struct {
Success bool `json:"success"`
}

// LockProfile runs a mutex profile writing to the specified file
func (service *Admin) LockProfile(_ *http.Request, _ *struct{}, reply *LockProfileReply) error {
func (service *Admin) LockProfile(_ *http.Request, _ *struct{}, reply *api.SuccessResponse) error {
service.log.Info("Admin: LockProfile called")

reply.Success = true
Expand All @@ -104,13 +84,8 @@ type AliasArgs struct {
Alias string `json:"alias"`
}

// AliasReply are the results from calling Alias
type AliasReply struct {
Success bool `json:"success"`
}

// Alias attempts to alias an HTTP endpoint to a new name
func (service *Admin) Alias(_ *http.Request, args *AliasArgs, reply *AliasReply) error {
func (service *Admin) Alias(_ *http.Request, args *AliasArgs, reply *api.SuccessResponse) error {
service.log.Info("Admin: Alias called with URL: %s, Alias: %s", args.Endpoint, args.Alias)

if len(args.Alias) > maxAliasLength {
Expand All @@ -127,13 +102,8 @@ type AliasChainArgs struct {
Alias string `json:"alias"`
}

// AliasChainReply are the results from calling AliasChain
type AliasChainReply struct {
Success bool `json:"success"`
}

// AliasChain attempts to alias a chain to a new name
func (service *Admin) AliasChain(_ *http.Request, args *AliasChainArgs, reply *AliasChainReply) error {
func (service *Admin) AliasChain(_ *http.Request, args *AliasChainArgs, reply *api.SuccessResponse) error {
service.log.Info("Admin: AliasChain called with Chain: %s, Alias: %s", args.Chain, args.Alias)

if len(args.Alias) > maxAliasLength {
Expand Down
12 changes: 6 additions & 6 deletions api/common_args_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ type SuccessResponse struct {
Success bool `json:"success"`
}

// TxIDResponse contains the ID of a transaction
type TxIDResponse struct {
// JsonTxID contains the ID of a transaction
type JsonTxID struct {
TxID ids.ID `json:"txID"`
}

Expand All @@ -20,12 +20,12 @@ type UserPass struct {
Password string `json:"password"`
}

// AddressResponse contains an address
type AddressResponse struct {
// JsonAddress contains an address
type JsonAddress struct {
Address string `json:"address"`
}

// AddressesResponse contains a list of address
type AddressesResponse struct {
// JsonAddresses contains a list of address
type JsonAddresses struct {
Addresses []string `json:"addresses"`
}
7 changes: 1 addition & 6 deletions api/ipcs/server.go → api/ipcs/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,8 @@ type UnpublishBlockchainArgs struct {
BlockchainID string `json:"blockchainID"`
}

// UnpublishBlockchainReply are the results from calling UnpublishBlockchain
type UnpublishBlockchainReply struct {
Success bool `json:"success"`
}

// UnpublishBlockchain closes publishing of a blockchainID
func (ipc *IPCs) UnpublishBlockchain(r *http.Request, args *UnpublishBlockchainArgs, reply *UnpublishBlockchainReply) error {
func (ipc *IPCs) UnpublishBlockchain(r *http.Request, args *UnpublishBlockchainArgs, reply *api.SuccessResponse) error {
ipc.log.Info("IPCs: UnpublishBlockchain called with BlockchainID: %s", args.BlockchainID)
chainID, err := ipc.chainManager.Lookup(args.BlockchainID)
if err != nil {
Expand Down
55 changes: 10 additions & 45 deletions api/keystore/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

zxcvbn "github.com/nbutton23/zxcvbn-go"

"github.com/ava-labs/gecko/api"
"github.com/ava-labs/gecko/chains/atomic"
"github.com/ava-labs/gecko/database"
"github.com/ava-labs/gecko/database/encdb"
Expand Down Expand Up @@ -130,19 +131,8 @@ func (ks *Keystore) getUser(username string) (*User, error) {
return usr, ks.codec.Unmarshal(usrBytes, usr)
}

// CreateUserArgs are arguments for passing into CreateUser requests
type CreateUserArgs struct {
Username string `json:"username"`
Password string `json:"password"`
}

// CreateUserReply is the response from calling CreateUser
type CreateUserReply struct {
Success bool `json:"success"`
}

// CreateUser creates an empty user with the provided username and password
func (ks *Keystore) CreateUser(_ *http.Request, args *CreateUserArgs, reply *CreateUserReply) error {
func (ks *Keystore) CreateUser(_ *http.Request, args *api.UserPass, reply *api.SuccessResponse) error {
ks.lock.Lock()
defer ks.lock.Unlock()

Expand All @@ -155,16 +145,13 @@ func (ks *Keystore) CreateUser(_ *http.Request, args *CreateUserArgs, reply *Cre
return nil
}

// ListUsersArgs are the arguments to ListUsers
type ListUsersArgs struct{}

// ListUsersReply is the reply from ListUsers
type ListUsersReply struct {
Users []string `json:"users"`
}

// ListUsers lists all the registered usernames
func (ks *Keystore) ListUsers(_ *http.Request, args *ListUsersArgs, reply *ListUsersReply) error {
func (ks *Keystore) ListUsers(_ *http.Request, args *struct{}, reply *ListUsersReply) error {
ks.lock.Lock()
defer ks.lock.Unlock()

Expand All @@ -180,19 +167,13 @@ func (ks *Keystore) ListUsers(_ *http.Request, args *ListUsersArgs, reply *ListU
return it.Error()
}

// ExportUserArgs are the arguments to ExportUser
type ExportUserArgs struct {
Username string `json:"username"`
Password string `json:"password"`
}

// ExportUserReply is the reply from ExportUser
type ExportUserReply struct {
User formatting.CB58 `json:"user"`
}

// ExportUser exports a serialized encoding of a user's information complete with encrypted database values
func (ks *Keystore) ExportUser(_ *http.Request, args *ExportUserArgs, reply *ExportUserReply) error {
func (ks *Keystore) ExportUser(_ *http.Request, args *api.UserPass, reply *ExportUserReply) error {
ks.lock.Lock()
defer ks.lock.Unlock()

Expand Down Expand Up @@ -234,18 +215,13 @@ func (ks *Keystore) ExportUser(_ *http.Request, args *ExportUserArgs, reply *Exp

// ImportUserArgs are arguments for ImportUser
type ImportUserArgs struct {
Username string `json:"username"`
Password string `json:"password"`
User formatting.CB58 `json:"user"`
}

// ImportUserReply is the response for ImportUser
type ImportUserReply struct {
Success bool `json:"success"`
api.UserPass
User formatting.CB58 `json:"user"`
}

// ImportUser imports a serialized encoding of a user's information complete with encrypted database values, integrity checks the password, and adds it to the database
func (ks *Keystore) ImportUser(r *http.Request, args *ImportUserArgs, reply *ImportUserReply) error {
// ImportUser imports a serialized encoding of a user's information complete with encrypted database values,
// integrity checks the password, and adds it to the database
func (ks *Keystore) ImportUser(r *http.Request, args *ImportUserArgs, reply *api.SuccessResponse) error {
ks.lock.Lock()
defer ks.lock.Unlock()

Expand Down Expand Up @@ -293,19 +269,8 @@ func (ks *Keystore) ImportUser(r *http.Request, args *ImportUserArgs, reply *Imp
return nil
}

// DeleteUserArgs are arguments for passing into DeleteUser requests
type DeleteUserArgs struct {
Username string `json:"username"`
Password string `json:"password"`
}

// DeleteUserReply is the response from calling DeleteUser
type DeleteUserReply struct {
Success bool `json:"success"`
}

// DeleteUser deletes user with the provided username and password.
func (ks *Keystore) DeleteUser(_ *http.Request, args *DeleteUserArgs, reply *DeleteUserReply) error {
func (ks *Keystore) DeleteUser(_ *http.Request, args *api.UserPass, reply *api.SuccessResponse) error {
ks.lock.Lock()
defer ks.lock.Unlock()

Expand Down
Loading

0 comments on commit 1170989

Please sign in to comment.