dart_pkpass/lib/pkpass_web_wervice/web_service.dart
The one with the braid 6e7f19a764 feat: implement web service
Signed-off-by: The one with the braid <info@braid.business>
2023-11-26 19:42:29 +01:00

69 lines
2.1 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:typed_data';
import 'package:http/http.dart';
import 'package:pkpass/pkpass.dart';
import 'package:pkpass/pkpass_web_wervice/utils/http_date.dart';
import 'web_service_error.dart';
/// PassKit WebService implementation
///
/// A Representational State Transfer (REST)style web service protocol is used
/// to communicate with your server about changes to passes, and to fetch the
/// latest version of a pass when it has changed.
///
/// https://developer.apple.com/library/archive/documentation/PassKit/Reference/PassKit_WebService/WebService.html#//apple_ref/doc/uid/TP40011988
class PkPassWebService {
static const _apiVersion = 'v1';
static Client? _client;
/// The [PassMetadata] to check for updates.
final PassMetadata metadata;
/// An optional [Client] used for any http requests
final Client? client;
const PkPassWebService(this.metadata, {this.client});
PassWebService get webService {
final service = metadata.webService;
if (service == null) noWebServiceProvided();
return service;
}
Client get httpClient {
final client = this.client;
if (client != null) return client;
return _client ??= Client();
}
/// Getting the Latest Version of a Pass
///
/// Requests the latest version of the current PkPass file
///
/// [modifiedSince] should be provided in order to support "304 Not Modified"
Future<Uint8List?> getLatestVersion([DateTime? modifiedSince]) async {
final identifier = metadata.passTypeIdentifier;
final serial = metadata.serialNumber;
final endpoint = '/$_apiVersion/passes/$identifier/$serial';
final response = await httpClient.get(
Uri.parse(webService.webServiceURL.toString() + endpoint),
headers: {
if (modifiedSince != null)
'If-Modified-Since': HttpDate.format(modifiedSince),
'Authorization': 'ApplePass ${webService.authenticationToken}',
},
);
switch (response.statusCode) {
case 200:
return response.bodyBytes;
case 304:
return null;
default:
throw WebServiceResponseError(response);
}
}
Never noWebServiceProvided() => throw WebServiceUnavailable();
}