Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 00b651e

Browse files
committed
Add JetpackAIServiceRemote/transcribeAudio
1 parent cb6f875 commit 00b651e

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

Sources/CoreAPI/HTTPRequestBuilder.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ final class HTTPRequestBuilder {
6868
.append(query: urlComponents.queryItems ?? [])
6969
}
7070

71+
func headers(_ headers: [String: String]) -> Self {
72+
for (key, value) in headers {
73+
self.headers[key] = value
74+
}
75+
return self
76+
}
77+
7178
func header(name: String, value: String?) -> Self {
7279
headers[name] = value
7380
return self

Sources/CoreAPI/WordPressComRestApi.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,8 @@ open class WordPressComRestApi: NSObject {
449449

450450
public func upload(
451451
URLString: String,
452-
parameters: [String: AnyObject]?,
452+
parameters: [String: AnyObject]? = nil,
453+
httpHeaders: [String: String]? = nil,
453454
fileParts: [FilePart],
454455
requestEnqueued: RequestEnqueuedBlock? = nil,
455456
fulfilling progress: Progress? = nil
@@ -462,6 +463,7 @@ open class WordPressComRestApi: NSObject {
462463
builder = try requestBuilder(URLString: URLString)
463464
.method(.post)
464465
.body(form: form)
466+
.headers(httpHeaders ?? [:])
465467
} catch {
466468
return .failure(.requestEncodingFailure(underlyingError: error))
467469
}

Sources/WordPressKit/Services/JetpackAIServiceRemote.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,26 @@ import Foundation
22

33
public final class JetpackAIServiceRemote: SiteServiceRemoteWordPressComREST {
44
/// Returns short-lived JWT token (lifetime is in minutes).
5-
public func getJWT() async throws -> String {
5+
public func getAuthorizationToken() async throws -> String {
66
struct Response: Decodable {
77
let token: String
88
}
99
let path = path(forEndpoint: "sites/\(siteID)/jetpack-openai-query/jwt", withVersion: ._2_0)
1010
let response = await wordPressComRestApi.perform(.post, URLString: path, type: Response.self)
1111
return try response.get().body.token
1212
}
13+
14+
/// - parameter token: Token retrieved using ``JetpackAIServiceRemote/getAuthorizationToken``.
15+
public func transcribeAudio(from fileURL: URL, token: String) async throws -> String {
16+
let path = path(forEndpoint: "jetpack-ai-transcription?feature=voice-to-content", withVersion: ._2_0)
17+
let file = FilePart(parameterName: "audio_file", url: fileURL, fileName: "voice_recording", mimeType: "audio/m4a")
18+
let result = await wordPressComRestApi.upload(URLString: path, httpHeaders: [
19+
"Authorization": "Bearer \(token)"
20+
], fileParts: [file])
21+
guard let body = try result.get().body as? [String: Any],
22+
let text = body["text"] as? String else {
23+
throw URLError(.unknown)
24+
}
25+
return text
26+
}
1327
}

0 commit comments

Comments
 (0)