diff --git a/example/lib/main.dart b/example/lib/main.dart index fcf429fdc8..4b6418bc80 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,6 +1,30 @@ import 'dart:async'; import 'package:openfoodfacts/openfoodfacts.dart'; +import 'package:json_annotation/src/json_value.dart'; +import 'package:openfoodfacts/src/nutripatrol/get_ticket.dart'; + +void main() { + OpenFoodAPIConfiguration.userAgent = UserAgent( + name: 'openfoodfacts-dart', + version: '1.0.0', + url: '', + ); + getTickets(); +} + +/// Get the ticket by its ID +/// The result will be a MaybeError that can be parsed +void getTicket() async { + await NutripatrolApiClient.getTicket(ticketId: 2); +} + +/// Get all tickets +/// The result will be a MaybeError that can be parsed +void getTickets() async { + await NutripatrolApiClient.getTickets( + status: TicketStatus.open, type_: TicketType.image, page: 1); +} /// request a product from the OpenFoodFacts database Future getProduct() async { diff --git a/lib/openfoodfacts.dart b/lib/openfoodfacts.dart index 83b5e46a51..62a40a2e1f 100644 --- a/lib/openfoodfacts.dart +++ b/lib/openfoodfacts.dart @@ -153,3 +153,4 @@ export 'src/utils/unit_helper.dart'; export 'src/utils/uri_helper.dart'; export 'src/utils/uri_reader.dart'; export 'src/robot_off_api_client.dart'; +export 'src/nutripatrol_api_client.dart'; \ No newline at end of file diff --git a/lib/src/nutripatrol/get_ticket.dart b/lib/src/nutripatrol/get_ticket.dart index f59900c041..747e943a88 100644 --- a/lib/src/nutripatrol/get_ticket.dart +++ b/lib/src/nutripatrol/get_ticket.dart @@ -16,7 +16,7 @@ class Ticket extends JsonObject { /// Type of the ticket. @JsonKey() - late Type type; + late TicketType type; /// Url of the ticket. Read-only. @JsonKey() @@ -24,7 +24,7 @@ class Ticket extends JsonObject { /// Status of the ticket. @JsonKey() - late Status status; + late TicketStatus status; /// Image id of the ticket. Read-only. @JsonKey(name: 'image_id') @@ -46,7 +46,7 @@ class Ticket extends JsonObject { } /// Enum for ticket type -enum Type { +enum TicketType { @JsonValue('image') image, @@ -58,7 +58,7 @@ enum Type { } /// Enum for ticket status -enum Status { +enum TicketStatus { @JsonValue('open') open, diff --git a/lib/src/nutripatrol/get_ticket.g.dart b/lib/src/nutripatrol/get_ticket.g.dart index 1a40e14b89..079d7e827e 100644 --- a/lib/src/nutripatrol/get_ticket.g.dart +++ b/lib/src/nutripatrol/get_ticket.g.dart @@ -9,9 +9,9 @@ part of 'get_ticket.dart'; Ticket _$TicketFromJson(Map json) => Ticket() ..id = (json['id'] as num).toInt() ..barcode = json['barcode'] as String? - ..type = $enumDecode(_$TypeEnumMap, json['type']) + ..type = $enumDecode(_$TicketTypeEnumMap, json['type']) ..url = json['url'] as String - ..status = $enumDecode(_$StatusEnumMap, json['status']) + ..status = $enumDecode(_$TicketStatusEnumMap, json['status']) ..imageId = json['image_id'] as String? ..flavor = $enumDecode(_$FlavorEnumMap, json['flavor']) ..CreatedAt = json['created_at'] as String; @@ -19,23 +19,23 @@ Ticket _$TicketFromJson(Map json) => Ticket() Map _$TicketToJson(Ticket instance) => { 'id': instance.id, 'barcode': instance.barcode, - 'type': _$TypeEnumMap[instance.type]!, + 'type': _$TicketTypeEnumMap[instance.type]!, 'url': instance.url, - 'status': _$StatusEnumMap[instance.status]!, + 'status': _$TicketStatusEnumMap[instance.status]!, 'image_id': instance.imageId, 'flavor': _$FlavorEnumMap[instance.flavor]!, 'created_at': instance.CreatedAt, }; -const _$TypeEnumMap = { - Type.image: 'image', - Type.product: 'product', - Type.search: 'search', +const _$TicketTypeEnumMap = { + TicketType.image: 'image', + TicketType.product: 'product', + TicketType.search: 'search', }; -const _$StatusEnumMap = { - Status.open: 'open', - Status.closed: 'closed', +const _$TicketStatusEnumMap = { + TicketStatus.open: 'open', + TicketStatus.closed: 'closed', }; const _$FlavorEnumMap = { diff --git a/lib/src/nutripatrol/get_tickets.dart b/lib/src/nutripatrol/get_tickets.dart new file mode 100644 index 0000000000..214fb41766 --- /dev/null +++ b/lib/src/nutripatrol/get_tickets.dart @@ -0,0 +1,23 @@ +import 'package:json_annotation/json_annotation.dart'; +import 'get_ticket.dart'; + +import '../interface/json_object.dart'; + +part 'get_tickets.g.dart'; + +@JsonSerializable() +class Tickets extends JsonObject { + /// List of Tickets + @JsonKey() + late List tickets; + + /// Max Page + @JsonKey(name: 'max_page') + late int maxPage; + + Tickets(); + + factory Tickets.fromJson(Map json) => _$TicketsFromJson(json); + + @override Map toJson() => _$TicketsToJson(this); +} \ No newline at end of file diff --git a/lib/src/nutripatrol/get_tickets.g.dart b/lib/src/nutripatrol/get_tickets.g.dart new file mode 100644 index 0000000000..b65e9c90b3 --- /dev/null +++ b/lib/src/nutripatrol/get_tickets.g.dart @@ -0,0 +1,18 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'get_tickets.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +Tickets _$TicketsFromJson(Map json) => Tickets() + ..tickets = (json['tickets'] as List) + .map((e) => Ticket.fromJson(e as Map)) + .toList() + ..maxPage = (json['max_page'] as num).toInt(); + +Map _$TicketsToJson(Tickets instance) => { + 'tickets': instance.tickets, + 'max_page': instance.maxPage, + }; diff --git a/lib/src/nutripatrol_api_client.dart b/lib/src/nutripatrol_api_client.dart index 3b9de513a1..5e75c6bd1a 100644 --- a/lib/src/nutripatrol_api_client.dart +++ b/lib/src/nutripatrol_api_client.dart @@ -1,6 +1,5 @@ -import 'dart:convert'; - import 'package:http/http.dart'; +import 'package:openfoodfacts/src/nutripatrol/get_tickets.dart'; import 'package:openfoodfacts/src/prices/maybe_error.dart'; import 'utils/http_helper.dart'; import 'utils/open_food_api_configuration.dart'; @@ -16,7 +15,7 @@ class NutripatrolApiClient { /// Subdomain of the Nutripatrol API. static const String _subdomain = 'nutripatrol'; - + static String _getHost(final UriProductHelper uriHelper) => uriHelper.getHost(_subdomain); @@ -34,7 +33,7 @@ class NutripatrolApiClient { ); /// Get a ticket by its ID. - /// + /// /// [ticketId] is the ID of the ticket. static Future> getTicket({ required final int ticketId, @@ -61,12 +60,22 @@ class NutripatrolApiClient { } /// Get all tickets. - static Future>> getTickets({ - required final String barcode, + static Future> getTickets({ + final TicketStatus status = TicketStatus.open, + final TicketType type_ = TicketType.image, + final int? page, + final int? pageSize, final UriProductHelper uriHelper = uriHelperFoodProd, }) async { + final Map queryParameters = {}; + queryParameters['status'] = status.toString().split('.').last; + queryParameters['type'] = type_.toString().split('.').last; + if (page != null) queryParameters['page'] = page.toString(); + if (pageSize != null) queryParameters['page_size'] = pageSize.toString(); + final Uri uri = getUri( path: '/api/v1/tickets', + queryParameters: queryParameters, uriHelper: uriHelper, ); final Response response = await HttpHelper().doGetRequest( @@ -77,13 +86,16 @@ class NutripatrolApiClient { try { final dynamic decodedResponse = HttpHelper().jsonDecodeUtf8(response); final List tickets = decodedResponse['tickets']; - return MaybeError>.value( - tickets.map((dynamic ticket) => Ticket.fromJson(ticket)).toList(), + return MaybeError.value( + Tickets.fromJson({ + 'tickets': tickets, + 'max_page': decodedResponse['max_page'], + }), ); } catch (_) { // } } - return MaybeError>.responseError(response); + return MaybeError.responseError(response); } -} \ No newline at end of file +}