Files
jinzhi/lib/features/mall/data/mall_auth_api.dart
T
2026-09-08 13:46:45 +08:00

56 lines
1.6 KiB
Dart

import 'dart:typed_data';
import 'package:dio/dio.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import '../../../common/network/dio_provider.dart';
import '../../ads/data/ads_api_client.dart';
class AppMallAuthToken {
const AppMallAuthToken({
required this.authToken,
required this.expiresIn,
required this.sourceApp,
required this.scene,
});
final String authToken;
final int expiresIn;
final String sourceApp;
final String scene;
}
class MallAuthApi {
const MallAuthApi(this._dio);
final Dio _dio;
Future<AppMallAuthToken> issueAppMallAuthToken() async {
final response = await _dio.post<List<int>>(
'/api/v1/auth/mall/app-token',
// Dio serializes a List<int> as the UTF-8 text "[]". This endpoint
// expects an empty protobuf message, whose wire representation is zero
// bytes; Uint8List keeps the request body binary and untransformed.
data: Uint8List(0),
options: protobufOptions(),
);
final reply = ProtoMessage(response.data ?? const <int>[]);
final token = reply.string(1).trim();
final expiresIn = reply.integer(2);
final sourceApp = reply.string(3).trim();
final scene = reply.string(4).trim();
if (token.isEmpty || expiresIn <= 0 || sourceApp.isEmpty || scene.isEmpty) {
throw const FormatException('Incomplete mall auth token response');
}
return AppMallAuthToken(
authToken: token,
expiresIn: expiresIn,
sourceApp: sourceApp,
scene: scene,
);
}
}
final mallAuthApiProvider = Provider(
(ref) => MallAuthApi(ref.watch(dioProvider)),
);