Skip to content

Commit adec32d

Browse files
committed
poc: freshworks jwt api
1 parent 909a787 commit adec32d

File tree

4 files changed

+205
-2
lines changed

4 files changed

+205
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// ignore_for_file: prefer_single_quotes
2+
3+
import 'package:flutter_deriv_api/api/exceptions/exceptions.dart';
4+
import 'package:flutter_deriv_api/basic_api/generated/freshchat_auth_jwt_receive.dart';
5+
import 'package:flutter_deriv_api/basic_api/generated/freshchat_auth_jwt_send.dart';
6+
import 'package:flutter_deriv_api/basic_api/generated/service_token_receive.dart';
7+
import 'package:flutter_deriv_api/helpers/helpers.dart';
8+
import 'package:flutter_deriv_api/services/connection/api_manager/base_api.dart';
9+
import 'package:deriv_dependency_injector/dependency_injector.dart';
10+
11+
/// Freshworks Auth JWT response model class.
12+
abstract class FreshChatJwtResponseModel {
13+
/// Initializes FreshChat JWT response model class.
14+
const FreshChatJwtResponseModel({
15+
this.freshworksAuthJwt,
16+
});
17+
18+
/// The Freshworks Auth JWT.
19+
final FreshworksAuthJwt? freshworksAuthJwt;
20+
}
21+
22+
/// Freshworks Auth JWT.
23+
class FreshworksAuthJwt {
24+
/// Initializes Freshworks Auth JWT.
25+
const FreshworksAuthJwt({
26+
required this.token,
27+
});
28+
29+
/// Creates an instance from JSON.
30+
factory FreshworksAuthJwt.fromJson(Map<String, dynamic> json) =>
31+
FreshworksAuthJwt(
32+
token: json['token'],
33+
);
34+
35+
/// The token itself.
36+
final String token;
37+
38+
/// Converts an instance to JSON.
39+
Map<String, dynamic> toJson() => <String, String>{
40+
'token': token,
41+
};
42+
}
43+
44+
/// Freshworks Auth JWT response class.
45+
class FreshChatJwtResponse extends FreshChatJwtResponseModel {
46+
/// Initializes FreshChat JWT response class.
47+
const FreshChatJwtResponse({
48+
required FreshworksAuthJwt freshworksAuthJwt,
49+
}) : super(freshworksAuthJwt: freshworksAuthJwt);
50+
51+
/// Creates an instance from JSON.
52+
factory FreshChatJwtResponse.fromJson(Map<String, dynamic> json) =>
53+
FreshChatJwtResponse(
54+
freshworksAuthJwt:
55+
FreshworksAuthJwt.fromJson(json['freshworks_auth_jwt']),
56+
);
57+
58+
/// Converts an instance to JSON.
59+
Map<String, dynamic> toJson() => <String, dynamic>{
60+
'service_token': <String, Map<String, dynamic>?>{
61+
'freshworks_auth_jwt': freshworksAuthJwt?.toJson(),
62+
},
63+
};
64+
65+
static final BaseAPI _api = Injector()<BaseAPI>();
66+
67+
/// Calls the Freshworks JWT service.
68+
///
69+
/// Throws a [BaseAPIException] if the API response contains an error.
70+
static Future<FreshChatJwtResponse> fetchFreshworksJwt(
71+
FreshworksAuthJwtRequest
72+
request, // Replace with your actual request type if available
73+
) async {
74+
final ServiceTokenReceive response = await _api.call(
75+
request:
76+
request); // You may need to adjust this line based on your actual API call
77+
78+
checkException(
79+
response: response,
80+
exceptionCreator: ({BaseExceptionModel? baseExceptionModel}) =>
81+
BaseAPIException(baseExceptionModel: baseExceptionModel),
82+
);
83+
84+
return FreshChatJwtResponse.fromJson(
85+
response.serviceToken!); // Adjust based on your API response
86+
}
87+
88+
/// Creates a copy of instance with given parameters.
89+
FreshChatJwtResponse copyWith({
90+
FreshworksAuthJwt? freshworksAuthJwt,
91+
}) =>
92+
FreshChatJwtResponse(
93+
freshworksAuthJwt: freshworksAuthJwt ?? this.freshworksAuthJwt!,
94+
);
95+
}

lib/api/response/p2p_advert_create_response_result.dart

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// ignore_for_file: prefer_single_quotes, unnecessary_import, unused_import
22

33
import 'package:equatable/equatable.dart';
4+
import 'package:flutter_deriv_api/api/exceptions/base_api_exception.dart';
45

5-
import 'package:flutter_deriv_api/api/exceptions/p2p_advert_exception.dart';
66
import 'package:flutter_deriv_api/api/models/base_exception_model.dart';
77
import 'package:flutter_deriv_api/basic_api/generated/p2p_advert_create_receive.dart';
88
import 'package:flutter_deriv_api/basic_api/generated/p2p_advert_create_send.dart';
@@ -239,6 +239,7 @@ enum VisibilityStatusItemEnum {
239239
/// advertiser_temp_ban.
240240
advertiserTempBan,
241241
}
242+
242243
/// P2p advert create model class.
243244
abstract class P2pAdvertCreateModel {
244245
/// Initializes P2p advert create model class .
@@ -742,6 +743,7 @@ class P2pAdvertCreate extends P2pAdvertCreateModel {
742743
visibilityStatus: visibilityStatus ?? this.visibilityStatus,
743744
);
744745
}
746+
745747
/// Advertiser details model class.
746748
abstract class AdvertiserDetailsModel {
747749
/// Initializes Advertiser details model class .
@@ -892,6 +894,7 @@ class AdvertiserDetails extends AdvertiserDetailsModel {
892894
totalCompletionRate: totalCompletionRate ?? this.totalCompletionRate,
893895
);
894896
}
897+
895898
/// Payment method details property model class.
896899
abstract class PaymentMethodDetailsPropertyModel {
897900
/// Initializes Payment method details property model class .
@@ -1020,6 +1023,7 @@ class PaymentMethodDetailsProperty extends PaymentMethodDetailsPropertyModel {
10201023
usedByOrders: usedByOrders ?? this.usedByOrders,
10211024
);
10221025
}
1026+
10231027
/// Fields property model class.
10241028
abstract class FieldsPropertyModel {
10251029
/// Initializes Fields property model class .

lib/api/response/p2p_advert_list_response_result.dart

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// ignore_for_file: prefer_single_quotes, unnecessary_import, unused_import
22

33
import 'package:equatable/equatable.dart';
4+
import 'package:flutter_deriv_api/api/exceptions/base_api_exception.dart';
45

5-
import 'package:flutter_deriv_api/api/exceptions/p2p_advert_exception.dart';
66
import 'package:flutter_deriv_api/api/models/base_exception_model.dart';
77
import 'package:flutter_deriv_api/basic_api/generated/p2p_advert_list_receive.dart';
88
import 'package:flutter_deriv_api/basic_api/generated/p2p_advert_list_send.dart';
@@ -226,6 +226,7 @@ enum VisibilityStatusItemEnum {
226226
/// advertiser_temp_ban.
227227
advertiserTempBan,
228228
}
229+
229230
/// P2p advert list model class.
230231
abstract class P2pAdvertListModel {
231232
/// Initializes P2p advert list model class .
@@ -274,6 +275,7 @@ class P2pAdvertList extends P2pAdvertListModel {
274275
list: list ?? this.list,
275276
);
276277
}
278+
277279
/// List item model class.
278280
abstract class ListItemModel {
279281
/// Initializes List item model class .
@@ -817,6 +819,7 @@ class ListItem extends ListItemModel {
817819
visibilityStatus: visibilityStatus ?? this.visibilityStatus,
818820
);
819821
}
822+
820823
/// Advertiser details model class.
821824
abstract class AdvertiserDetailsModel {
822825
/// Initializes Advertiser details model class .
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// ignore_for_file: always_put_required_named_parameters_first
2+
3+
import '../request.dart';
4+
5+
/// Freshworks Auth JWT request class.
6+
class FreshworksAuthJwtRequest extends Request {
7+
/// Initializes the Freshworks Auth JWT request class.
8+
const FreshworksAuthJwtRequest({
9+
required this.serviceToken,
10+
required this.referrer,
11+
required this.extraFields,
12+
this.service = 'freshworks_auth_jwt',
13+
super.msgType = 'freshworks_auth_jwt',
14+
super.passthrough,
15+
super.reqId,
16+
}); // Set service to the value of msgType
17+
18+
/// Creates an instance from JSON.
19+
factory FreshworksAuthJwtRequest.fromJson(Map<String, dynamic> json) =>
20+
FreshworksAuthJwtRequest(
21+
serviceToken: json['service_token'] as int,
22+
referrer: json['referrer'] as String,
23+
extraFields: ExtraFields.fromJson(json['extra_fields']),
24+
passthrough: json['passthrough'] as Map<String, dynamic>?,
25+
reqId: json['req_id'] as int?,
26+
);
27+
28+
/// The service for the request, same as msgType.
29+
final String service;
30+
31+
/// Service token for the request.
32+
final int serviceToken;
33+
34+
/// Referrer for the request.
35+
final String referrer;
36+
37+
/// Extra fields for the request.
38+
final ExtraFields extraFields;
39+
40+
/// Converts this instance to JSON.
41+
@override
42+
Map<String, dynamic> toJson() => <String, dynamic>{
43+
'service_token': serviceToken,
44+
'referrer': referrer,
45+
'extra_fields': extraFields.toJson(),
46+
'service': service, // Add service field to JSON
47+
'passthrough': passthrough,
48+
'req_id': reqId,
49+
};
50+
51+
/// Creates a copy of the instance with given parameters.
52+
@override
53+
FreshworksAuthJwtRequest copyWith({
54+
int? serviceToken,
55+
String? referrer,
56+
ExtraFields? extraFields,
57+
String? service,
58+
Map<String, dynamic>? passthrough,
59+
int? reqId,
60+
}) =>
61+
FreshworksAuthJwtRequest(
62+
serviceToken: serviceToken ?? this.serviceToken,
63+
referrer: referrer ?? this.referrer,
64+
extraFields: extraFields ?? this.extraFields,
65+
service: service ?? this.service, // Include service in copy
66+
passthrough: passthrough ?? this.passthrough,
67+
reqId: reqId ?? this.reqId,
68+
);
69+
70+
/// Override equatable class.
71+
@override
72+
List<Object?> get props => <Object?>[
73+
serviceToken,
74+
referrer,
75+
extraFields,
76+
service, // Include service in props for equality
77+
passthrough,
78+
reqId,
79+
];
80+
}
81+
82+
/// Extra fields for the Freshworks Auth JWT request.
83+
class ExtraFields {
84+
/// Initializes ExtraFields.
85+
const ExtraFields({
86+
required this.freshchatUuid,
87+
});
88+
89+
/// Creates an instance from JSON.
90+
factory ExtraFields.fromJson(Map<String, dynamic> json) => ExtraFields(
91+
freshchatUuid: json['freshchat_uuid'] as String,
92+
);
93+
94+
/// The Freshchat UUID.
95+
final String freshchatUuid;
96+
97+
/// Converts this instance to JSON.
98+
Map<String, dynamic> toJson() => <String, dynamic>{
99+
'freshchat_uuid': freshchatUuid,
100+
};
101+
}

0 commit comments

Comments
 (0)