Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

poc: freshworks jwt api #340

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions lib/api/response/freshchat_jwt_response_result.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// ignore_for_file: prefer_single_quotes

import 'package:flutter_deriv_api/api/exceptions/exceptions.dart';
import 'package:flutter_deriv_api/basic_api/generated/freshchat_user_jwt_send.dart';
import 'package:flutter_deriv_api/basic_api/generated/service_token_receive.dart';
import 'package:flutter_deriv_api/helpers/helpers.dart';
import 'package:flutter_deriv_api/services/connection/api_manager/base_api.dart';
import 'package:deriv_dependency_injector/dependency_injector.dart';

/// Freshworks User JWT response model class.
abstract class FreshChatJwtResponseModel {
/// Initializes FreshChat JWT response model class.
const FreshChatJwtResponseModel({
this.freshworksUserJwt,
});

/// The Freshworks User JWT.
final FreshworksUserJwt? freshworksUserJwt;
}

/// Freshworks User JWT.
class FreshworksUserJwt {
/// Initializes Freshworks User JWT.
const FreshworksUserJwt({
required this.token,
});

/// Creates an instance from JSON.
factory FreshworksUserJwt.fromJson(Map<String, dynamic> json) =>
FreshworksUserJwt(
token: json['token'],
);

/// The token itself.
final String token;

/// Converts an instance to JSON.
Map<String, dynamic> toJson() => <String, String>{
'token': token,
};
}

/// Freshworks User JWT response class.
class FreshChatJwtResponse extends FreshChatJwtResponseModel {
/// Initializes FreshChat JWT response class.
const FreshChatJwtResponse({
required FreshworksUserJwt freshworksUserJwt,
}) : super(freshworksUserJwt: freshworksUserJwt);

/// Creates an instance from JSON.
factory FreshChatJwtResponse.fromJson(Map<String, dynamic> json) =>
FreshChatJwtResponse(
freshworksUserJwt:
FreshworksUserJwt.fromJson(json['freshworks_user_jwt']),
);

/// Converts an instance to JSON.
Map<String, dynamic> toJson() => <String, dynamic>{
'service_token': <String, Map<String, dynamic>?>{
'freshworks_user_jwt': freshworksUserJwt?.toJson(),
},
};

static final BaseAPI _api = Injector()<BaseAPI>();

/// Calls the Freshworks JWT service.
///
/// Throws a [BaseAPIException] if the API response contains an error.
static Future<FreshChatJwtResponse> fetchFreshworksJwt(
FreshworksUserJwtRequest
request, // Replace with your actual request type if available
) async {
final ServiceTokenReceive response = await _api.call(
request:
request); // You may need to adjust this line based on your actual API call

checkException(
response: response,
exceptionCreator: ({BaseExceptionModel? baseExceptionModel}) =>
BaseAPIException(baseExceptionModel: baseExceptionModel),
);

return FreshChatJwtResponse.fromJson(
response.serviceToken!); // Adjust based on your API response
}

/// Creates a copy of instance with given parameters.
FreshChatJwtResponse copyWith({
FreshworksUserJwt? freshworksUserJwt,
}) =>
FreshChatJwtResponse(
freshworksUserJwt: freshworksUserJwt ?? this.freshworksUserJwt!,
);
}
110 changes: 110 additions & 0 deletions lib/basic_api/generated/freshchat_user_jwt_send.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// ignore_for_file: always_put_required_named_parameters_first

import '../request.dart';

/// Freshworks Auth JWT request class.
class FreshworksUserJwtRequest extends Request {
/// Initializes the Freshworks Auth JWT request class.
const FreshworksUserJwtRequest({
required this.serviceToken,
required this.referrer,
required this.extraFields,
required this.loginId,
this.service = 'freshworks_user_jwt',
super.msgType = 'freshworks_user_jwt',
super.passthrough,
super.reqId,
}); // Set service to the value of msgType

/// Creates an instance from JSON.
factory FreshworksUserJwtRequest.fromJson(Map<String, dynamic> json) =>
FreshworksUserJwtRequest(
serviceToken: json['service_token'] as int,
referrer: json['referrer'] as String,
extraFields: ExtraFields.fromJson(json['extra_fields']),
passthrough: json['passthrough'] as Map<String, dynamic>?,
reqId: json['req_id'] as int?,
loginId: json['loginid'] as String,
);

/// The service for the request, same as msgType.
final String service;

/// Service token for the request.
final int serviceToken;

/// Referrer for the request.
final String referrer;

/// Extra fields for the request.
final ExtraFields extraFields;

/// Login ID for the request.
final String loginId;

/// Converts this instance to JSON.
@override
Map<String, dynamic> toJson() => <String, dynamic>{
'service_token': serviceToken,
'loginid': loginId,
'referrer': referrer,
'extra_fields': extraFields.toJson(),
'service': service, // Add service field to JSON
'passthrough': passthrough,
'req_id': reqId,
};

/// Creates a copy of the instance with given parameters.
@override
FreshworksUserJwtRequest copyWith({
int? serviceToken,
String? referrer,
ExtraFields? extraFields,
String? service,
Map<String, dynamic>? passthrough,
int? reqId,
String? loginId,
}) =>
FreshworksUserJwtRequest(
serviceToken: serviceToken ?? this.serviceToken,
referrer: referrer ?? this.referrer,
extraFields: extraFields ?? this.extraFields,
service: service ?? this.service, // Include service in copy
passthrough: passthrough ?? this.passthrough,
reqId: reqId ?? this.reqId,
loginId: loginId ?? this.loginId,
);

/// Override equatable class.
@override
List<Object?> get props => <Object?>[
serviceToken,
referrer,
extraFields,
service, // Include service in props for equality
passthrough,
reqId,
loginId,
];
}

/// Extra fields for the Freshworks Auth JWT request.
class ExtraFields {
/// Initializes ExtraFields.
const ExtraFields({
required this.freshchatUuid,
});

/// Creates an instance from JSON.
factory ExtraFields.fromJson(Map<String, dynamic> json) => ExtraFields(
freshchatUuid: json['freshchat_uuid'] as String,
);

/// The Freshchat UUID.
final String freshchatUuid;

/// Converts this instance to JSON.
Map<String, dynamic> toJson() => <String, dynamic>{
'freshchat_uuid': freshchatUuid,
};
}
Loading