getAttribute method

Future<WooProductAttribute> getAttribute(
  1. int id, {
  2. bool? useFaker,
})

Retrieve a single product attribute by id

Implementation

Future<WooProductAttribute> getAttribute(int id, {bool? useFaker}) async {
  final isUsingFaker = useFaker ?? this.useFaker;

  if (isUsingFaker) {
    return WooProductAttribute.fake();
  }

  try {
    final response = await dio.get(_AttributeEndpoints.singleAttribute(id));
    if (response.statusCode != null &&
        response.statusCode! >= 200 &&
        response.statusCode! < 300) {
      return WooProductAttribute.fromJson(response.data);
    } else {
      throw Exception(
        "API call failed with status code: ${response.statusCode}",
      );
    }
  } on DioException catch (e) {
    final errorMsg = e.response?.data["message"] ?? e.message;
    throw Exception("API call failed: $errorMsg");
  } catch (e) {
    throw Exception("Unexpected error in API call: $e");
  }
}