From c6987d2cc24d02f344703df4e56391c5569040ad Mon Sep 17 00:00:00 2001 From: Valimp Date: Tue, 29 Oct 2024 13:59:15 +0100 Subject: [PATCH 1/6] feat: get tickets for nutripatrol --- lib/src/nutripatrol/get_ticket.dart | 46 +++++++++++++++++++++ lib/src/nutripatrol/get_ticket.g.dart | 28 +++++++++++++ lib/src/nutripatrol_api_client.dart | 59 +++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 lib/src/nutripatrol/get_ticket.dart create mode 100644 lib/src/nutripatrol/get_ticket.g.dart create mode 100644 lib/src/nutripatrol_api_client.dart diff --git a/lib/src/nutripatrol/get_ticket.dart b/lib/src/nutripatrol/get_ticket.dart new file mode 100644 index 000000000..afbf073e8 --- /dev/null +++ b/lib/src/nutripatrol/get_ticket.dart @@ -0,0 +1,46 @@ +import 'package:json_annotation/json_annotation.dart'; + +import '../interface/json_object.dart'; + +part 'get_ticket.g.dart'; + +@JsonSerializable() +class Ticket extends JsonObject { + /// Flag ID. Read-only. + @JsonKey() + late int id; + + /// Barcode of the product. Read-only. + @JsonKey() + late String barcode; + + /// Type of the ticket. + @JsonKey() + late String type; + + /// Url of the ticket. Read-only. + @JsonKey() + String? url; + + /// Status of the ticket. + @JsonKey() + late String status; + + /// Image id of the ticket. Read-only. + @JsonKey(name: 'image_id') + String? imageId; + + /// Flavor of the ticket. + @JsonKey() + late String flavor; + + /// created date of the ticket. Read-only. + @JsonKey(name: 'created_at') + late String createdAt; + + Ticket(); + + factory Ticket.fromJson(Map json) => _$TicketFromJson(json); + + @override Map toJson() => _$TicketToJson(this); +} \ No newline at end of file diff --git a/lib/src/nutripatrol/get_ticket.g.dart b/lib/src/nutripatrol/get_ticket.g.dart new file mode 100644 index 000000000..3bbc6458d --- /dev/null +++ b/lib/src/nutripatrol/get_ticket.g.dart @@ -0,0 +1,28 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'get_ticket.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +Ticket _$TicketFromJson(Map json) => Ticket() + ..id = (json['id'] as num).toInt() + ..barcode = json['barcode'] as String + ..type = json['type'] as String + ..url = json['url'] as String? + ..status = json['status'] as String + ..imageId = json['image_id'] as String? + ..flavor = json['flavor'] as String + ..createdAt = json['created_at'] as String; + +Map _$TicketToJson(Ticket instance) => { + 'id': instance.id, + 'barcode': instance.barcode, + 'type': instance.type, + 'url': instance.url, + 'status': instance.status, + 'image_id': instance.imageId, + 'flavor': instance.flavor, + 'created_at': instance.createdAt, + }; diff --git a/lib/src/nutripatrol_api_client.dart b/lib/src/nutripatrol_api_client.dart new file mode 100644 index 000000000..753ff30ae --- /dev/null +++ b/lib/src/nutripatrol_api_client.dart @@ -0,0 +1,59 @@ +import 'dart:convert'; + +import 'package:http/http.dart'; +import 'package:openfoodfacts/src/prices/maybe_error.dart'; +import 'utils/http_helper.dart'; +import 'utils/open_food_api_configuration.dart'; +import 'utils/uri_helper.dart'; + +import 'nutripatrol/get_ticket.dart'; + +/// Client calls of the Nutripatrol API. +/// +/// cf. [Nutripatrol](https://nutripatrol.openfoodfacts.org/api/docs) +class NutripatrolApiClient { + NutripatrolApiClient._(); + + /// Subdomain of the Nutripatrol API. + static const String _subdomain = 'nutripatrol'; + + /// Host of the Elastic Search API. + static String _getHost(final UriProductHelper uriHelper) => + uriHelper.getHost(_subdomain); + + static Uri getUri({ + required final String path, + final Map? queryParameters, + final UriProductHelper uriHelper = uriHelperFoodProd, + final bool? addUserAgentParameters, + }) => + uriHelper.getUri( + path: path, + queryParameters: queryParameters, + forcedHost: _getHost(uriHelper), + addUserAgentParameters: addUserAgentParameters, + ); + + static Future> getTicket({ + required final int ticketId, + final UriProductHelper uriHelper = uriHelperFoodProd, + }) async { + final Uri uri = getUri( + path: '/api/v1/tickets/$ticketId', + uriHelper: uriHelper, + ); + final Response response = await HttpHelper().doGetRequest( + uri, + uriHelper: uriHelper, + ); + if (response.statusCode == 200) { + try { + final dynamic decodedResponse = HttpHelper().jsonDecodeUtf8(response); + return MaybeError.value(Ticket.fromJson(decodedResponse)); + } catch (e) { + // + } + } + return MaybeError.responseError(response); + } +} \ No newline at end of file From 8aea7a1ea5f97e5b3bbaf4472a6ccc79f33a52eb Mon Sep 17 00:00:00 2001 From: Valimp Date: Tue, 29 Oct 2024 14:27:56 +0100 Subject: [PATCH 2/6] fix: get tickets should be a list --- lib/src/nutripatrol_api_client.dart | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/src/nutripatrol_api_client.dart b/lib/src/nutripatrol_api_client.dart index 753ff30ae..262253450 100644 --- a/lib/src/nutripatrol_api_client.dart +++ b/lib/src/nutripatrol_api_client.dart @@ -56,4 +56,30 @@ class NutripatrolApiClient { } return MaybeError.responseError(response); } + + static Future>> getTickets({ + required final String barcode, + final UriProductHelper uriHelper = uriHelperFoodProd, + }) async { + final Uri uri = getUri( + path: '/api/v1/tickets', + uriHelper: uriHelper, + ); + final Response response = await HttpHelper().doGetRequest( + uri, + uriHelper: uriHelper, + ); + if (response.statusCode == 200) { + try { + final dynamic decodedResponse = HttpHelper().jsonDecodeUtf8(response); + final List tickets = decodedResponse['tickets']; + return MaybeError>.value( + tickets.map((dynamic ticket) => Ticket.fromJson(ticket)).toList(), + ); + } catch (e) { + // + } + } + return MaybeError>.responseError(response); + } } \ No newline at end of file From 351f32613e5b0989fe2f6466a6c70e81b6999089 Mon Sep 17 00:00:00 2001 From: Valimp Date: Tue, 29 Oct 2024 16:25:35 +0100 Subject: [PATCH 3/6] fix: use enum to type datas --- lib/src/nutripatrol/get_ticket.dart | 47 ++++++++++++++++++++++++--- lib/src/nutripatrol/get_ticket.g.dart | 35 +++++++++++++++----- lib/src/nutripatrol_api_client.dart | 10 ++++-- 3 files changed, 77 insertions(+), 15 deletions(-) diff --git a/lib/src/nutripatrol/get_ticket.dart b/lib/src/nutripatrol/get_ticket.dart index afbf073e8..03bda4231 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 String type; + late Type type; /// Url of the ticket. Read-only. @JsonKey() @@ -24,7 +24,7 @@ class Ticket extends JsonObject { /// Status of the ticket. @JsonKey() - late String status; + late Status status; /// Image id of the ticket. Read-only. @JsonKey(name: 'image_id') @@ -32,15 +32,54 @@ class Ticket extends JsonObject { /// Flavor of the ticket. @JsonKey() - late String flavor; + late Flavor flavor; /// created date of the ticket. Read-only. @JsonKey(name: 'created_at') - late String createdAt; + late String CreatedAt; Ticket(); factory Ticket.fromJson(Map json) => _$TicketFromJson(json); @override Map toJson() => _$TicketToJson(this); +} + +/// Enum for ticket type +enum Type { + @JsonValue('image') + image, + + @JsonValue('product') + product, + + @JsonValue('search') + search +} + +/// Enum for ticket status +enum Status { + @JsonValue('open') + open, + + @JsonValue('closed') + closed +} + +/// Enum for ticket flavor +enum Flavor { + @JsonValue('off') + off, + + @JsonValue('obf') + obf, + + @JsonValue('opff') + opff, + + @JsonValue('opf') + opf, + + @JsonValue('off-pro') + offPro } \ No newline at end of file diff --git a/lib/src/nutripatrol/get_ticket.g.dart b/lib/src/nutripatrol/get_ticket.g.dart index 3bbc6458d..fc73f58e3 100644 --- a/lib/src/nutripatrol/get_ticket.g.dart +++ b/lib/src/nutripatrol/get_ticket.g.dart @@ -9,20 +9,39 @@ part of 'get_ticket.dart'; Ticket _$TicketFromJson(Map json) => Ticket() ..id = (json['id'] as num).toInt() ..barcode = json['barcode'] as String - ..type = json['type'] as String + ..type = $enumDecode(_$TypeEnumMap, json['type']) ..url = json['url'] as String? - ..status = json['status'] as String + ..status = $enumDecode(_$StatusEnumMap, json['status']) ..imageId = json['image_id'] as String? - ..flavor = json['flavor'] as String - ..createdAt = json['created_at'] as String; + ..flavor = $enumDecode(_$FlavorEnumMap, json['flavor']) + ..CreatedAt = json['created_at'] as String; Map _$TicketToJson(Ticket instance) => { 'id': instance.id, 'barcode': instance.barcode, - 'type': instance.type, + 'type': _$TypeEnumMap[instance.type]!, 'url': instance.url, - 'status': instance.status, + 'status': _$StatusEnumMap[instance.status]!, 'image_id': instance.imageId, - 'flavor': instance.flavor, - 'created_at': instance.createdAt, + 'flavor': _$FlavorEnumMap[instance.flavor]!, + 'created_at': instance.CreatedAt, }; + +const _$TypeEnumMap = { + Type.image: 'image', + Type.product: 'product', + Type.search: 'search', +}; + +const _$StatusEnumMap = { + Status.open: 'open', + Status.closed: 'closed', +}; + +const _$FlavorEnumMap = { + Flavor.off: 'off', + Flavor.obf: 'obf', + Flavor.opff: 'opff', + Flavor.opf: 'opf', + Flavor.offPro: 'off-pro', +}; diff --git a/lib/src/nutripatrol_api_client.dart b/lib/src/nutripatrol_api_client.dart index 262253450..3b9de513a 100644 --- a/lib/src/nutripatrol_api_client.dart +++ b/lib/src/nutripatrol_api_client.dart @@ -17,7 +17,6 @@ class NutripatrolApiClient { /// Subdomain of the Nutripatrol API. static const String _subdomain = 'nutripatrol'; - /// Host of the Elastic Search API. static String _getHost(final UriProductHelper uriHelper) => uriHelper.getHost(_subdomain); @@ -34,10 +33,14 @@ class NutripatrolApiClient { addUserAgentParameters: addUserAgentParameters, ); + /// Get a ticket by its ID. + /// + /// [ticketId] is the ID of the ticket. static Future> getTicket({ required final int ticketId, final UriProductHelper uriHelper = uriHelperFoodProd, }) async { + assert(ticketId >= 0, "The id must be >= 0"); final Uri uri = getUri( path: '/api/v1/tickets/$ticketId', uriHelper: uriHelper, @@ -50,13 +53,14 @@ class NutripatrolApiClient { try { final dynamic decodedResponse = HttpHelper().jsonDecodeUtf8(response); return MaybeError.value(Ticket.fromJson(decodedResponse)); - } catch (e) { + } catch (_) { // } } return MaybeError.responseError(response); } + /// Get all tickets. static Future>> getTickets({ required final String barcode, final UriProductHelper uriHelper = uriHelperFoodProd, @@ -76,7 +80,7 @@ class NutripatrolApiClient { return MaybeError>.value( tickets.map((dynamic ticket) => Ticket.fromJson(ticket)).toList(), ); - } catch (e) { + } catch (_) { // } } From ff091e2f924cbdb9befe821f9ba88d7bdbcf7826 Mon Sep 17 00:00:00 2001 From: Valimp Date: Thu, 31 Oct 2024 09:39:17 +0100 Subject: [PATCH 4/6] feat: create flag type --- lib/src/nutripatrol/create_flag.dart | 98 ++++++++++++++++++++++++++ lib/src/nutripatrol/create_flag.g.dart | 40 +++++++++++ lib/src/nutripatrol/get_ticket.dart | 4 +- 3 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 lib/src/nutripatrol/create_flag.dart create mode 100644 lib/src/nutripatrol/create_flag.g.dart diff --git a/lib/src/nutripatrol/create_flag.dart b/lib/src/nutripatrol/create_flag.dart new file mode 100644 index 000000000..8296214e6 --- /dev/null +++ b/lib/src/nutripatrol/create_flag.dart @@ -0,0 +1,98 @@ +import 'package:json_annotation/json_annotation.dart'; + +import '../interface/json_object.dart'; + +part 'create_flag.g.dart'; + +@JsonSerializable() +class Flag extends JsonObject { + @JsonKey() + late String id; + + @JsonKey() + String? barcode; + + @JsonKey() + late Type type; + + @JsonKey() + String? url; + + @JsonKey(name: 'user_id') + late String userId; + + @JsonKey() + late Source source; + + @JsonKey() + double? confidence; + + @JsonKey(name: 'image_id') + String? imageId; + + @JsonKey() + late Flavor flavor; + + @JsonKey() + String? reason; + + @JsonKey() + String? comment; + + @JsonKey(name: 'created_at') + late DateTime CreatedAt; + + @JsonKey(name: 'ticket_id') + late int ticketId; + + @JsonKey(name: 'device_id') + late String deviceId; + + Flag(); + + factory Flag.fromJson(Map json) => _$FlagFromJson(json); + + @override Map toJson() => _$FlagToJson(this); +} + +/// Enum for ticket type +enum Type { + @JsonValue('image') + image, + + @JsonValue('product') + product, + + @JsonValue('search') + search +} + +/// Enum for ticket type +enum Source { + @JsonValue('mobile') + mobile, + + @JsonValue('web') + web, + + @JsonValue('robotoff') + robotoff +} + +/// Enum for ticket flavor +enum Flavor { + @JsonValue('off') + off, + + @JsonValue('obf') + obf, + + @JsonValue('opff') + opff, + + @JsonValue('opf') + opf, + + @JsonValue('off-pro') + offPro +} \ No newline at end of file diff --git a/lib/src/nutripatrol/create_flag.g.dart b/lib/src/nutripatrol/create_flag.g.dart new file mode 100644 index 000000000..aef801698 --- /dev/null +++ b/lib/src/nutripatrol/create_flag.g.dart @@ -0,0 +1,40 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'create_flag.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +Flag _$FlagFromJson(Map json) => Flag() + ..id = json['id'] as String + ..ticket = json['ticket'] as String + ..barcode = json['barcode'] as String + ..type = json['type'] as String + ..url = json['url'] as String? + ..userId = json['user_id'] as String + ..deviceId = json['device_id'] as String + ..source = json['source'] as String + ..confidence = (json['confidence'] as num?)?.toDouble() + ..imageId = json['image_id'] as String? + ..flavor = json['flavor'] as String + ..reason = json['reason'] as String? + ..comment = json['comment'] as String? + ..createdAt = DateTime.parse(json['created_at'] as String); + +Map _$FlagToJson(Flag instance) => { + 'id': instance.id, + 'ticket': instance.ticket, + 'barcode': instance.barcode, + 'type': instance.type, + 'url': instance.url, + 'user_id': instance.userId, + 'device_id': instance.deviceId, + 'source': instance.source, + 'confidence': instance.confidence, + 'image_id': instance.imageId, + 'flavor': instance.flavor, + 'reason': instance.reason, + 'comment': instance.comment, + 'created_at': instance.createdAt.toIso8601String(), + }; diff --git a/lib/src/nutripatrol/get_ticket.dart b/lib/src/nutripatrol/get_ticket.dart index 03bda4231..f59900c04 100644 --- a/lib/src/nutripatrol/get_ticket.dart +++ b/lib/src/nutripatrol/get_ticket.dart @@ -12,7 +12,7 @@ class Ticket extends JsonObject { /// Barcode of the product. Read-only. @JsonKey() - late String barcode; + String? barcode; /// Type of the ticket. @JsonKey() @@ -20,7 +20,7 @@ class Ticket extends JsonObject { /// Url of the ticket. Read-only. @JsonKey() - String? url; + late String url; /// Status of the ticket. @JsonKey() From 0ec4771b56a9dbed1f635e8cb65af1d2dd893af3 Mon Sep 17 00:00:00 2001 From: Valimp Date: Thu, 31 Oct 2024 13:53:45 +0100 Subject: [PATCH 5/6] fix: add generated files --- lib/src/nutripatrol/create_flag.g.dart | 46 ++++++++++++++++++-------- lib/src/nutripatrol/get_ticket.g.dart | 4 +-- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/lib/src/nutripatrol/create_flag.g.dart b/lib/src/nutripatrol/create_flag.g.dart index aef801698..8b87f2f17 100644 --- a/lib/src/nutripatrol/create_flag.g.dart +++ b/lib/src/nutripatrol/create_flag.g.dart @@ -8,33 +8,53 @@ part of 'create_flag.dart'; Flag _$FlagFromJson(Map json) => Flag() ..id = json['id'] as String - ..ticket = json['ticket'] as String - ..barcode = json['barcode'] as String - ..type = json['type'] as String + ..barcode = json['barcode'] as String? + ..type = $enumDecode(_$TypeEnumMap, json['type']) ..url = json['url'] as String? ..userId = json['user_id'] as String - ..deviceId = json['device_id'] as String - ..source = json['source'] as String + ..source = $enumDecode(_$SourceEnumMap, json['source']) ..confidence = (json['confidence'] as num?)?.toDouble() ..imageId = json['image_id'] as String? - ..flavor = json['flavor'] as String + ..flavor = $enumDecode(_$FlavorEnumMap, json['flavor']) ..reason = json['reason'] as String? ..comment = json['comment'] as String? - ..createdAt = DateTime.parse(json['created_at'] as String); + ..CreatedAt = DateTime.parse(json['created_at'] as String) + ..ticketId = (json['ticket_id'] as num).toInt() + ..deviceId = json['device_id'] as String; Map _$FlagToJson(Flag instance) => { 'id': instance.id, - 'ticket': instance.ticket, 'barcode': instance.barcode, - 'type': instance.type, + 'type': _$TypeEnumMap[instance.type]!, 'url': instance.url, 'user_id': instance.userId, - 'device_id': instance.deviceId, - 'source': instance.source, + 'source': _$SourceEnumMap[instance.source]!, 'confidence': instance.confidence, 'image_id': instance.imageId, - 'flavor': instance.flavor, + 'flavor': _$FlavorEnumMap[instance.flavor]!, 'reason': instance.reason, 'comment': instance.comment, - 'created_at': instance.createdAt.toIso8601String(), + 'created_at': instance.CreatedAt.toIso8601String(), + 'ticket_id': instance.ticketId, + 'device_id': instance.deviceId, }; + +const _$TypeEnumMap = { + Type.image: 'image', + Type.product: 'product', + Type.search: 'search', +}; + +const _$SourceEnumMap = { + Source.mobile: 'mobile', + Source.web: 'web', + Source.robotoff: 'robotoff', +}; + +const _$FlavorEnumMap = { + Flavor.off: 'off', + Flavor.obf: 'obf', + Flavor.opff: 'opff', + Flavor.opf: 'opf', + Flavor.offPro: 'off-pro', +}; diff --git a/lib/src/nutripatrol/get_ticket.g.dart b/lib/src/nutripatrol/get_ticket.g.dart index fc73f58e3..1a40e14b8 100644 --- a/lib/src/nutripatrol/get_ticket.g.dart +++ b/lib/src/nutripatrol/get_ticket.g.dart @@ -8,9 +8,9 @@ part of 'get_ticket.dart'; Ticket _$TicketFromJson(Map json) => Ticket() ..id = (json['id'] as num).toInt() - ..barcode = json['barcode'] as String + ..barcode = json['barcode'] as String? ..type = $enumDecode(_$TypeEnumMap, json['type']) - ..url = json['url'] as String? + ..url = json['url'] as String ..status = $enumDecode(_$StatusEnumMap, json['status']) ..imageId = json['image_id'] as String? ..flavor = $enumDecode(_$FlavorEnumMap, json['flavor']) From 8eb01e1e664b69649460c5b1c3bd82eaf4accb07 Mon Sep 17 00:00:00 2001 From: Valimp Date: Wed, 13 Nov 2024 11:04:00 +0100 Subject: [PATCH 6/6] feat: add getTicket and getTickets in the sdk --- example/lib/main.dart | 24 +++++++++++++++++++ lib/openfoodfacts.dart | 1 + lib/src/nutripatrol/get_ticket.dart | 8 +++---- lib/src/nutripatrol/get_ticket.g.dart | 22 +++++++++--------- lib/src/nutripatrol/get_tickets.dart | 23 ++++++++++++++++++ lib/src/nutripatrol/get_tickets.g.dart | 18 +++++++++++++++ lib/src/nutripatrol_api_client.dart | 32 ++++++++++++++++++-------- 7 files changed, 103 insertions(+), 25 deletions(-) create mode 100644 lib/src/nutripatrol/get_tickets.dart create mode 100644 lib/src/nutripatrol/get_tickets.g.dart diff --git a/example/lib/main.dart b/example/lib/main.dart index fcf429fdc..4b6418bc8 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 83b5e46a5..62a40a2e1 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 f59900c04..747e943a8 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 1a40e14b8..079d7e827 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 000000000..214fb4176 --- /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 000000000..b65e9c90b --- /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 3b9de513a..5e75c6bd1 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 +}