Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 10 additions & 10 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,24 @@ let package = Package(
]),
.binaryTarget(
name:"GMP",
url: "https://github.com/simplito/privmx-endpoint-xcframeworks/releases/download/2.5.3/gmp-6.3.0.xcframework.zip",
checksum: "d699c72eae675bd1b78cd903236b7d6724b22f7213ba91b0451683f430ac0ac6"),
url: "https://github.com/simplito/privmx-endpoint-xcframeworks/releases/download/2.6.0/gmp-6.3.0.xcframework.zip",
checksum: "ce6cf0eed23bc77ee42022753e51a0cb3826ab486094c56cf2101b92935b858c"),
.binaryTarget(
name:"POCO",
url: "https://github.com/simplito/privmx-endpoint-xcframeworks/releases/download/2.5.3/poco-1.13.2.xcframework.zip",
checksum: "23956ca9ae72585d2158d0f9836b5b71184f6ebfb392a7af34d3fff704a4189d"),
url: "https://github.com/simplito/privmx-endpoint-xcframeworks/releases/download/2.6.0/poco-1.13.2.xcframework.zip",
checksum: "4cf1de72810bcabce2393e023f3aae299775cc68ea7314abc8c9d532f4c2348e"),
.binaryTarget(
name:"PSON",
url: "https://github.com/simplito/privmx-endpoint-xcframeworks/releases/download/2.5.3/pson-1.0.7.xcframework.zip",
checksum: "e7a8773f43b9792c42f93d4aae53b4a942e39a149b71dac4099950dfa6f125a7"),
url: "https://github.com/simplito/privmx-endpoint-xcframeworks/releases/download/2.6.0/pson-1.0.7.xcframework.zip",
checksum: "c4a4cbaf063ebfcbb78b7e5396507664caed95be9c5d49013623974260ea1275"),
.binaryTarget(
name:"OpenSSL",
url:"https://github.com/simplito/privmx-endpoint-xcframeworks/releases/download/2.5.3/openssl-3.0.16.xcframework.zip",
checksum: "b628a920217e792365231a48323e19de75cabaee84b81cfd378d5c1c16ff7855"),
url:"https://github.com/simplito/privmx-endpoint-xcframeworks/releases/download/2.6.0/openssl-3.0.17.xcframework.zip",
checksum: "f062d32b63759750c6b38c82f289f686cea8e60eb6a3c9fa87245b9fdaa10c46"),
.binaryTarget(
name:"PrivMXEndpoint",
url: "https://github.com/simplito/privmx-endpoint-xcframeworks/releases/download/2.5.3/privmx-endpoint-v2.5.3.xcframework.zip",
checksum: "3da66cc8668f9132a656ae4d252d5795e8340d0b4ef1ae6ae761950a2dd18c84")
url: "https://github.com/simplito/privmx-endpoint-xcframeworks/releases/download/2.6.0/privmx-endpoint-v2.6.0.xcframework.zip",
checksum: "62e7ce5c1fa193a11a45a449c083821f315686934de064aa6e1e2e8adc945c57")

],
cxxLanguageStandard: .cxx17
Expand Down
76 changes: 39 additions & 37 deletions Sources/PrivMXEndpointSwift/Core/BackendRequester.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,27 @@
import Foundation
import PrivMXEndpointSwiftNative

/// Tool for direct requests to PrivMX Bridge.
/// 'BackendRequester' provides functions to call PrivMX Bridge API.
public enum BackendRequester{

/// Makes a direct request to PrivMX Bridge.
///
/// This function allows you to make direct API requests to PrivMX Bridge with the required authorization and parameters. The response is returned as a string, representing the result of the request.
///
/// - Parameters:
/// - serverUrl: The URL of PrivMX cloud server to which the request will be sent.
/// - memberToken: The authorization token for the member making the request.
/// - method: The API method or endpoint to be called on PrivMX Bridge.
/// - paramsAsJson: The parameters for the request, formatted as a JSON string.
///
/// - Throws:
/// - `PrivMXEndpointError.failedRequestingBackend` if the request to the backend fails due to any error from the bridge or incorrect request formatting.
/// - Any other unexpected errors that might occur during the execution of the request.
///
/// - Returns:
/// - A string containing the response from PrivMX Bridge, typically in JSON format.
///
/// - Parameter serverUrl: PrivMX Bridge server URL
/// - Parameter accessToken: token for authorization (see PrivMX Bridge API for more details)
/// - Parameter method: API method to call
/// - Parameter paramsAsJson: API method's parameters in JSON format
///
/// - Throws: `PrivMXEndpointError.failedRequestingBackend` if the request to the backend fails due to any error from the bridge or incorrect request formatting. And any other unexpected errors that might occur during the execution of the request.
///
/// - Returns: JSON string representing raw server response
public static func backendRequest(
serverUrl: std.string,
memberToken: std.string,
accessToken: std.string,
method: std.string,
paramsAsJson: std.string
) throws -> std.string {
let res = privmx.NativeBackendRequesterWrapper.backendRequest(serverUrl,
memberToken,
accessToken,
method,
paramsAsJson)
guard res.error.value == nil else{
Expand All @@ -53,21 +47,29 @@ public enum BackendRequester{
return result
}

/// Makes a direct request to PrivMX Bridge.
///
/// This function allows you to make direct API requests to PrivMX Bridge with the required parameters. The response is returned as a string, representing the result of the request.
@available(*, deprecated, renamed: "backendRequest(serverUrl:accessToken:method:paramsAsJson:)")
public static func backendRequest(
serverUrl: std.string,
memberToken: std.string,
method: std.string,
paramsAsJson: std.string
) throws -> std.string {
try self.backendRequest(
serverUrl: serverUrl,
accessToken: memberToken,
method: method,
paramsAsJson: paramsAsJson)
}

/// Sends request to PrivMX Bridge API.
///
/// - Parameters:
/// - serverUrl: The URL of PrivMX cloud server to which the request will be sent.
/// - method: The API method or endpoint to be called on PrivMX Bridge.
/// - paramsAsJson: The parameters for the request, formatted as a JSON string.
/// - Parameter serverUrl: PrivMX Bridge server URL
/// - Parameter method: API method to call
/// - Parameter paramsAsJson: API method's parameters in JSON format
///
/// - Throws:
/// - `PrivMXEndpointError.failedRequestingBackend` if the request to the backend fails due to any error from the bridge or incorrect request formatting.
/// - Any other unexpected errors that might occur during the execution of the request.
/// - Throws: `PrivMXEndpointError.failedRequestingBackend` if the request to the backend fails due to any error from the bridge or incorrect request formatting. And any other unexpected errors that might occur during the execution of the request.
///
/// - Returns:
/// - A string containing the response from PrivMX Bridge, typically in JSON format.
/// - Returns: JSON string representing raw server response
public static func backendRequest(
serverUrl: std.string,
method: std.string,
Expand All @@ -91,13 +93,13 @@ public enum BackendRequester{

/// Sends a request to PrivMX Bridge API using pair of API KEY ID and API KEY SECRET for authorization.
///
/// - Parameters:
/// - serverUrl: PrivMX Bridge server URL
/// - apiKeyId: API KEY ID (see PrivMX Bridge API for more details)
/// - apiKeySecret: API KEY SECRET (see PrivMX Bridge API for more details)
/// - mode: allows you to set whether the request should be signed (mode = 1) or plain (mode = 0)
/// - method: API method to call
/// - paramsAsJson: API method's parameters in JSON format
/// - Parameter serverUrl: PrivMX Bridge server URL
/// - Parameter apiKeyId: API KEY ID (see PrivMX Bridge API for more details)
/// - Parameter apiKeySecret: API KEY SECRET (see PrivMX Bridge API for more details)
/// - Parameter mode: allows you to set whether the request should be signed (mode = 1) or plain (mode = 0)
/// - Parameter method: API method to call
/// - Parameter paramsAsJson: API method's parameters in JSON format
///
/// - Returns: A string containing the response from PrivMX Bridge, typically in JSON format.
public static func backendRequest(
serverUrl: std.string,
Expand Down
118 changes: 88 additions & 30 deletions Sources/PrivMXEndpointSwift/Core/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import Cxx
import CxxStdlib
import PrivMXEndpointSwiftNative

/// 'Connection' represents and manages the current connection between the Endpoint and the Bridge server.
///
/// Swift wrapper for `privmx.NativeConnectionWrapper`, used to establish and manage secure connections with PrivMX platform.
public class Connection{

Expand All @@ -37,17 +39,16 @@ public class Connection{
/// An instance of the wrapped C++ class.
internal var api: privmx.NativeConnectionWrapper

/// Creates a new connection instance to PrivMX platform using a private key.
///
/// The path to the certificates must be set beforehand using `setCertsPath()`. This connection is used to interact with secured operations such as Inboxes, Threads, and Stores.
/// Connects to the PrivMX Bridge server.
///
/// - Parameter userPrivKey: The user's private key in WIF format, required for authentication.
/// - Parameter solutionId: The ID of the Solution that the connection targets.
/// - Parameter bridgeUrl: The URL of PrivMX platform endpoint.
/// - Parameter userPrivKey: user's private key
/// - Parameter solutionId: ID of the Solution
/// - Parameter bridgeUrl: Bridge Server URL
/// - Parameter verificationOptions: PrivMX Bridge server instance verification options using a PKI server
///
/// - Throws: `PrivMXEndpointError.failedConnecting` if establishing the connection fails.
///
/// - Returns: A `Connection` instance that can be used for further operations.
/// - Returns: Connection object
public static func connect(
userPrivKey: std.string,
solutionId: std.string,
Expand Down Expand Up @@ -104,17 +105,17 @@ public class Connection{
return Connection(api:result.pointee)
}

/// Creates a new public connection to PrivMX platform.
/// Connects to the PrivMX Bridge Server as a guest user.
///
/// The path to the certificates must be set beforehand using `setCertsPath()`. This type of connection is primarily used for public operations, such as inbound Inbox traffic, where authentication is not required.
///
/// - Parameter solutionId: The ID of the Solution that the connection targets.
/// - Parameter bridgeUrl: The URL of PrivMX platform endpoint.
/// - Parameter verificationOptions: Options used to verify if Bridge on given url is the one you expect.
///
/// - Parameter solutionId: ID of the Solution
/// - Parameter bridgeUrl: Bridge Server URL
/// - Parameter verificationOptions: PrivMX Bridge server instance verification options using a PKI server
///
/// - Throws: `PrivMXEndpointError.failedConnecting` if establishing the connection fails.
///
/// - Returns: A public `Connection` instance that can be used for non-authenticated operations.
/// - Returns: Connection object
public static func connectPublic(
solutionId: std.string,
bridgeUrl: std.string,
Expand Down Expand Up @@ -166,13 +167,11 @@ public class Connection{
return Connection(api:result.pointee)
}

/// Retrieves the unique ID of the connection.
///
/// Each connection instance has a unique identifier that can be used to track and manage multiple connections.
/// Gets the ID of the current connection.
///
/// - Throws: `PrivMXEndpointError.failedGettingConnectionId` if retrieving the connection ID fails.
///
/// - Returns: The unique ID of the current connection as an `Int64`.
/// - Returns: ID of the connection
public func getConnectionId(
) throws -> Int64 {
let res = api.getConnectionId()
Expand All @@ -194,9 +193,7 @@ public class Connection{
self.api = api
}

/// Disconnects the current connection to PrivMX platform.
///
/// Once disconnected, the `Connection` instance, along with any associated API instances like `StoreApi` or `ThreadApi`, becomes unusable. It is important to call this method when the connection is no longer needed to free up resources.
///Disconnects from the PrivMX Bridge server.
///
/// - Throws: `PrivMXEndpointError.failedDisconnecting` if the disconnection process fails.
public func disconnect(
Expand All @@ -207,15 +204,15 @@ public class Connection{
}
}

/// Lists all Contexts available to the currently connected user.
/// Gets a list of Contexts available for the user.
///
/// Contexts represent different environments or groups to which the user has access. This method returns a list of these Contexts.
///
/// - Parameter query: A `PagingQuery` object that specifies the filtering and pagination options for the query.
/// - Parameter query: struct with list query parameters
///
/// - Throws: `PrivMXEndpointError.failedListingContexts` if listing the Contexts fails.
///
/// - Returns: A `ContextList` structure containing the total number of Contexts and a list of the retrieved Contexts.
/// - Returns: struct containing a list of Contexts
public func listContexts(
query: privmx.endpoint.core.PagingQuery
)throws -> privmx.ContextList{
Expand All @@ -232,17 +229,18 @@ public class Connection{
return result
}

/// Retrieves a list of Users from a particular Context.
/// Gets a list of users with their status and the last status change.
///
/// - parameter contextId: Id of the Context.
/// - parameter contextId: ID of the Context
///
/// - throws: When the operation fails.
///
/// - returns: a list of UserInfo objects.
public func getContextUsers(
contextId: std.string
) throws -> privmx.UserInfoVector {
let res = api.getContextUsers(contextId)
/// - returns: List of users with their status and the last status change
public func listContextUsers(
contextId: std.string,
query: privmx.endpoint.core.PagingQuery
) throws -> privmx.UserInfoList {
let res = api.listContextUsers(contextId,query)
guard res.error.value == nil else {
throw PrivMXEndpointError.failedGettingContextUsers(res.error.value!)
}
Expand Down Expand Up @@ -273,4 +271,64 @@ public class Connection{
throw PrivMXEndpointError.failedSettingUserVerifier(res.error.value!)
}
}

/// Subscribe for the Context events on the given subscription query.
///
/// - Parameter subscriptionQueries: List of queries
///
/// - Throws: When subscribing for events fails.
///
/// - Returns: List of subscriptionIds in matching order to subscriptionQueries
public func subscribeFor(
subscriptionQueries: privmx.SubscriptionQueryVector
) throws -> privmx.SubscriptionIdVector {
let res = api.subscribeFor(subscriptionQueries)
guard res.error.value == nil else {
throw PrivMXEndpointError.failedSubscribingForEvents(res.error.value!)
}
guard let result = res.result.value else {
var err = privmx.InternalError()
err.name = "Value error"
err.description = "Unexpectedly recived nil result"
throw PrivMXEndpointError.failedSubscribingForEvents(err)
}
return result
}

/// Unsubscribe from events for the given subscriptionId.
///
/// - Parameter subscriptionIds: List of subscriptionId
///
/// - Throws: When unsubscribing fails.
public func unsubscribeFrom(
subscriptionIds: privmx.SubscriptionIdVector
) throws -> Void {
let res = api.unsubscribeFrom(subscriptionIds)
guard res.error.value == nil else {
throw PrivMXEndpointError.failedUnsubscribingFromEvents(res.error.value!)
}
}

/// Generate subscription Query for the Context events.
///
/// - Parameter eventType: type of event which you listen for
/// - Parameter selectorType: scope on which you listen for events
/// - Parameter selectorId: ID of the selector
public func buildSubscriptionQuery(
eventType: privmx.endpoint.core.EventType,
selectorType: privmx.endpoint.core.EventSelectorType,
selectorId: std.string
) throws -> std.string {
let res = api.buildSubscriptionQuery(eventType,selectorType,selectorId)
guard res.error.value == nil else {
throw PrivMXEndpointError.failedBuildingSubscriptionQuery(res.error.value!)
}
guard let result = res.result.value else {
var err = privmx.InternalError()
err.name = "Value error"
err.description = "Unexpectedly recived nil result"
throw PrivMXEndpointError.failedBuildingSubscriptionQuery(err)
}
return result
}
}
Loading