github.com/status-im/status-go@v1.1.0/services/wallet/thirdparty/mercuryo/request_currencies.go (about)

     1  package mercuryo
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"net/http"
     8  )
     9  
    10  const (
    11  	currenciesURL = "https://api.mercuryo.io/v1.6/lib/currencies" // nolint: gosec
    12  )
    13  
    14  type Token struct {
    15  	Symbol   string `json:"symbol"`
    16  	Address  string `json:"address"`
    17  	Decimals uint   `json:"decimals"`
    18  	Img      string `json:"img"`
    19  	Network  int    `json:"network"`
    20  }
    21  
    22  type CurrenciesResponse struct {
    23  	Data   CurrenciesData `json:"data"`
    24  	Status int            `json:"status"`
    25  }
    26  
    27  type CurrenciesData struct {
    28  	Config Config `json:"config"`
    29  }
    30  
    31  type Config struct {
    32  	CryptoCurrencies []CryptoCurrency `json:"crypto_currencies"`
    33  }
    34  
    35  type CryptoCurrency struct {
    36  	Symbol   string `json:"currency"`
    37  	Network  string `json:"network"`
    38  	Contract string `json:"contract"`
    39  }
    40  
    41  func (c *Client) FetchCurrencies(ctx context.Context) ([]CryptoCurrency, error) {
    42  	response, err := c.httpClient.DoGetRequest(ctx, currenciesURL, nil, nil)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	return handleCurrenciesResponse(response)
    48  }
    49  
    50  func handleCurrenciesResponse(response []byte) ([]CryptoCurrency, error) {
    51  	var currenciesResponse CurrenciesResponse
    52  	err := json.Unmarshal(response, &currenciesResponse)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	if currenciesResponse.Status != http.StatusOK {
    58  		return nil, fmt.Errorf("unsuccessful request: %d %s", currenciesResponse.Status, http.StatusText(currenciesResponse.Status))
    59  	}
    60  
    61  	assets := currenciesResponse.Data.Config.CryptoCurrencies
    62  
    63  	return assets, nil
    64  }