Making HTTP request using Swift on OpenWhisk? - swift

How can I make HTTP requests to retrieve and return data during a serverless Swift function running on Apache OpenWhisk?
Serverless cloud platforms restrict access to the runtime environment. This means you can't install extra libraries to help with this, e.g https://github.com/Alamofire/Alamofire.

The Swift runtime on Apache OpenWhisk does provide the following libraries pre-installed:
CCurl (0.2.3)
Kitura-net (1.7.10)
SwiftyJSON (15.0.1)
IBM Watson SDK (0.16.0)
The Kitura-net library provides a higher-level API for making HTTP requests than Swift's networking primitives (URLSession).
Here's an example of using that library to return data from an external JSON API as the function response.
import KituraNet
import Foundation
import SwiftyJSON
func httpRequestOptions() -> [ClientRequest.Options] {
let request: [ClientRequest.Options] = [
.method("GET"),
.schema("https://"),
.hostname("api.coindesk.com"),
.path("/v1/bpi/currentprice.json")
]
return request
}
func currentBitcoinPricesJson() -> JSON? {
var json: JSON = nil
let req = HTTP.request(httpRequestOptions()) { resp in
if let resp = resp, resp.statusCode == HTTPStatusCode.OK {
do {
var data = Data()
try resp.readAllData(into: &data)
json = JSON(data: data)
} catch {
print("Error \(error)")
}
} else {
print("Status error code or nil reponse received from App ID server.")
}
}
req.end()
return json
}
func main(args: [String:Any]) -> [String:Any] {
guard let json = currentBitcoinPricesJson() else {
return ["error": "unable to retrieve JSON API response"]
}
guard let rate = json["bpi"]["USD"]["rate_float"].double else {
return [ "error": "Currency not listed in Bitcoin prices" ]
}
return ["bitcoin_to_dollars": rate]
}
HTTP requests can be still be manually made using Swift's low-level networking primitives.

Related

How to get data from call cloud functions using swift app?

Now I'm developing cloud functions.
Please teach me how to get data from call cloud functions using swift app.
I use TypeScript in cloud functions as backend service.
import * as functions from "firebase-functions"
import * as admin from "firebase-admin"
admin.initializeApp(functions.config().firebase)
export const helloWorld = functions
.https.onCall((data: any, context: any) => {
functions.logger.info("Hello logs!", { structuredData: true })
return { result: "Hello World" }
})
And in frontend I use swift.
func callCloudfunction(){
functions.httpsCallable("helloWorld").call(["name": "taro"]) { (result, error) in
if let error = error as NSError? {
if error.domain == FunctionsErrorDomain {
let code = FunctionsErrorCode(rawValue: error.code)
let message = error.localizedDescription
let details = error.userInfo[FunctionsErrorDetailsKey]
print(message)
}
}
if let data = (result?.data as? [String: Any]), let text = data["result"] as? String {
print("SUCCESS: \(text)")
}
}
}
In Swift I think functions.httpsCallable("helloWorld").call(["name": "taro"]) method is like http post request. So in TypeScript I can get the name's data using data argument.
But I don't know how to get result data from TypeScript. In TypeScript I created json { result: "Hello World" }.
How do I fix the process in swift? I think this code below is not good.
if let data = (result?.data as? [String: Any]), let text = data["result"] as? String {
print("SUCCESS: \(text)")
}
And please tell me how to handle error process.
Some class and property in error handling are already deprecated .FunctionsErrorDomain and FunctionsErrorDetailsKey.
Please teach me how to handle getting data and handle error process.
Thank you.

Accessing Google API data from within 3 async callbacks and a function in SwiftUI

I know this question is asked a lot, but I can't figure out how to apply any answers to my program. Sorry in advance this async stuff makes absolutely zero sense to me.
Basically, I have a button in SwiftUI that, when pressed, calls a function that makes two API calls to Google Sheets using Alamofire and GoogleSignIn.
Button("Search") {
if fullName != "" {
print(SheetsAPI.nameSearch(name: fullName, user: vm.getUser()) ?? "Error")
}
}
This function should return the values of some cells on success or nil on an error. However, it only ever prints out "Error". Here is the function code.
static func nameSearch<S: StringProtocol>(name: S, advisory: S = "", user: GIDGoogleUser?) -> [String]? {
let name = String(name)
let advisory = String(advisory)
let writeRange = "'App Control'!A2:C2"
let readRange = "'App Control'!A4:V4"
// This function can only ever run when user is logged in, ! should be fine?
let user = user!
let parameters: [String: Any] = [
"range": writeRange,
"values": [
[
name,
nil,
advisory
]
]
]
// What I want to be returned
var data: [String]?
// Google Identity said use this wrapper so that the OAuth tokens refresh
user.authentication.do { authentication, error in
guard error == nil else { return }
guard let authentication = authentication else { return }
// Get the access token to attach it to a REST or gRPC request.
let token = authentication.accessToken
let headers: HTTPHeaders = ["Authorization": "Bearer \(token)"]
AF.request("url", method: .put, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseString { response in
switch response.result {
case .success:
// I assume there is a better way to make two API calls...
AF.request("anotherURL", headers: headers).responseDecodable(of: NameResponseModel.self) { response2 in
switch response2.result {
case .success:
guard let responseData = response2.value else { return }
data = responseData.values[0]
// print(responseData.values[0]) works fine
case .failure:
print(response2.error ?? "Unknown error.")
data = nil
}
}
case .failure:
print(response.error ?? "Unknown error.")
data = nil
}
}
}
// Always returns nil, "Unknown error." never printed
return data
}
The model struct for my second AF request:
struct NameResponseModel: Decodable { let values: [[String]] }
An example API response for the second AF request:
{
"range": "'App Control'!A4:V4",
"majorDimension": "ROWS",
"values": [
[
"Bob Jones",
"A1234",
"Cathy Jones",
"1234 N. Street St. City, State 12345"
]
]
}
I saw stuff about your own callback function as a function parameter (or something along those lines) to handle this, but I was completely lost. I also looked at Swift async/await, but I don't know how that works with callback functions. Xcode had the option to refactor user.authentication.do { authentication, error in to let authentication = try await user.authentication.do(), but it threw a missing parameter error (the closure it previously had).
EDIT: user.authentication.do also returns void--another reason the refactor didn't work (I think).
There is probably a much more elegant way to do all of this so excuse the possibly atrocious way I did it.
Here is the link to Google Identity Wrapper info.
Thanks in advance for your help.
Solved my own problem.
It appears (according to Apple's async/await intro video) that when you have an unsupported callback that you need to run asynchronously, you wrap it in something called a Continuation, which allows you to manually resume the function on the thread, whether throwing or returning.
So using that code allows you to run the Google Identity token refresh with async/await.
private static func auth(_ user: GIDGoogleUser) async throws -> GIDAuthentication? {
typealias AuthContinuation = CheckedContinuation<GIDAuthentication?, Error>
return try await withCheckedThrowingContinuation { (continuation: AuthContinuation) in
user.authentication.do { authentication, error in
if let error = error {
continuation.resume(throwing: error)
} else {
continuation.resume(returning: authentication)
}
}
}
}
static func search(user: GIDGoogleUser) async throws {
// some code
guard let authentication = try await auth(user) else { ... }
// some code
}
I then ran that before using Alamofire's built-in async/await functionality for each request (here's one).
let dataTask = AF.request(...).serializingDecodable(NameResponseModel.self)
let response = try await dataTask.value
return response.values[0]

Removing Swift RxAlamofire dependency

I'm trying to remove my dependency on RxAlamofire.
I currently have this function:
func requestData(_ urlRequest: URLRequestConvertible) -> Observable<(HTTPURLResponse, Data)> {
RxAlamofire.request(urlRequest).responseData()
}
How can I refactor this and use Alamofire directly to build and return an RxSwift Observable?
I suggest you look at the way the library wraps URLRequest to get an idea on how to do it...
Below is an abbreviated example from the library. In essence, you need to use Observable.create, make the network call passing in a closure that knows how to use the observer that create gives you.
Make sure you send a completed when done and make sure the disposable knows how to cancel the request.
Your Base will be something in Alamofire (I don't use Alamofire so I'm not sure what that might be.)
extension Reactive where Base: URLSession {
/**
Observable sequence of responses for URL request.
Performing of request starts after observer is subscribed and not after invoking this method.
**URL requests will be performed per subscribed observer.**
Any error during fetching of the response will cause observed sequence to terminate with error.
- parameter request: URL request.
- returns: Observable sequence of URL responses.
*/
public func response(request: URLRequest) -> Observable<(response: HTTPURLResponse, data: Data)> {
return Observable.create { observer in
let task = self.base.dataTask(with: request) { data, response, error in
guard let response = response, let data = data else {
observer.on(.error(error ?? RxCocoaURLError.unknown))
return
}
guard let httpResponse = response as? HTTPURLResponse else {
observer.on(.error(RxCocoaURLError.nonHTTPResponse(response: response)))
return
}
observer.on(.next((httpResponse, data)))
observer.on(.completed)
}
task.resume()
return Disposables.create(with: task.cancel)
}
}
}

Is it possible to use Soap API using Almofire in Swift 2.2

I am using both Rest & Soap API in my iOS application .For Rest API I can easily use Alamofire for both POST & GET method .But in Case of SOAP ,I am not able to handle the XML response .
Parse SOAP API using Alamofire and SWXMLHash Libraries easy to use for parsing : -
Swift 2.2
//MARK:- Parsing API here
func parseMyApi(is_URL: String, completion: (result: String) -> Void) {
Alamofire.request(.GET, is_URL)
.responseJSON { response in
let xmls = SWXMLHash.parse(response.data!)
func enumerate(indexer: XMLIndexer, level: Int) {
for child in indexer.children {
let name:String? = child.element!.name
print("\(level) \(name)")
// Take Link from XML data here
if name! == "link" {
let text = child.element!.text
if text?.isEmpty == false{
print(text)
// Finish here Process
completion(result: text!)
}
}
enumerate(child, level: level + 1)
}
}
enumerate(xmls, level: 0)
}
}
}
And you can see this example also for Soap parsing.

How to fetch data from SOAP WebService in Swift

I need to get data from SOAP WebService in Swift https://codedump.io/share/JDHdwpOOmqne/1/swift-2-soap-web-service-call
I tried the procedures in the referred link but it was not working,Can any one help wit a working code.
You can use SWXML library for get data from soap web service this is easy to use and simple see the code below.
Alamofire.request(.GET, is_URL)
.responseJSON { response in
let xmls = SWXMLHash.parse(response.data!)
func enumerate(indexer: XMLIndexer, level: Int) {
for child in indexer.children {
let name:String? = child.element!.name
print("\(level) \(name)")
// Take Link from XML data here
if name! == "link" {
let text = child.element!.text
if text?.isEmpty == false{
print(text)
// Finish here Process
completion(result: text!)
}
}
enumerate(child, level: level + 1)
}
}
enumerate(xmls, level: 0)
}
}
And i having demo link also for this related code for Soap parsing Demo