Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@W-16589742: [iOS] REST wrappers for select SFAP APIs #3801

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

JohnsonEricAtSalesforce
Copy link
Contributor

@JohnsonEricAtSalesforce JohnsonEricAtSalesforce commented Jan 13, 2025

🧱 Draft: Only one of the four endpoints is included. We'll review that for a quick look, then add the remaining three 🚧

This adds REST API clients for select sfap_api endpoints to match the Android version in forcedotcom/SalesforceMobileSDK-Android#2644

Only one of the four is included in the draft status. Here's a code sample from how I tested in iOSNativeSwiftTemplate's AppDelegate.

    /// The generation id from the `sfap_api` `generations` endpoint
    private var generationId: String? = nil
    
    /// Fetches generated text from the `sfap_api` "generations" endpoint.
    private func generateText() async {
        do {
            guard let userAccountCurrent = UserAccountManager.shared.currentUserAccount, let restClient = RestClient.restClient(for: userAccountCurrent) else { return }
            
            // Guards.            
            let generationsResponseBody = try await SfapApiClient(
                apiHostName: "dev.api.salesforce.com",
                modelName: "sfdc_ai__DefaultGPT35Turbo",
                restClient: restClient
            ).fetchGeneratedText(
                "Tell me a story about an action movie hero with really, really cool hair."
            )
            
            self.generationId = generationsResponseBody.generation?.id
            print("SFAP_API-TESTS: \(String(describing: generationsResponseBody.generation?.generatedText))")
            print("SFAP_API-TESTS: \(String(describing: generationsResponseBody.sourceJson))")
        } catch let error {
            SFSDKCoreLogger().e(
                SfapApiClient.classForCoder(),
                message: "Cannot fetch generated text due to an error with message '\(error.localizedDescription)'.")
        }
    }

// Guards.
guard
let userAccountCurrent = UserAccountManager.shared.currentUserAccount,
let restClient = RestClient.restClient(for: userAccountCurrent) else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this getting a RestClient based on the current user vs using the RestClient from init?

let restClient = RestClient.restClient(for: userAccountCurrent) else {
throw sfapApiErrorWithMessage("Cannot invoke sfap_api client without a current user account.")}

guard let modelNameUnwrapped = modelName else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you don't want to rename it, I think you could also do guard let modelName { here

do {
return try sfapApiGenerationsResponse.asDecodable(type: SfapApiGenerationsResponseBody.self)
} catch let error {
throw sfapApiErrorWithMessage("Cannot JSON decode sfap_api generations response body due to a decoding error with description '\(error.localizedDescription)'.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little confused with the error handling, why does the error need to be caught and re-thrown?

/// @param messageCode The `sfap_api` error code
/// @param source The original `sfap_api` error response body
@objc
public class SfapApiError : NSError, @unchecked Sendable {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this use NSError? Was there an issue with Swift's error type?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants