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

[Feat]New connection methods #6

Merged
merged 1 commit into from
Nov 28, 2024
Merged
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
67 changes: 65 additions & 2 deletions Sources/PrivMXEndpointSwift/Core/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,38 @@ 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.
///
/// - 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.
///
/// - Throws: `PrivMXEndpointError.failedConnecting` if establishing the connection fails.
///
/// - Returns: A `Connection` instance that can be used for further operations.
public static func connect(
userPrivKey: std.string,
solutionId: std.string,
bridgeUrl: std.string
) throws -> Connection{
let res = privmx.NativeConnectionWrapper.connect(userPrivKey,
solutionId,
bridgeUrl)
guard res.error.value == nil else {
throw PrivMXEndpointError.failedConnecting(res.error.value!)
}
guard let result = res.result.value else{
var err = privmx.InternalError()
err.name = "Value error"
err.description = "Unexpectedly received nil result"
throw PrivMXEndpointError.failedConnecting(err)
}

return Connection(api:result.pointee)
}

/// 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.
Expand All @@ -48,7 +80,8 @@ public class Connection{
/// - Throws: `PrivMXEndpointError.failedConnecting` if establishing the connection fails.
///
/// - Returns: A `Connection` instance that can be used for further operations.
public static func connect(
@available(*, deprecated, renamed: "connect(userPrivKey:solutionId:bridgeUrl:)")
public static func connect(
userPrivKey: std.string,
solutionId: std.string,
platformUrl: std.string
Expand All @@ -74,17 +107,47 @@ public class Connection{
/// 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.
///
/// - Throws: `PrivMXEndpointError.failedConnecting` if establishing the connection fails.
///
/// - Returns: A public `Connection` instance that can be used for non-authenticated operations.
public static func connectPublic(
solutionId: std.string,
bridgeUrl: std.string
) throws -> Connection{
let res = privmx.NativeConnectionWrapper.connectPublic(solutionId,
bridgeUrl)
guard res.error.value == nil else {
throw PrivMXEndpointError.failedConnecting(res.error.value!)
}
guard let result = res.result.value else{
var err = privmx.InternalError()
err.name = "Value error"
err.description = "Unexpectedly received nil result"
throw PrivMXEndpointError.failedConnecting(err)
}

return Connection(api:result.pointee)
}

/// Creates a new public connection to PrivMX platform.
///
/// 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 platformUrl: The URL of PrivMX platform endpoint.
///
/// - Throws: `PrivMXEndpointError.failedConnecting` if establishing the connection fails.
///
/// - Returns: A public `Connection` instance that can be used for non-authenticated operations.
@available(*, deprecated, renamed: "connectPublic(solutionId:bridgeUrl:)")
public static func connectPublic(
solutionId: std.string,
platformUrl: std.string
) throws -> Connection{
let res = privmx.NativeConnectionWrapper.platformConnectPublic(solutionId,
platformUrl)
platformUrl)
guard res.error.value == nil else {
throw PrivMXEndpointError.failedConnecting(res.error.value!)
}
Expand Down
18 changes: 14 additions & 4 deletions Sources/PrivMXEndpointSwiftNative/NativeConnectionWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ NativeConnectionWrapper::NativeConnectionWrapper(std::shared_ptr<core::Connectio
ResultWithError<std::shared_ptr<NativeConnectionWrapper>> NativeConnectionWrapper::platformConnect(const std::string &userPrivKey,
const std::string &solutionId,
const std::string &platformUrl){
return connect(userPrivKey, solutionId, platformUrl);
}

ResultWithError<std::shared_ptr<NativeConnectionWrapper>> NativeConnectionWrapper::connect(const std::string &userPrivKey,
const std::string &solutionId,
const std::string &platformUrl){
ResultWithError<std::shared_ptr<NativeConnectionWrapper>> res;
std::shared_ptr<NativeConnectionWrapper> x;
try{
auto conn = NativeConnectionWrapper(std::make_shared<core::Connection>(core::Connection::platformConnect(userPrivKey,
auto conn = NativeConnectionWrapper(std::make_shared<core::Connection>(core::Connection::connect(userPrivKey,
solutionId,
platformUrl)));
res.result = std::make_shared<NativeConnectionWrapper>(conn);
Expand All @@ -57,12 +63,16 @@ ResultWithError<std::shared_ptr<NativeConnectionWrapper>> NativeConnectionWrappe
}

ResultWithError<std::shared_ptr<NativeConnectionWrapper>> NativeConnectionWrapper::platformConnectPublic(const std::string &solutionId,
const std::string &platformUrl){
const std::string &platformUrl){
return connectPublic(solutionId, platformUrl);
}
ResultWithError<std::shared_ptr<NativeConnectionWrapper>> NativeConnectionWrapper::connectPublic(const std::string &solutionId,
const std::string &platformUrl){
ResultWithError<std::shared_ptr<NativeConnectionWrapper>> res;
std::shared_ptr<NativeConnectionWrapper> x;
try{
auto conn = NativeConnectionWrapper(std::make_shared<core::Connection>(core::Connection::platformConnectPublic(solutionId,
platformUrl)));
auto conn = NativeConnectionWrapper(std::make_shared<core::Connection>(core::Connection::connectPublic(solutionId,
platformUrl)));
res.result = std::make_shared<NativeConnectionWrapper>(conn);
}catch(core::Exception& err){
res.error = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ class NativeConnectionWrapper {
* @return Shared pointer to a Connection object, wrapped in the `ResultWithError` for error handling.
*
*/
static ResultWithError<std::shared_ptr<NativeConnectionWrapper>> connect(const std::string& userPrivKey,
const std::string& solutionId,
const std::string& bridgeUrl);
/**
* Connects to the PrivMX Bridge server.
*
* @param userPrivKey user's private key
* @param solutionId ID of the Solution
* @param platformUrl Platform's Endpoint URL
*
* @return Shared pointer to a Connection object, wrapped in the `ResultWithError` for error handling.
*
*/
[[deprecated]]
static ResultWithError<std::shared_ptr<NativeConnectionWrapper>> platformConnect(const std::string& userPrivKey,
const std::string& solutionId,
const std::string& platformUrl);
Expand All @@ -50,8 +64,20 @@ class NativeConnectionWrapper {
* @return Shared pointer to a Connection object, wrapped in `ResultWithError` for error handling.
*
*/
static ResultWithError<std::shared_ptr<NativeConnectionWrapper>> connectPublic(const std::string& solutionId,
const std::string& bridgeUrl);
/**
* Connects to the PrivMX Bridge Server as a guest user.
*
* @param solutionId ID of the Solution
* @param platformUrl Platform's Endpoint URL
*
* @return Shared pointer to a Connection object, wrapped in `ResultWithError` for error handling.
*
*/
[[deprecated]]
static ResultWithError<std::shared_ptr<NativeConnectionWrapper>> platformConnectPublic(const std::string& solutionId,
const std::string& platformUrl);
const std::string& platformUrl);

/**
* Sets the path to.pem file with certificates.
Expand Down