Implement Milestone 7.2 offline audio downloads
This commit is contained in:
@@ -43,6 +43,11 @@ public protocol VelodyAPIClient: Sendable {
|
||||
deviceId: String
|
||||
) async throws -> RemoteLibraryResponseDTO
|
||||
|
||||
func downloadAudioAsset(
|
||||
assetId: String,
|
||||
deviceId: String
|
||||
) async throws -> Data
|
||||
|
||||
func prepareUpload(
|
||||
_ payload: UploadPrepareRequest
|
||||
) async throws -> UploadPrepareResponse
|
||||
@@ -123,6 +128,23 @@ public struct URLSessionVelodyAPIClient: VelodyAPIClient {
|
||||
)
|
||||
}
|
||||
|
||||
public func downloadAudioAsset(
|
||||
assetId: String,
|
||||
deviceId: String
|
||||
) async throws -> Data {
|
||||
let request = try buildRequest(
|
||||
method: "GET",
|
||||
pathComponents: ["api", "v1", "assets", assetId, "download"],
|
||||
queryItems: [
|
||||
URLQueryItem(name: "deviceId", value: deviceId),
|
||||
],
|
||||
bodyData: nil,
|
||||
acceptType: "audio/mpeg"
|
||||
)
|
||||
|
||||
return try await executeData(request)
|
||||
}
|
||||
|
||||
public func prepareUpload(
|
||||
_ payload: UploadPrepareRequest
|
||||
) async throws -> UploadPrepareResponse {
|
||||
@@ -236,7 +258,8 @@ public struct URLSessionVelodyAPIClient: VelodyAPIClient {
|
||||
pathComponents: [String],
|
||||
queryItems: [URLQueryItem],
|
||||
bodyData: Data?,
|
||||
contentType: String? = nil
|
||||
contentType: String? = nil,
|
||||
acceptType: String = "application/json"
|
||||
) throws -> URLRequest {
|
||||
guard let url = endpointURL(
|
||||
pathComponents: pathComponents,
|
||||
@@ -247,7 +270,7 @@ public struct URLSessionVelodyAPIClient: VelodyAPIClient {
|
||||
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = method
|
||||
request.setValue("application/json", forHTTPHeaderField: "Accept")
|
||||
request.setValue(acceptType, forHTTPHeaderField: "Accept")
|
||||
|
||||
if let bodyData {
|
||||
request.httpBody = bodyData
|
||||
@@ -280,6 +303,20 @@ public struct URLSessionVelodyAPIClient: VelodyAPIClient {
|
||||
)
|
||||
}
|
||||
|
||||
private func executeData(_ request: URLRequest) async throws -> Data {
|
||||
let data: Data
|
||||
let response: URLResponse
|
||||
|
||||
do {
|
||||
(data, response) = try await session.data(for: request)
|
||||
} catch {
|
||||
throw VelodyAPIError.requestFailed(error.localizedDescription)
|
||||
}
|
||||
|
||||
try validate(response: response, data: data)
|
||||
return data
|
||||
}
|
||||
|
||||
private func decodeResponse<Response: Decodable>(
|
||||
data: Data,
|
||||
response: URLResponse,
|
||||
@@ -401,6 +438,18 @@ public struct StubVelodyAPIClient: VelodyAPIClient {
|
||||
)
|
||||
}
|
||||
|
||||
public func downloadAudioAsset(
|
||||
assetId: String,
|
||||
deviceId: String
|
||||
) async throws -> Data {
|
||||
_ = assetId
|
||||
_ = deviceId
|
||||
|
||||
return Data([
|
||||
0x49, 0x44, 0x33, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21,
|
||||
])
|
||||
}
|
||||
|
||||
public func prepareUpload(
|
||||
_ payload: UploadPrepareRequest
|
||||
) async throws -> UploadPrepareResponse {
|
||||
|
||||
Reference in New Issue
Block a user