getProducts function

Future<List<Product>> getProducts({
  1. int? limit,
  2. int? offset,
  3. String? productType,
  4. List<String>? productIds,
  5. String? contractExpiryType,
  6. Client? client,
  7. bool isSandbox = false,
})

Gets a list of products.

GET /api/v3/brokerage/market/products https://docs.cdp.coinbase.com/api-reference/advanced-trade-api/rest-api/public/list-public-products

This function makes a GET request to the /products endpoint of the Coinbase Advanced Trade API.

limit - A limit for the number of products to be returned. offset - An optional offset for pagination. productType - An optional product type to filter by. productIds - An optional list of product IDs to filter by. contractExpiryType - An optional contract expiry type to filter by. isSandbox - Whether to use the sandbox environment.

Returns a list of Product objects.

Implementation

Future<List<Product>> getProducts(
    {int? limit,
    int? offset,
    String? productType,
    List<String>? productIds,
    String? contractExpiryType,
    http.Client? client,
    bool isSandbox = false}) async {
  List<Product> products = [];
  Map<String, String> queryParameters = {
    if (limit != null) 'limit': '$limit',
    if (offset != null) 'offset': '$offset',
    if (productType != null) 'product_type': productType,
    if (productIds != null) 'product_ids': productIds.join(','),
    if (contractExpiryType != null) 'contract_expiry_type': contractExpiryType,
  };

  http.Response response = await get('/market/products',
      queryParameters: queryParameters, client: client, isSandbox: isSandbox);

  if (response.statusCode == 200) {
    String data = response.body;
    var jsonResponse = jsonDecode(data);
    var jsonProducts = jsonResponse['products'];

    for (var jsonObject in jsonProducts) {
      products.add(Product.fromCBJson(jsonObject));
    }
  } else {
    throw CoinbaseException(
        'Failed to get products', response.statusCode, response.body);
  }

  return products;
}