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

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 51 additions & 27 deletions libs/SalesforceSDKCore/SalesforceSDKCore.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@
CED452ED1D808DEE009266EB /* SFNativeRestRequestListener.h in Headers */ = {isa = PBXBuildFile; fileRef = CED452E91D808DEE009266EB /* SFNativeRestRequestListener.h */; };
CED452EF1D808E0A009266EB /* SFNativeRestRequestListener.m in Sources */ = {isa = PBXBuildFile; fileRef = CED452EA1D808DEE009266EB /* SFNativeRestRequestListener.m */; };
CED452F01D808E0F009266EB /* SalesforceRestAPITests.m in Sources */ = {isa = PBXBuildFile; fileRef = CED452E81D808DEE009266EB /* SalesforceRestAPITests.m */; };
D38F430A2D35BD4B00B9E251 /* SfapApiClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = D38F43052D35BD4B00B9E251 /* SfapApiClient.swift */; };
D38F430B2D35BD4B00B9E251 /* SfapApiGenerationsRequestBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = D38F43072D35BD4B00B9E251 /* SfapApiGenerationsRequestBody.swift */; };
D38F430C2D35BD4B00B9E251 /* SfapApiError.swift in Sources */ = {isa = PBXBuildFile; fileRef = D38F43062D35BD4B00B9E251 /* SfapApiError.swift */; };
D38F430D2D35BD4B00B9E251 /* SfapApiGenerationsResponseBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = D38F43082D35BD4B00B9E251 /* SfapApiGenerationsResponseBody.swift */; };
JohnsonEricAtSalesforce marked this conversation as resolved.
Show resolved Hide resolved
E1C80CDF1C5AEBFA001B3A21 /* SFLoginViewController.h in Headers */ = {isa = PBXBuildFile; fileRef = E1C80CDD1C5AEBFA001B3A21 /* SFLoginViewController.h */; settings = {ATTRIBUTES = (Public, ); }; };
E1C80CE01C5AEBFA001B3A21 /* SFLoginViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = E1C80CDE1C5AEBFA001B3A21 /* SFLoginViewController.m */; };
E1C80CEC1C5AEE31001B3A21 /* SFSDKLoginHost.h in Headers */ = {isa = PBXBuildFile; fileRef = E1C80CE11C5AEE31001B3A21 /* SFSDKLoginHost.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -894,6 +898,10 @@
CED452E81D808DEE009266EB /* SalesforceRestAPITests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SalesforceRestAPITests.m; path = SalesforceSDKCoreTests/SalesforceRestAPITests.m; sourceTree = SOURCE_ROOT; };
CED452E91D808DEE009266EB /* SFNativeRestRequestListener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SFNativeRestRequestListener.h; path = SalesforceSDKCoreTests/SFNativeRestRequestListener.h; sourceTree = SOURCE_ROOT; };
CED452EA1D808DEE009266EB /* SFNativeRestRequestListener.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SFNativeRestRequestListener.m; path = SalesforceSDKCoreTests/SFNativeRestRequestListener.m; sourceTree = SOURCE_ROOT; };
D38F43052D35BD4B00B9E251 /* SfapApiClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SfapApiClient.swift; sourceTree = "<group>"; };
D38F43062D35BD4B00B9E251 /* SfapApiError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SfapApiError.swift; sourceTree = "<group>"; };
D38F43072D35BD4B00B9E251 /* SfapApiGenerationsRequestBody.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SfapApiGenerationsRequestBody.swift; sourceTree = "<group>"; };
D38F43082D35BD4B00B9E251 /* SfapApiGenerationsResponseBody.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SfapApiGenerationsResponseBody.swift; sourceTree = "<group>"; };
E1C80CDD1C5AEBFA001B3A21 /* SFLoginViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SFLoginViewController.h; path = Login/SFLoginViewController.h; sourceTree = "<group>"; };
E1C80CDE1C5AEBFA001B3A21 /* SFLoginViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SFLoginViewController.m; path = Login/SFLoginViewController.m; sourceTree = "<group>"; };
E1C80CE11C5AEE31001B3A21 /* SFSDKLoginHost.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SFSDKLoginHost.h; path = Login/LoginHost/SFSDKLoginHost.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1387,23 +1395,23 @@
82AF840C16DFD95F008912EF /* Classes */ = {
isa = PBXGroup;
children = (
B722A702233C432E0089736E /* Extensions */,
B7FB26CA1F78096300FB25A2 /* URLHandlers */,
B7FB26B51F78094A00FB25A2 /* IDP */,
B7A20F7C1F25850E00D1E4B0 /* Views */,
CED452A81D808D0C009266EB /* RestAPI */,
CE145A881D137FD2003BCC20 /* Analytics */,
E139509E1C535F9C00575C03 /* Login */,
4F96FC511BFD32130022F021 /* Common */,
B722A702233C432E0089736E /* Extensions */,
4F96FC631BFD32130022F021 /* Identity */,
B7FB26B51F78094A00FB25A2 /* IDP */,
4F96FC691BFD32130022F021 /* Instrumentation */,
E139509E1C535F9C00575C03 /* Login */,
4F96FCC41BFD32130022F021 /* OAuth */,
4F96FCD81BFD32130022F021 /* PushNotification */,
6935FB9823FDE919002BEFCC /* UserAccount */,
CED452A81D808D0C009266EB /* RestAPI */,
4F96FCEA1BFD32130022F021 /* Security */,
69C5F99F24A3D999002C294D /* Storage */,
4F96FD2E1BFD32140022F021 /* Test */,
B7FB26CA1F78096300FB25A2 /* URLHandlers */,
6935FB9823FDE919002BEFCC /* UserAccount */,
4F96FD371BFD32140022F021 /* Util */,
B7A20F7C1F25850E00D1E4B0 /* Views */,
);
path = Classes;
sourceTree = "<group>";
Expand Down Expand Up @@ -1588,47 +1596,59 @@
CED452A81D808D0C009266EB /* RestAPI */ = {
isa = PBXGroup;
children = (
4F5727DC27F27F1A0008CDA4 /* SFSDKPrimingRecordsResponse.h */,
4F5727E227F27F1A0008CDA4 /* SFSDKPrimingRecordsResponse.m */,
69E2FD9622FB937F008E0AF0 /* SFSDKEncryptedURLCache.h */,
69E2FD9D22FB937F008E0AF0 /* SFSDKEncryptedURLCache.m */,
6938392423C82F38008E8E9A /* SFSDKNullURLCache.h */,
6938392523C82F38008E8E9A /* SFSDKNullURLCache.m */,
D38F43092D35BD4B00B9E251 /* SfapApi */,
CE7F66291E556CA800DC3FBB /* SFNetwork.h */,
CE7F662A1E556CA800DC3FBB /* SFNetwork.m */,
CED452B01D808D0C009266EB /* SFRestAPI.h */,
CED452B11D808D0C009266EB /* SFRestAPI.m */,
CED452A91D808D0C009266EB /* SFRestAPI+Blocks.h */,
CED452AA1D808D0C009266EB /* SFRestAPI+Blocks.m */,
CED452AB1D808D0C009266EB /* SFRestAPI+Files.h */,
CED452AC1D808D0C009266EB /* SFRestAPI+Files.m */,
CED452AD1D808D0C009266EB /* SFRestAPI+Internal.h */,
CED452AE1D808D0C009266EB /* SFRestAPI+QueryBuilder.h */,
CED452AF1D808D0C009266EB /* SFRestAPI+QueryBuilder.m */,
6900D802243D521C00888336 /* SFRestAPI+Notifications.h */,
6900D803243D521C00888336 /* SFRestAPI+Notifications.m */,
CED452B01D808D0C009266EB /* SFRestAPI.h */,
CED452B11D808D0C009266EB /* SFRestAPI.m */,
CED452B41D808D0C009266EB /* SFRestRequest+Internal.h */,
CED452AE1D808D0C009266EB /* SFRestAPI+QueryBuilder.h */,
CED452AF1D808D0C009266EB /* SFRestAPI+QueryBuilder.m */,
CED452B51D808D0C009266EB /* SFRestRequest.h */,
CED452B61D808D0C009266EB /* SFRestRequest.m */,
FDCECBE6688B51C1B9BDC524 /* SFSObjectTree.m */,
FDCEC40C4B860474C90D02AF /* SFSObjectTree.h */,
B7895D0B2345015B00765D85 /* SFSDKCompositeRequest.h */,
B7895D0C2345015B00765D85 /* SFSDKCompositeRequest.m */,
B7895D1323450E8E00765D85 /* SFSDKCompositeRequest+Internal.h */,
B7895D172345304800765D85 /* SFSDKCompositeResponse.h */,
B7895D8123469DE200765D85 /* SFSDKCompositeResponse+Internal.h */,
B7895D182345304800765D85 /* SFSDKCompositeResponse.m */,
CED452B41D808D0C009266EB /* SFRestRequest+Internal.h */,
B7460C0C2347DD4E00C7512E /* SFSDKBatchRequest.h */,
B7460C0D2347DD4E00C7512E /* SFSDKBatchRequest.m */,
B7460C192347E1A100C7512E /* SFSDKBatchResponse.h */,
B7460C1A2347E1A100C7512E /* SFSDKBatchResponse.m */,
B7460C0D2347DD4E00C7512E /* SFSDKBatchRequest.m */,
B7460C1D2347E1CB00C7512E /* SFSDKBatchResponse+Internal.h */,
4F2410A9282DC87A00E5EFE3 /* SFSDKCollectionResponse.h */,
4F2410B0282DCA4B00E5EFE3 /* SFSDKCollectionResponse.m */,
B7895D0B2345015B00765D85 /* SFSDKCompositeRequest.h */,
B7895D0C2345015B00765D85 /* SFSDKCompositeRequest.m */,
B7895D1323450E8E00765D85 /* SFSDKCompositeRequest+Internal.h */,
B7895D172345304800765D85 /* SFSDKCompositeResponse.h */,
B7895D182345304800765D85 /* SFSDKCompositeResponse.m */,
B7895D8123469DE200765D85 /* SFSDKCompositeResponse+Internal.h */,
69E2FD9622FB937F008E0AF0 /* SFSDKEncryptedURLCache.h */,
69E2FD9D22FB937F008E0AF0 /* SFSDKEncryptedURLCache.m */,
6938392423C82F38008E8E9A /* SFSDKNullURLCache.h */,
6938392523C82F38008E8E9A /* SFSDKNullURLCache.m */,
4F5727DC27F27F1A0008CDA4 /* SFSDKPrimingRecordsResponse.h */,
4F5727E227F27F1A0008CDA4 /* SFSDKPrimingRecordsResponse.m */,
FDCEC40C4B860474C90D02AF /* SFSObjectTree.h */,
FDCECBE6688B51C1B9BDC524 /* SFSObjectTree.m */,
);
path = RestAPI;
sourceTree = "<group>";
};
D38F43092D35BD4B00B9E251 /* SfapApi */ = {
isa = PBXGroup;
children = (
D38F43052D35BD4B00B9E251 /* SfapApiClient.swift */,
D38F43062D35BD4B00B9E251 /* SfapApiError.swift */,
D38F43072D35BD4B00B9E251 /* SfapApiGenerationsRequestBody.swift */,
D38F43082D35BD4B00B9E251 /* SfapApiGenerationsResponseBody.swift */,
);
path = SfapApi;
sourceTree = "<group>";
};
E139509E1C535F9C00575C03 /* Login */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -2285,6 +2305,10 @@
E1C80CED1C5AEE31001B3A21 /* SFSDKLoginHost.m in Sources */,
695E86A829EE24D0002BDEA6 /* SPConfig.swift in Sources */,
CE4CE3961C0E526A009F6029 /* SFUserAccountManager.m in Sources */,
D38F430A2D35BD4B00B9E251 /* SfapApiClient.swift in Sources */,
D38F430B2D35BD4B00B9E251 /* SfapApiGenerationsRequestBody.swift in Sources */,
D38F430C2D35BD4B00B9E251 /* SfapApiError.swift in Sources */,
D38F430D2D35BD4B00B9E251 /* SfapApiGenerationsResponseBody.swift in Sources */,
CE4CE36B1C0E526A009F6029 /* SFDefaultUserManagementViewController.m in Sources */,
CEA8C9711F02F79F00448B51 /* SFSDKCoreLogger.m in Sources */,
B711290D1F8A780800436CFB /* SFSDKAlertView.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
SfapApiClient.swift
SalesforceSDKCore

Created by Eric C. Johnson ([email protected]) on 20250108.

Copyright (c) 2025-present, salesforce.com, inc. All rights reserved.

Redistribution and use of this software in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of salesforce.com, inc. nor the names of its contributors may be used to
endorse or promote products derived from this software without specific prior written
permission of salesforce.com, inc.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import Foundation

@objc(SFApApiClient)
public class SfapApiClient : NSObject {
JohnsonEricAtSalesforce marked this conversation as resolved.
Show resolved Hide resolved

/// The error domain
static let errorDomain = "com.salesforce.sfap_api.client"

/// The sfap_api hostname
private let apiHostName: String

/// The sfap_api model name
private let modelName: String?

/// The REST client
private let restClient: RestClient

@objc
public init(
apiHostName: String,
modelName: String? = nil,
restClient: RestClient) {
self.apiHostName = apiHostName
self.modelName = modelName
self.restClient = restClient
}

/// Fetches generated text from the `sfap_api` "generations" endpoint.
/// - Parameters:
/// - prompt: The prompt request parameter value
/// - Returns: The endpoint's response
@objc
public func fetchGeneratedText(
_ prompt: String
) async throws -> SfapApiGenerationsResponseBody {

// Guards.
guard
let userAccountCurrent = UserAccountManager.shared.currentUserAccount,
let restClient = RestClient.restClient(for: userAccountCurrent) else {
JohnsonEricAtSalesforce marked this conversation as resolved.
Show resolved Hide resolved
throw sfapApiErrorWithMessage("Cannot invoke sfap_api client without a current user account.")}

guard let modelNameUnwrapped = modelName else {
JohnsonEricAtSalesforce marked this conversation as resolved.
Show resolved Hide resolved
throw sfapApiErrorWithMessage("Cannot fetch generated embeddings without specifying a model name.")
}

// Generate the sfap_api generations request body.
guard let sfapApiGenerationsRequestBodyString = try {
do { return String(
data: try JSONEncoder().encode(
SfapApiGenerationsRequestBody(prompt: prompt)
),
encoding: .utf8)
} catch let error {
throw sfapApiErrorWithMessage("Cannot JSON encode sfap_api generations request body due to an encoding error with description '\(error.localizedDescription)'.")
}}() else {
throw sfapApiErrorWithMessage("Cannot JSON encode sfap_api generations request body.")
}

// Create the sfap_api generations request.
let sfapApiGenerationsRequest = RestRequest(
method: .POST,
baseURL: "https://\(apiHostName)/",
path: "einstein/platform/v1/models/\(modelNameUnwrapped)/generations",
queryParams: nil)
sfapApiGenerationsRequest.customHeaders = [
"x-sfdc-app-context" : "EinsteinGPT",
"x-client-feature-id" : "ai-platform-models-connected-app"
]
sfapApiGenerationsRequest.endpoint = ""
sfapApiGenerationsRequest.requiresAuthentication = true
sfapApiGenerationsRequest.setCustomRequestBodyString(
sfapApiGenerationsRequestBodyString,
contentType: "application/json; charset=utf-8"
)

// Submit the sfap_api generations request and fetch the response.
let sfapApiGenerationsResponse = await withCheckedContinuation { continuation in
restClient.send(
request: sfapApiGenerationsRequest
) { result in
continuation.resume(returning: result)
}
}

// React to the sfap_api generations response.
switch sfapApiGenerationsResponse {

case .success(let sfapApiGenerationsResponse):
// Decode the sfap_api generations response.
guard let sfapApiGenerationsResponseBody = try {
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)'.")
JohnsonEricAtSalesforce marked this conversation as resolved.
Show resolved Hide resolved
}}() else {
throw sfapApiErrorWithMessage("Cannot JSON decode sfap_api generations response body.")
}
return sfapApiGenerationsResponseBody

case .failure(let error):
throw sfapApiErrorWithMessage("sfap_api generations request failure with description '\(error.localizedDescription)'.")
}
}

private func sfapApiErrorWithMessage(_ message: String) -> SfapApiError {

SFSDKCoreLogger().e(
classForCoder,
message: message)

return SfapApiError(
domain: SfapApiClient.errorDomain,
code: -1,
userInfo: [NSLocalizedDescriptionKey : message],
message: message,
messageCode: nil,
source: nil)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
SfapApiError.swift
SalesforceSDKCore

Created by Eric C. Johnson ([email protected]) on 20250109.

Copyright (c) 2025-present, salesforce.com, inc. All rights reserved.

Redistribution and use of this software in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of salesforce.com, inc. nor the names of its contributors may be used to
endorse or promote products derived from this software without specific prior written
permission of salesforce.com, inc.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import Foundation

/// An exception derived from an `sfap_api` endpoint failure response.
/// See https://developer.salesforce.com/docs/einstein/genai/guide/access-models-api-with-rest.html#step-3-use-models-rest-api
/// @param errorCode The `sfap_api` error code
/// @param message The `sfap_api` error message
JohnsonEricAtSalesforce marked this conversation as resolved.
Show resolved Hide resolved
/// @param messageCode The `sfap_api` error code
/// @param source The original `sfap_api` error response body
@objc
public class SfapApiError : NSError, @unchecked Sendable {
JohnsonEricAtSalesforce marked this conversation as resolved.
Show resolved Hide resolved

var errorCode: String? = nil
var message: String? = nil
var messageCode: String? = nil
var source: String? = nil

public required init?(
coder: NSCoder
) {
super.init(coder: coder)
}

public init(
domain: String,
code: Int,
userInfo: [String : Any]? = nil,
message: String?,
messageCode: String?,
source: String?
) {
super.init(domain: domain, code: code, userInfo: userInfo)
self.message = message
self.messageCode = messageCode
self.source = source
}
}
Loading
Loading