getCategory method
Get category by ID
Implementation
Future<Category> getCategory(int categoryId) async {
try {
final response = await _client.guestRequest<Map<String, dynamic>>(
'/rest/V1/categories/$categoryId',
queryParameters: {
'fields':
'id,name,description,image,parent_id,level,position,is_active,children_count,children,attributes',
},
);
if (response.statusCode == 200) {
return Category.fromJson(response.data!);
} else {
throw Exception('Failed to get category: ${response.statusMessage}');
}
} on DioException catch (e) {
if (e.response?.statusCode == 404) {
throw Exception('Category not found: $categoryId');
}
throw Exception('Failed to get category: ${e.message}');
} catch (e) {
throw Exception('Failed to get category: $e');
}
}