Skip to content

Commit

Permalink
Refactor remaining services to use the Generic Do helper to reduce bo…
Browse files Browse the repository at this point in the history
…ilerplate
  • Loading branch information
cmmarslender committed Nov 26, 2024
1 parent ea1f07f commit 3b5beeb
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 489 deletions.
42 changes: 7 additions & 35 deletions pkg/rpc/crawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,19 @@ func (s *CrawlerService) NewRequest(rpcEndpoint rpcinterface.Endpoint, opt inter
return s.client.NewRequest(rpcinterface.ServiceCrawler, rpcEndpoint, opt)
}

// Do is just a shortcut to the client's Do method
func (s *CrawlerService) Do(req *rpcinterface.Request, v rpcinterface.IResponse) (*http.Response, error) {
return s.client.Do(req, v)
// GetClient returns the active client for the service
func (s *CrawlerService) GetClient() rpcinterface.Client {
return s.client
}

// GetNetworkInfo gets the network name and prefix from the full node
func (s *CrawlerService) GetNetworkInfo(opts *GetNetworkInfoOptions) (*GetNetworkInfoResponse, *http.Response, error) {
request, err := s.NewRequest("get_network_info", opts)
if err != nil {
return nil, nil, err
}

r := &GetNetworkInfoResponse{}
resp, err := s.Do(request, r)
return r, resp, err
return Do(s, "get_network_info", opts, &GetNetworkInfoResponse{})
}

// GetVersion returns the application version for the service
func (s *CrawlerService) GetVersion(opts *GetVersionOptions) (*GetVersionResponse, *http.Response, error) {
request, err := s.NewRequest("get_version", opts)
if err != nil {
return nil, nil, err
}

r := &GetVersionResponse{}
resp, err := s.Do(request, r)
return r, resp, err
return Do(s, "get_version", opts, &GetVersionResponse{})
}

// GetPeerCountsResponse Response for get_get_peer_counts on crawler
Expand All @@ -56,14 +42,7 @@ type GetPeerCountsResponse struct {

// GetPeerCounts crawler rpc -> get_peer_counts
func (s *CrawlerService) GetPeerCounts() (*GetPeerCountsResponse, *http.Response, error) {
request, err := s.NewRequest("get_peer_counts", nil)
if err != nil {
return nil, nil, err
}

r := &GetPeerCountsResponse{}
resp, err := s.Do(request, r)
return r, resp, err
return Do(s, "get_peer_counts", nil, &GetPeerCountsResponse{})
}

// GetIPsAfterTimestampOptions Options for the get_ips_after_timestamp RPC call
Expand All @@ -82,12 +61,5 @@ type GetIPsAfterTimestampResponse struct {

// GetIPsAfterTimestamp Returns IP addresses seen by the network after a particular timestamp
func (s *CrawlerService) GetIPsAfterTimestamp(opts *GetIPsAfterTimestampOptions) (*GetIPsAfterTimestampResponse, *http.Response, error) {
request, err := s.NewRequest("get_ips_after_timestamp", opts)
if err != nil {
return nil, nil, err
}

r := &GetIPsAfterTimestampResponse{}
resp, err := s.Do(request, r)
return r, resp, err
return Do(s, "get_ips_after_timestamp", opts, &GetIPsAfterTimestampResponse{})
}
88 changes: 12 additions & 76 deletions pkg/rpc/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,19 @@ func (s *DaemonService) NewRequest(rpcEndpoint rpcinterface.Endpoint, opt interf
return s.client.NewRequest(rpcinterface.ServiceDaemon, rpcEndpoint, opt)
}

// Do is just a shortcut to the client's Do method
func (s *DaemonService) Do(req *rpcinterface.Request, v rpcinterface.IResponse) (*http.Response, error) {
return s.client.Do(req, v)
// GetClient returns the active client for the service
func (s *DaemonService) GetClient() rpcinterface.Client {
return s.client
}

// GetNetworkInfo gets the network name and prefix from the full node
func (s *DaemonService) GetNetworkInfo(opts *GetNetworkInfoOptions) (*GetNetworkInfoResponse, *http.Response, error) {
request, err := s.NewRequest("get_network_info", opts)
if err != nil {
return nil, nil, err
}

r := &GetNetworkInfoResponse{}
resp, err := s.Do(request, r)
return r, resp, err
return Do(s, "get_network_info", opts, &GetNetworkInfoResponse{})
}

// GetVersion returns the application version for the service
func (s *DaemonService) GetVersion(opts *GetVersionOptions) (*GetVersionResponse, *http.Response, error) {
request, err := s.NewRequest("get_version", opts)
if err != nil {
return nil, nil, err
}

r := &GetVersionResponse{}
resp, err := s.Do(request, r)
return r, resp, err
return Do(s, "get_version", opts, &GetVersionResponse{})
}

// GetKeysOptions configures how keys are returned in get_keys
Expand All @@ -59,15 +45,7 @@ type GetKeysResponse struct {

// GetKeys returns key information
func (s *DaemonService) GetKeys(opts *GetKeysOptions) (*GetKeysResponse, *http.Response, error) {
request, err := s.NewRequest("get_keys", opts)
if err != nil {
return nil, nil, err
}

r := &GetKeysResponse{}

resp, err := s.Do(request, r)
return r, resp, err
return Do(s, "get_keys", opts, &GetKeysResponse{})
}

// StartServiceOptions start service options
Expand All @@ -83,14 +61,7 @@ type StartServiceResponse struct {

// StartService starts the given service
func (s *DaemonService) StartService(opts *StartServiceOptions) (*StartServiceResponse, *http.Response, error) {
request, err := s.NewRequest("start_service", opts)
if err != nil {
return nil, nil, err
}

r := &StartServiceResponse{}
resp, err := s.Do(request, r)
return r, resp, err
return Do(s, "start_service", opts, &StartServiceResponse{})
}

// StopServiceOptions start service options
Expand All @@ -106,14 +77,7 @@ type StopServiceResponse struct {

// StopService stops the given service
func (s *DaemonService) StopService(opts *StopServiceOptions) (*StopServiceResponse, *http.Response, error) {
request, err := s.NewRequest("stop_service", opts)
if err != nil {
return nil, nil, err
}

r := &StopServiceResponse{}
resp, err := s.Do(request, r)
return r, resp, err
return Do(s, "stop_service", opts, &StopServiceResponse{})
}

// IsRunningOptions is service running options
Expand All @@ -130,14 +94,7 @@ type IsRunningResponse struct {

// IsRunning returns whether a service is running
func (s *DaemonService) IsRunning(opts *IsRunningOptions) (*IsRunningResponse, *http.Response, error) {
request, err := s.NewRequest("is_running", opts)
if err != nil {
return nil, nil, err
}

r := &IsRunningResponse{}
resp, err := s.Do(request, r)
return r, resp, err
return Do(s, "is_running", opts, &IsRunningResponse{})
}

// RunningServicesResponse is service running response
Expand All @@ -148,14 +105,7 @@ type RunningServicesResponse struct {

// RunningServices returns all running services
func (s *DaemonService) RunningServices() (*RunningServicesResponse, *http.Response, error) {
request, err := s.NewRequest("running_services", nil)
if err != nil {
return nil, nil, err
}

r := &RunningServicesResponse{}
resp, err := s.Do(request, r)
return r, resp, err
return Do(s, "running_services", nil, &RunningServicesResponse{})
}

// ExitResponse shows information about the services that were stopped
Expand All @@ -166,14 +116,7 @@ type ExitResponse struct {

// Exit tells the daemon to exit
func (s *DaemonService) Exit() (*ExitResponse, *http.Response, error) {
request, err := s.NewRequest("exit", nil)
if err != nil {
return nil, nil, err
}

r := &ExitResponse{}
resp, err := s.Do(request, r)
return r, resp, err
return Do(s, "exit", nil, &ExitResponse{})
}

// DaemonDeleteAllKeysOpts options for delete all keys request
Expand All @@ -186,12 +129,5 @@ type DaemonDeleteAllKeysResponse struct {

// DeleteAllKeys deletes all keys from the keychain
func (s *DaemonService) DeleteAllKeys(opts *DaemonDeleteAllKeysOpts) (*DaemonDeleteAllKeysResponse, *http.Response, error) {
request, err := s.NewRequest("delete_all_keys", opts)
if err != nil {
return nil, nil, err
}

r := &DaemonDeleteAllKeysResponse{}
resp, err := s.Do(request, r)
return r, resp, err
return Do(s, "delete_all_keys", opts, &DaemonDeleteAllKeysResponse{})
}
78 changes: 11 additions & 67 deletions pkg/rpc/datalayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,14 @@ func (s *DataLayerService) NewRequest(rpcEndpoint rpcinterface.Endpoint, opt int
return s.client.NewRequest(rpcinterface.ServiceDataLayer, rpcEndpoint, opt)
}

// Do is just a shortcut to the client's Do method
func (s *DataLayerService) Do(req *rpcinterface.Request, v rpcinterface.IResponse) (*http.Response, error) {
return s.client.Do(req, v)
// GetClient returns the active client for the service
func (s *DataLayerService) GetClient() rpcinterface.Client {
return s.client
}

// GetVersion returns the application version for the service
func (s *DataLayerService) GetVersion(opts *GetVersionOptions) (*GetVersionResponse, *http.Response, error) {
request, err := s.NewRequest("get_version", opts)
if err != nil {
return nil, nil, err
}

r := &GetVersionResponse{}
resp, err := s.Do(request, r)
return r, resp, err
return Do(s, "get_version", opts, &GetVersionResponse{})
}

// DatalayerGetSubscriptionsOptions options for get_subscriptions
Expand All @@ -51,14 +44,7 @@ func (s *DataLayerService) GetSubscriptions(opts *DatalayerGetSubscriptionsOptio

// Subscriptions calls the subscriptions endpoint to list all subscriptions
func (s *DataLayerService) Subscriptions(opts *DatalayerGetSubscriptionsOptions) (*DatalayerGetSubscriptionsResponse, *http.Response, error) {
request, err := s.NewRequest("subscriptions", opts)
if err != nil {
return nil, nil, err
}

r := &DatalayerGetSubscriptionsResponse{}
resp, err := s.Do(request, r)
return r, resp, err
return Do(s, "subscriptions", opts, &DatalayerGetSubscriptionsResponse{})
}

// DatalayerGetOwnedStoresOptions Options for get_owned_stores
Expand All @@ -72,14 +58,7 @@ type DatalayerGetOwnedStoresResponse struct {

// GetOwnedStores RPC endpoint get_owned_stores
func (s *DataLayerService) GetOwnedStores(opts *DatalayerGetOwnedStoresOptions) (*DatalayerGetOwnedStoresResponse, *http.Response, error) {
request, err := s.NewRequest("get_owned_stores", opts)
if err != nil {
return nil, nil, err
}

r := &DatalayerGetOwnedStoresResponse{}
resp, err := s.Do(request, r)
return r, resp, err
return Do(s, "get_owned_stores", opts, &DatalayerGetOwnedStoresResponse{})
}

// DatalayerGetMirrorsOptions Options for get_mirrors
Expand All @@ -95,14 +74,7 @@ type DatalayerGetMirrorsResponse struct {

// GetMirrors lists the mirrors for the given datalayer store
func (s *DataLayerService) GetMirrors(opts *DatalayerGetMirrorsOptions) (*DatalayerGetMirrorsResponse, *http.Response, error) {
request, err := s.NewRequest("get_mirrors", opts)
if err != nil {
return nil, nil, err
}

r := &DatalayerGetMirrorsResponse{}
resp, err := s.Do(request, r)
return r, resp, err
return Do(s, "get_mirrors", opts, &DatalayerGetMirrorsResponse{})
}

// DatalayerDeleteMirrorOptions options for delete_mirror RPC call
Expand All @@ -118,14 +90,7 @@ type DatalayerDeleteMirrorResponse struct {

// DeleteMirror deletes a datalayer mirror
func (s *DataLayerService) DeleteMirror(opts *DatalayerDeleteMirrorOptions) (*DatalayerDeleteMirrorResponse, *http.Response, error) {
request, err := s.NewRequest("delete_mirror", opts)
if err != nil {
return nil, nil, err
}

r := &DatalayerDeleteMirrorResponse{}
resp, err := s.Do(request, r)
return r, resp, err
return Do(s, "delete_mirror", opts, &DatalayerDeleteMirrorResponse{})
}

// DatalayerAddMirrorOptions options for delete_mirror RPC call
Expand All @@ -143,14 +108,7 @@ type DatalayerAddMirrorResponse struct {

// AddMirror deletes a datalayer mirror
func (s *DataLayerService) AddMirror(opts *DatalayerAddMirrorOptions) (*DatalayerAddMirrorResponse, *http.Response, error) {
request, err := s.NewRequest("add_mirror", opts)
if err != nil {
return nil, nil, err
}

r := &DatalayerAddMirrorResponse{}
resp, err := s.Do(request, r)
return r, resp, err
return Do(s, "add_mirror", opts, &DatalayerAddMirrorResponse{})
}

// DatalayerSubscribeOptions options for subscribe
Expand All @@ -166,14 +124,7 @@ type DatalayerSubscribeResponse struct {

// Subscribe deletes a datalayer mirror
func (s *DataLayerService) Subscribe(opts *DatalayerSubscribeOptions) (*DatalayerSubscribeResponse, *http.Response, error) {
request, err := s.NewRequest("subscribe", opts)
if err != nil {
return nil, nil, err
}

r := &DatalayerSubscribeResponse{}
resp, err := s.Do(request, r)
return r, resp, err
return Do(s, "subscribe", opts, &DatalayerSubscribeResponse{})
}

// DatalayerUnsubscribeOptions options for unsubscribing to a datastore
Expand All @@ -189,12 +140,5 @@ type DatalayerUnsubscribeResponse struct {

// Unsubscribe deletes a datalayer mirror
func (s *DataLayerService) Unsubscribe(opts *DatalayerUnsubscribeOptions) (*DatalayerUnsubscribeResponse, *http.Response, error) {
request, err := s.NewRequest("unsubscribe", opts)
if err != nil {
return nil, nil, err
}

r := &DatalayerUnsubscribeResponse{}
resp, err := s.Do(request, r)
return r, resp, err
return Do(s, "unsubscribe", opts, &DatalayerUnsubscribeResponse{})
}
Loading

0 comments on commit 3b5beeb

Please sign in to comment.