REST Wrappers for SFAP APIs

We provide REST wrappers for sfap_api endpoints, simplifying integration for developers. The REST wrappers automatically include the JSON Web Token (JWT) provided to the user at login with each sfap_api request. Find these wrappers in the SfapApiClient class.

iOS 

1/// The generation ID from the `sfap_api` `generations` endpoint
2private var generationId: String? = nil
3
4/**
5 * Fetches generated text from the `sfap_api` "generations" endpoint.
6 */
7private func generateText() async {
8    do {
9        guard
10            let userAccountCurrent = UserAccountManager.shared.currentUserAccount,
11            let restClient = RestClient.restClient(for: userAccountCurrent)
12        else { return }
13
14        let generationsResponseBody = try await SFapClient(
15            apiHostName: "dev.api.salesforce.com",
16            modelName: "sfdc_ai__DefaultGPT35Turbo",
17            restClient: restClient
18        ).fetchGeneratedText(
19            "Tell me a story about an action movie hero with really cool hair."
20        )
21
22        self.generationId = generationsResponseBody.generation?.id
23        print("SFAP_API-TESTS: \(String(describing: generationsResponseBody
24        .generation?.generatedText))")
25        print("SFAP_API-TESTS: \(String(describing: generationsResponseBody
26        .sourceJson))")
27
28        // Test submitting feedback for the generated text.
29        await submitFeedback()
30    } catch {
31        SFSDKCoreLogger().e(
32            SFapClient.self,
33            message: "Cannot fetch generated text due to an error: '
34            \(error.localizedDescription)'."
35        )
36    }
37}
38
39/**
40 * Submits feedback for previously generated text from the `sfap_api`
41 * endpoints to the `sfap_api` `feedback` endpoint.
42 */
43private func submitFeedback() async {
44    do {
45        guard
46            let userAccountCurrent = UserAccountManager.shared.currentUserAccount,
47            let restClient = RestClient.restClient(for: userAccountCurrent)
48        else { return }
49
50        let feedbackResponseBody = try await SFapClient(
51            apiHostName: "dev.api.salesforce.com",
52            restClient: restClient
53        ).submitGeneratedTextFeedback(requestBody: FeedbackRequestBody(
54            feedback: "GOOD",
55            generationId: generationId
56        ))
57
58        print("SFAP_API-TESTS: \(String(describing: feedbackResponseBody.message))")
59        print("SFAP_API-TESTS: \(String(describing: feedbackResponseBody.sourceJson))")
60    } catch {
61        SFSDKCoreLogger().e(
62            SFapClient.self,
63            message: "Cannot submit feedback due to an error: '
64            \(error.localizedDescription)'."
65        )
66    }
67}

Android 

1// Region `sfap_api` Testing
2
3/** The generation ID from the `sfap_api` `generations` endpoint */
4private var generationId: String? = null
5
6/**
7 * Fetches generated text from the `sfap_api` `generations` endpoint.
8 * @return Job The Kotlin Coroutines Job running the fetch
9 */
10private fun generateText() = CoroutineScope(Dispatchers.Default).launch {
11    runCatching {
12        val generationsResponseBody = SfapApiClient(
13            apiHostName = "dev.api.salesforce.com",
14            modelName = "sfdc_ai__DefaultGPT35Turbo",
15            restClient = client ?: return@launch
16        ).fetchGeneratedText(
17            "Tell me a story about an action movie hero with really cool hair."
18        )
19
20        generationId = generationsResponseBody.generation?.id
21        Log.i("SFAP_API-TESTS", generationsResponseBody.generation?
22        .generatedText ?: "")
23        Log.i("SFAP_API-TESTS", generationsResponseBody.sourceJson ?: "")
24
25        // Test submitting feedback for the generated text.
26        submitFeedback()
27    }.onFailure { throwable ->
28        Log.e("App", "Cannot fetch generated text due to an error:
29        '${throwable.message}'.")
30    }
31}
32
33/**
34 * Submits feedback for previously generated text from the `sfap_api`
35 * endpoints to the `sfap_api` `feedback` endpoint.
36 */
37private fun submitFeedback() = CoroutineScope(Dispatchers.Default).launch {
38    runCatching {
39        val feedbackResponseBody = SfapApiClient(
40            apiHostName = "dev.api.salesforce.com",
41            restClient = client ?: return@launch
42        ).submitGeneratedTextFeedback(
43            requestBody = SfapApiFeedbackRequestBody(
44                feedback = "GOOD",
45                generationId = generationId
46            )
47        )
48
49        Log.i("SFAP_API-TESTS", feedbackResponseBody.message ?: "")
50        Log.i("SFAP_API-TESTS", feedbackResponseBody.sourceJson ?: "")
51    }.onFailure { throwable ->
52        Log.e("App", "Cannot submit feedback due to an error:
53        '${throwable.message}'.")
54    }
55}
56// endregion

See Also 

We've Moved

Welcome to the new home of the Mobile SDK Developer Guide! For now, the Japanese guide can be found in PDF form.