Skip to content

Commit

Permalink
feat: add getTicket and getTickets in the sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
Valimp committed Nov 13, 2024
1 parent 0ec4771 commit 8eb01e1
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 25 deletions.
24 changes: 24 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -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<Ticket> that can be parsed
void getTicket() async {
await NutripatrolApiClient.getTicket(ticketId: 2);
}

/// Get all tickets
/// The result will be a MaybeError<Tickets> 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<Product?> getProduct() async {
Expand Down
1 change: 1 addition & 0 deletions lib/openfoodfacts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
8 changes: 4 additions & 4 deletions lib/src/nutripatrol/get_ticket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class Ticket extends JsonObject {

/// Type of the ticket.
@JsonKey()
late Type type;
late TicketType type;

/// Url of the ticket. Read-only.
@JsonKey()
late String url;

/// Status of the ticket.
@JsonKey()
late Status status;
late TicketStatus status;

/// Image id of the ticket. Read-only.
@JsonKey(name: 'image_id')
Expand All @@ -46,7 +46,7 @@ class Ticket extends JsonObject {
}

/// Enum for ticket type
enum Type {
enum TicketType {
@JsonValue('image')
image,

Expand All @@ -58,7 +58,7 @@ enum Type {
}

/// Enum for ticket status
enum Status {
enum TicketStatus {
@JsonValue('open')
open,

Expand Down
22 changes: 11 additions & 11 deletions lib/src/nutripatrol/get_ticket.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions lib/src/nutripatrol/get_tickets.dart
Original file line number Diff line number Diff line change
@@ -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<Ticket> tickets;

/// Max Page
@JsonKey(name: 'max_page')
late int maxPage;

Tickets();

factory Tickets.fromJson(Map<String, dynamic> json) => _$TicketsFromJson(json);

@override Map<String, dynamic> toJson() => _$TicketsToJson(this);
}
18 changes: 18 additions & 0 deletions lib/src/nutripatrol/get_tickets.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 22 additions & 10 deletions lib/src/nutripatrol_api_client.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);

Expand All @@ -34,7 +33,7 @@ class NutripatrolApiClient {
);

/// Get a ticket by its ID.
///
///
/// [ticketId] is the ID of the ticket.
static Future<MaybeError<Ticket>> getTicket({
required final int ticketId,
Expand All @@ -61,12 +60,22 @@ class NutripatrolApiClient {
}

/// Get all tickets.
static Future<MaybeError<List<Ticket>>> getTickets({
required final String barcode,
static Future<MaybeError<Tickets>> getTickets({
final TicketStatus status = TicketStatus.open,
final TicketType type_ = TicketType.image,
final int? page,
final int? pageSize,
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Map<String, String> queryParameters = <String, String>{};
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(
Expand All @@ -77,13 +86,16 @@ class NutripatrolApiClient {
try {
final dynamic decodedResponse = HttpHelper().jsonDecodeUtf8(response);
final List<dynamic> tickets = decodedResponse['tickets'];
return MaybeError<List<Ticket>>.value(
tickets.map((dynamic ticket) => Ticket.fromJson(ticket)).toList(),
return MaybeError<Tickets>.value(
Tickets.fromJson(<String, dynamic>{
'tickets': tickets,
'max_page': decodedResponse['max_page'],
}),
);
} catch (_) {
//
}
}
return MaybeError<List<Ticket>>.responseError(response);
return MaybeError<Tickets>.responseError(response);
}
}
}

0 comments on commit 8eb01e1

Please sign in to comment.