TheraForge's Client REST API framework to connect to TheraForge's secure CloudBox Backend-as-a-Service (BaaS)

Overview

OTFCloudClientAPI

TheraForge's Client REST API Framework to Connect to TheraForge's Secure CloudBox Backend-as-a-Service (BaaS).

OTFCloudClientAPI provides the following functions to interact with the cloud back-end.

Configuration

Before creating an instance of TheraForgeNetwork, you need to first set up configurations with the API-KEY assigned to you and with the server URL like so:

let configurations = NetworkingLayer.Configurations(APIBaseURL: URL(string:"Server URL"), apiKey: "Your API_KEY")
TheraForgeNetwork.configureNetwork(configurations)
let otfNetworkService = TheraForgeNetwork.shared

Now you can use this shared instance to call any of the authentication APIs.

Remember that if you try to access the shared instance of TheraForgeNetwork your application will crash with the exception of missing configurations.

Login

You need to create a Request object for login credentials as shown in the example below. In it you need to pass an email address and a password. In the response block, it will return Result which will contain the response with user data if the credentials are correct or an error if something went wrong.

otfNetworkService.login(request: Request.Login(email: "Your email address", password: "Your password")) { (result) in
    switch result {
        case .success(let response): print(response)
        case .failure(let error): print(error)
    }
}

Social Login

You need to create a Request object for social login credentials as shown in the example below. In it you need to pass socialID and specify the social platform (Apple ID in this example). Email here is optional (Apple does not provide the email address on the second attempt). In the response block, it will return Result which will contain the response with user data if the request completes successfully or an error if something went wrong:

let socialLogin = Request.SocialLogin(type: .patient, email: "[email protected]",
                                      loginType: .apple,
                                      socialId: "12384c4a36634ca82cf0a6032005a4e")
otfNetworkService.socialLogin(request: socialLogin) { result in
    switch result {
        case .success(let response): print(response)
        case .failure(let error): print(error)
    }
}

Signup

Create a SignUp Request with the required fields and pass this signup request to TheraForgeNetwork. Like other requests you'll get the success case if everything goes well, otherwise failure.

otfNetworkService.signup(request: .init(email: "your email address",
                         password: "your password",
                         first_name: "your first name",
                         last_name: "your last name",
                         type: .patient,
                         dob: "08-07-1997",
                         gender: "male",
                         phoneNo: "(111) 111-1111")) { (result) in
        switch result {
        case .success(let response): print(response)
        case .failure(let error): print(error)
    }
}

Change password

Create a ChangePassword request and pass it to the TheraForgeNetwork. You need to provide your current password and the new password that you want to set:

otfNetworkService.changePassword(request: .init(email: "Your email address", password: "Your old password", newPassword: "Your new password")) { (result) in
    switch result {
    case .success(let response): print(response)
    case .failure(let error): print(error)
    }
}

Forgot password

Create a ForgotPassword request and send it to TheraForgeNetwork as shown in the example below:

otfNetworkService.forgotPassword(request: .init(email: "Your email address.")) { (result) in
    switch result {
    case .success(let response): print(response )
    case .failure(let error): print(error)
    }
}

This function will send an OTP on the given email ID, that you need to use with the Reset password API mentioned below.

Reset password OTP code

Once you use forgot password API, an OTP code will be sent to your email address that you need to use with this API in order to create a new password:

otfNetworkService.resetPassword(request: .init(email: "Your email address", code: "code", newPassword: "Your new password")) { (result) in
    switch result {
    case .success: print("success")
    case .failure(let error): print(error)
    }
}

Refresh token

The auth token that you get upon authentication is valid for 30 minutes only. In the auth object, you get the refresh token as well. Use this refresh token to refresh your auth token. Once you authorise the auth and user objects are stored in the keychain. You need to make sure that you have a refresh token before calling this API, else the application will crash:

guard TheraForgeKeychainService.shared.loadAuth().refreshToken != nil else {
    return
}
otfNetworkService.refreshToken(completionHandler: completionHandler)

Logout

When you need to invalidate a user's auth you may call the logout API like so:

otfNetworkService.signOut { (result) in
    switch result {
    case .success(let response): print(response)
    case .failure(let error): print(error.localizedDescription)
    }
}

Server sent events (SSE)

The server sends an event to the user when a change occurs in that user's database. There's a subscribe endpoint to listen to those events. There are three parts to this subscription:

  1. Connection established: When the connection with the server is established you get a callback.
otfNetworkService.eventSourceOnOpen = {
    print("Server sent event connection opened")
}
  1. Event received: Once subscribed for the events the application keep getting the events.
otfNetworkService.onReceivedMessage = { event in
    print(event)
    guard event.message.count > 0 else {
        print("This is just a keep alive event.")
        return
    }

    print("This is a db change event. You may take a decision here if you want to do something.")
}

There are two types of events: i. keep-alive, ii. db-updated. In case there is a db change then the event will have some message e.g. "Database updated." and for keep alive events it'll be an empty string. You can take a decision based on this message as shown in the example above.

  1. Event source completed: You get this callback upon disconnection with the server. In the callback you get a flag reconnect that indicates if you should resubscribe for the events or not.
otfNetworkService.eventSourceOnComplete = { code, reconnect, error in
    if let error = error {
        print(error.localizedDescription)
    }
    else if reconnect {
        // resubscribe to events if you want
    }
}

Once the above setup is done, you may call the subscribe API:

otfNetworkService.observeOnServerSentEvents(auth: auth)

License

This project is made available under the terms of a modified BSD license. See the LICENSE file.

You might also like...
SSL/TLS Add-in for BlueSocket using Secure Transport and OpenSSL

BlueSSLService SSL/TLS Add-in framework for BlueSocket in Swift using the Swift Package Manager. Works on supported Apple platforms (using Secure Tran

Easy to use SMJobBless, along with a full Swift implementation of the Authorization Services and Service Management frameworks

Leverage SMJobBless functionality with just one function call: let message = "Example App needs your permission to do thingamajig." let icon = Bundle.

OctopusKit is a simplicity but graceful solution for invoke RESTful web service APIs
OctopusKit is a simplicity but graceful solution for invoke RESTful web service APIs

OctopusKit OctopusKit is a simplicity but graceful solution for invoke RESTful web service APIs,it can help coder develop app based MVC pattern, it ca

Twitter-Client - A twitter client that allow users to view tweets on their iphone

Project 3 - Twitter Client Name of your app is a basic twitter app to read your

 Postie - The Next-Level HTTP API Client using Combine
Postie - The Next-Level HTTP API Client using Combine

Postie - The Next-Level HTTP API Client using Combine Postie is a pure Swift library for building URLRequests using property wrappers.

An iOS library to route API paths to objects on client side with request, mapping, routing and auth layers

WANetworkRouting Developed and Maintained by ipodishima Founder & CTO at Wasappli Inc. Sponsored by Wisembly A routing library to fetch objects from a

An unofficial supported Swift client library for accessing News API.

An unofficial supported Swift client library for accessing News API.

The official iOS client library for api.video
The official iOS client library for api.video

api.video iOS client api.video is the video infrastructure for product builders.

A web API client in Swift built using Async/Await

Get A modern web API client in Swift built using Async/Await and Actors. let cli

Releases(1.0.2-beta)
Owner
TheraForge
The TheraForge software development kit (SDK) is a rapid application development (RAD) solution for offline-first server-less mobile and web-based eHealth apps
TheraForge
Simple REST Client based on RxSwift and Alamofire.

RxRestClient Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements iOS 10.0+ tvOS 10.

STDev 16 Nov 19, 2022
Simple REST Client based on RxSwift and Alamofire.

RxRestClient Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements iOS 10.0+ tvOS 10.

STDev 16 Nov 19, 2022
Swift backend / server framework (Pure Swift, Supports Linux)

NetworkObjects NetworkObjects is a #PureSwift backend. This framework compiles for OS X, iOS and Linux and serves as the foundation for building power

Alsey Coleman Miller 258 Oct 6, 2022
Elegantly connect to a JSON api. (Alamofire + Promises + JSON Parsing)

⚠ Important Notice: Farewell ws... hello Networking ! Networking is the next generation of the ws project. Think of it as ws 2.0 built for iOS13. It u

Fresh 351 Oct 2, 2022
A native, lightweight and secure time-based (TOTP) & counter-based (HOTP) password client built for iOS

A native, lightweight and secure time-based (TOTP) & counter-based (HOTP) password client built for iOS Built by Tijme Gommers – Buy me a coffee via P

Raivo OTP 770 Jan 8, 2023
The civilized way to write REST API clients for iOS / macOS

The elegant way to write iOS / macOS REST clients Drastically simplifies app code by providing a client-side cache of observable models for RESTful re

Bust Out 2.2k Nov 20, 2022
Swift/Obj-C HTTP framework with a focus on REST and JSON

Now Archived and Forked PMHTTP will not be maintained in this repository going forward. Please use, create issues on, and make PRs to the fork of PHMT

Postmates Inc. 509 Sep 4, 2022
Lightweight REST and JSON library for Swift.

RestEssentials is an extremely lightweight REST and JSON library for Swift and can be used on iOS, iPadOS, macOS, tvOS, and watchOS. Features Easily p

null 37 Nov 2, 2022
Lightweight REST library for iOS and watchOS. Available on cococapods

RMHttp RMHttp is a Lightweight REST library for iOS and watchOS. Features Chainable Request URL / JSON Parameter Encoding HTTP Methods GET/POST/DELETE

Roger Molas 7 Nov 19, 2022
🌏 A zero-dependency networking solution for building modern and secure iOS, watchOS, macOS and tvOS applications.

A zero-dependency networking solution for building modern and secure iOS, watchOS, macOS and tvOS applications. ?? TermiNetwork was tested in a produc

Bill Panagiotopoulos 90 Dec 17, 2022