github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/cmd/commands/cli/clio/tequilapi.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package clio
    19  
    20  import (
    21  	"fmt"
    22  
    23  	"github.com/mysteriumnetwork/node/config"
    24  	tequilapi_client "github.com/mysteriumnetwork/node/tequilapi/client"
    25  
    26  	"github.com/urfave/cli/v2"
    27  )
    28  
    29  // NewTequilApiClient - initializes and returns a pointer to tequilapi client - also fetches config using it
    30  func NewTequilApiClient(ctx *cli.Context) (*tequilapi_client.Client, error) {
    31  	address := TequilAPIAddress(ctx)
    32  	port := TequilAPIPort(ctx)
    33  	client := tequilapi_client.NewClient(address, port)
    34  
    35  	_, err := client.Healthcheck()
    36  	if err != nil {
    37  		Error(fmt.Sprintf("failed to connect to node via url: %s:%d", address, port))
    38  		return nil, err
    39  	}
    40  	return client, nil
    41  }
    42  
    43  // TequilAPIAddress - wil resolve default tequilapi address or from flag if one is provided
    44  func TequilAPIAddress(ctx *cli.Context) string {
    45  	flag := config.FlagTequilapiAddress
    46  
    47  	if ctx.IsSet(flag.Name) {
    48  		return ctx.String(flag.Name)
    49  	}
    50  
    51  	return flag.Value
    52  }
    53  
    54  // TequilAPIPort - wil resolve default tequilapi port or from flag if one is provided
    55  func TequilAPIPort(ctx *cli.Context) int {
    56  	flag := config.FlagTequilapiPort
    57  
    58  	if ctx.IsSet(flag.Name) {
    59  		return ctx.Int(flag.Name)
    60  	}
    61  
    62  	return flag.Value
    63  }