add explain module
This commit is contained in:
@@ -37,3 +37,46 @@ enum SentenceBoundaryAPI {
|
||||
return result.value
|
||||
}
|
||||
}
|
||||
|
||||
struct SentenceExplanationResponse: Decodable {
|
||||
let explanation: String
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case explanation
|
||||
}
|
||||
}
|
||||
|
||||
extension SentenceBoundaryAPI {
|
||||
static func explain(
|
||||
_ sentence: SentenceBoundary,
|
||||
courseTitle: String
|
||||
) async throws -> String {
|
||||
var body: [String: Any] = [
|
||||
"sentence_text": sentence.text ?? "",
|
||||
"course_title": courseTitle,
|
||||
"sentence_index": sentence.index,
|
||||
]
|
||||
if let language = sentence.language {
|
||||
body["language"] = language
|
||||
}
|
||||
var headers: [String: String] = ["Content-Type": "application/json"]
|
||||
if !AppConfig.authToken.isEmpty {
|
||||
headers["Authorization"] = "Bearer \(AppConfig.authToken)"
|
||||
}
|
||||
let result = try await HTTPClient.execute(
|
||||
baseURL: AppConfig.serverBaseURL,
|
||||
path: "api/v1/sentences/explain",
|
||||
method: "POST",
|
||||
timeout: AppConfig.readTimeout,
|
||||
headers: headers,
|
||||
body: try JSONSerialization.data(withJSONObject: body)
|
||||
)
|
||||
guard (200...299).contains(result.statusCode) else {
|
||||
let detail = String(data: result.body, encoding: .utf8) ?? ""
|
||||
throw HTTPClientError.badStatus(result.statusCode, detail)
|
||||
}
|
||||
return try JSONDecoder()
|
||||
.decode(SentenceExplanationResponse.self, from: result.body)
|
||||
.explanation
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user