Swift
The Swift implementation uses the BVContentCoachQueryResponse and BVContentCoachSubmissionResponse handlers to process Result types (Success or Failure).
Retrieve AI-generated topic suggestions
To retrieve AI-generated topic suggestions for a product within the review submission form, initiate a BVReviewTokensQuery and execute it using .async() as shown below:
// 1. Create and configure the query
let query = BVReviewTokensQuery(productId: "product_id")
.configure(BVReviewTokensQueryTest.config) // Apply your BV configuration
.handler { (response: BVContentCoachQueryResponse<BVReviewTokens>) in
switch response {
case .success(let tokensResponse):
// Check for API-level errors within a successful HTTP response
guard let status = tokensResponse.status,
let error = BVContentCoachError("\(status)", message: tokensResponse.detail) else {
// Extract and use the tokens
guard let tokens = tokensResponse.tokens else {
print("No tokens returned")
return
}
print(tokens)
return
}
print("\(error.code) : \(error.message)")
case .failure(let errors):
// Handle network or system failures
errors.forEach { (error: Error) in
guard let bverror: BVError = error as? BVError else { return }
print(bverror.message)
}
}
}
// 2. Execute the query
query.async()
Retrieve matched topics
Once the user types at least 5 words into the Review text box, trigger BVMatchedTokensSubmission to pass the live texts. Use this to evaluate which words match the topic suggestions from BVReviewTokensQuery. Ensure the BVMatchedTokens data object is nested inside BVMatchedTokensSubmission as shown below:
// 1. Define the tokens payload with the current review text
let tokens = BVMatchedTokens(productId: "product_id", reviewText: "review_text")
guard let tokensSubmission = BVMatchedTokensSubmission(tokens) else { return }
// 2. Configure and attach the handler
tokensSubmission
.configure(BVMatchedTokensQueryTest.config)
.handler { (response: BVContentCoachSubmissionResponse<BVMatchedTokens>) in
switch response {
case .success(let tokensResponse):
guard let status = tokensResponse.status,
let error = BVContentCoachError("\(status)", message: tokensResponse.detail) else {
guard let tokens = tokensResponse.tokens else {
print("No tokens returned")
return
}
print(tokens) // UI updates for matched tokens go here
return
}
print("\(error.code) : \(error.message)")
case .failure(let errors):
errors.forEach { (error: Error) in
guard let bverror: BVError = error as? BVError else { return }
print(bverror.message)
}
}
}
// 3. Execute the submission
tokensSubmission.async()
