Skip to content

Commit c9cc881

Browse files
committed
actions and code updated for performance
1 parent 2c22cec commit c9cc881

File tree

13 files changed

+248
-156
lines changed

13 files changed

+248
-156
lines changed

actions/feedback/AddFeedback.swift

Lines changed: 79 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,103 @@
1-
/**
2-
* Add feedback to Cloudant NoSQL DB on IBM Cloud.
3-
*/
1+
/********************************************
2+
*** Add feedback to Cloudant NoSQL DB on IBM Cloud.
3+
********************************************/
44

55
import KituraNet
66
import Dispatch
77
import Foundation
8+
import SwiftyJSON
89

910
func main(args: [String:Any]) -> [String:Any] {
1011

1112
var str = ""
12-
var result:[String:Any] = [
13-
"cloudantId": str,
14-
"cloudantResult": str
13+
var subject = ""
14+
var cloudantBody = [String:String]()
15+
var cloudantBodyStr : String? = ""
16+
17+
var status:[String:Any] = [
18+
"ok": str,
19+
"response": str
1520
]
1621

17-
var resultGet: [String:Any] = [
18-
"document": str
22+
var body:[String:Any] = [
23+
"body": status
1924
]
2025

21-
guard let accessToken = args["_accessToken"] as? String,
22-
let idToken = args["_idToken"] as? String
26+
guard let accessToken = args["_accessToken"] as? String
2327
else{
24-
print("Unauthorized: Missing tokens")
25-
return result
28+
print("UNAUTHORIZED: Missing tokens")
29+
status["ERROR"] = "UNAUTHORIZED: Missing tokens"
30+
return status
2631
}
2732

28-
guard let cloudantBody = args["cloudantBody"] as? String,
29-
let cloudantId = args["cloudantId"] as? String,
30-
let cloudantDbName = args["cloudantDbName"] as? String else {
31-
32-
print("Error: missing a required parameter for writing a Cloudant document.")
33-
return result
33+
guard let message = args["message"] as? String
34+
else{
35+
print("ERROR: Input message missing")
36+
status["ERROR"] = "Input message missing"
37+
return status
3438
}
3539

40+
if let accessTokenFromString = accessToken.data(using: .utf8, allowLossyConversion: false) {
41+
let accessTokenJSON = JSON(data: accessTokenFromString)
42+
subject = accessTokenJSON["sub"].stringValue
43+
cloudantBody["subject"] = subject
44+
cloudantBody["message"] = message
45+
}
46+
47+
let cloudantDbName = "feedback"
48+
3649
let yourTargetUrl = URL(string: args["services.cloudant.url"] as! String)!
3750
let components = URLComponents(url: yourTargetUrl, resolvingAgainstBaseURL: false)!
38-
var requestOptions: [ClientRequest.Options] = [ .method("PUT"),
39-
.schema(components.scheme as String!),
40-
.hostname(components.host as String!),
41-
.username(components.user as String!),
42-
.password(components.password as String!),
43-
.port(443),
44-
.path("/\(cloudantDbName)/\(cloudantId)")
45-
]
46-
47-
var headers = [String:String]()
48-
headers["Accept"] = "application/json"
49-
headers["Content-Type"] = "application/json"
50-
requestOptions.append(.headers(headers))
51+
var requestOptions: [ClientRequest.Options] = [ .method("POST"),
52+
.schema(components.scheme as String!),
53+
.hostname(components.host as String!),
54+
.username(components.user as String!),
55+
.password(components.password as String!),
56+
.port(443),
57+
.path("/\(cloudantDbName)")
58+
]
59+
60+
var headers = [String:String]()
61+
headers["Accept"] = "application/json"
62+
headers["Content-Type"] = "application/json"
63+
requestOptions.append(.headers(headers))
64+
65+
let cloudantJSON = JSON(cloudantBody)
66+
if let rawString = cloudantJSON.rawString() {
67+
cloudantBodyStr = rawString
68+
} else {
69+
print("ERROR: json.rawString is nil")
70+
}
71+
72+
if (cloudantBodyStr == "") {
73+
str = "Error: Unable to serialize cloudantBody parameter as a String instance"
74+
status["ERROR"] = str
75+
return status
76+
}
77+
else {
78+
let requestData:Data? = cloudantBodyStr!.data(using: String.Encoding.utf8, allowLossyConversion: true)
79+
80+
if let data = requestData {
5181

52-
if (cloudantBody == "") {
53-
str = "Error: Unable to serialize cloudantBody parameter as a String instance"
54-
}
55-
else {
56-
let requestData:Data? = cloudantBody.data(using: String.Encoding.utf8, allowLossyConversion: true)
57-
58-
if let data = requestData {
59-
60-
let req = HTTP.request(requestOptions) { response in
61-
do {
62-
if let responseUnwrapped = response,
63-
let responseStr = try responseUnwrapped.readString() {
64-
str = responseStr
65-
}
66-
} catch {
67-
print("Error \(error)")
68-
}
82+
let req = HTTP.request(requestOptions) { response in
83+
do {
84+
if let responseUnwrapped = response,
85+
let responseStr = try responseUnwrapped.readString() {
86+
str = responseStr
6987
}
70-
req.end(data);
88+
} catch {
89+
print("Error \(error)")
7190
}
7291
}
73-
74-
result = [
75-
"cloudantId": cloudantId,
76-
"cloudantResult": str
77-
]
78-
79-
80-
return result
92+
req.end(data);
93+
}
94+
}
95+
status = [
96+
"ok": "true",
97+
"response": str
98+
]
99+
body = [
100+
"body": status
101+
]
102+
return body
81103
}

actions/feedback/AddFeedback.zip

1.72 MB
Binary file not shown.

actions/feedback/AnalyzeFeedback.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**********
22
* Analyze the provided feedback using IBM Watson Tone Analyzer
33
* Service on IBM Cloud.
4-
***********/
4+
***********/
55
import Foundation
66
import Dispatch
77
import KituraNet
@@ -145,4 +145,3 @@ func main(args: [String:Any]) -> [String:Any] {
145145
]
146146
return response
147147
}
148-
1.72 MB
Binary file not shown.

actions/users/AddUser.swift

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,62 @@
1-
/**
2-
* Add User to Cloudant NoSQL DB
3-
*/
1+
/*******************************
2+
**** Add User to Cloudant NoSQL DB
3+
******************************/
44

55
import KituraNet
66
import Dispatch
77
import Foundation
8+
import SwiftyJSON
89

910
func main(args: [String:Any]) -> [String:Any] {
1011

1112
var str = ""
12-
var result:[String:Any] = [
13-
"cloudantId": str,
14-
"cloudantResult": str
13+
var subject = ""
14+
var name = "Guest"
15+
var deviceId = ""
16+
var picture = ""
17+
var email = ""
18+
var cloudantBody = [String:String]()
19+
var cloudantBodyStr : String? = ""
20+
21+
var status:[String:Any] = [
22+
"ok": str,
23+
"response": str
1524
]
1625

17-
var resultGet: [String:Any] = [
18-
"document": str
26+
var body:[String:Any] = [
27+
"body": status
1928
]
2029

2130
guard let accessToken = args["_accessToken"] as? String,
2231
let idToken = args["_idToken"] as? String
2332
else{
24-
print("Unauthorized: Missing tokens")
25-
return result
33+
print("Unauthorized ERROR: Missing tokens")
34+
status["ERROR"] = "Unauthorized ERROR: Missing tokens"
35+
return status
2636
}
2737

28-
guard let cloudantBody = args["cloudantBody"] as? String,
29-
let cloudantId = args["cloudantId"] as? String,
30-
let cloudantDbName = args["cloudantDbName"] as? String else {
31-
32-
print("Error: missing a required parameter for writing a Cloudant document.")
33-
return result
38+
if let accessTokenFromString = accessToken.data(using: .utf8, allowLossyConversion: false) {
39+
let accessTokenJSON = JSON(data: accessTokenFromString)
40+
subject = accessTokenJSON["sub"].stringValue
41+
cloudantBody["subject"] = subject
42+
}
43+
44+
if let idTokenFromString = idToken.data(using: .utf8, allowLossyConversion: false) {
45+
let idTokenJSON = JSON(data: idTokenFromString)
46+
name = idTokenJSON["name"].stringValue.isEmpty ? "Guest" : idTokenJSON["name"].stringValue
47+
deviceId = idTokenJSON["oauth_client"]["device_id"].stringValue
48+
cloudantBody["name"] = name
49+
cloudantBody["deviceid"] = deviceId
50+
if(idTokenJSON["amr"][0].stringValue != "appid_anon")
51+
{
52+
picture = idTokenJSON["picture"].stringValue
53+
email = idTokenJSON["email"].stringValue
54+
cloudantBody["picture"] = picture
55+
cloudantBody["email"] = email
56+
}
3457
}
3558

59+
let cloudantDbName = "users"
3660
let yourTargetUrl = URL(string: args["services.cloudant.url"] as! String)!
3761
let components = URLComponents(url: yourTargetUrl, resolvingAgainstBaseURL: false)!
3862

@@ -42,7 +66,7 @@ func main(args: [String:Any]) -> [String:Any] {
4266
.username(components.user as String!),
4367
.password(components.password as String!),
4468
.port(443),
45-
.path("/\(cloudantDbName)/\(cloudantId)")
69+
.path("/\(cloudantDbName)/\(subject)")
4670
]
4771

4872
var headers = [String: String]()
@@ -58,12 +82,10 @@ func main(args: [String:Any]) -> [String:Any] {
5882
}
5983
} catch {
6084
print("Error: \(error)")
85+
status["Error"] = error
6186
}
6287
}
6388
req.end()
64-
/*resultGet = [
65-
"document": str
66-
]*/
6789

6890
if let data = str.data(using: String.Encoding.utf8)
6991
{
@@ -76,20 +98,27 @@ func main(args: [String:Any]) -> [String:Any] {
7698
.username(components.user as String!),
7799
.password(components.password as String!),
78100
.port(443),
79-
.path("/\(cloudantDbName)/\(cloudantId)")
101+
.path("/\(cloudantDbName)/\(subject)")
80102
]
81103

82104
var headers = [String:String]()
83105
headers["Accept"] = "application/json"
84106
headers["Content-Type"] = "application/json"
85107
requestOptions.append(.headers(headers))
108+
let cloudantJSON = JSON(cloudantBody)
109+
if let rawString = cloudantJSON.rawString() {
110+
cloudantBodyStr = rawString
111+
} else {
112+
print("ERROR: cloudantJSON.rawString is nil")
113+
}
86114

87-
88-
if (cloudantBody == "") {
115+
if (cloudantBodyStr == "") {
89116
str = "Error: Unable to serialize cloudantBody parameter as a String instance"
117+
status["Error"] = str
118+
return status
90119
}
91120
else {
92-
let requestData:Data? = cloudantBody.data(using: String.Encoding.utf8, allowLossyConversion: true)
121+
let requestData:Data? = cloudantBodyStr!.data(using: String.Encoding.utf8, allowLossyConversion: true)
93122

94123
if let data = requestData {
95124

@@ -101,18 +130,26 @@ func main(args: [String:Any]) -> [String:Any] {
101130
}
102131
} catch {
103132
print("Error \(error)")
133+
status["Error"] = error
104134
}
105135
}
106136
req.end(data);
107137
}
108138
}
109-
110-
result = [
111-
"cloudantId": cloudantId,
112-
"cloudantResult": str
139+
status = [
140+
"ok": "true",
141+
"response": str
113142
]
114143
}
144+
145+
else{
146+
status["ERROR"] = "User already exists"
147+
148+
}
149+
115150
}
116-
117-
return result
151+
body = [
152+
"body": status
153+
]
154+
return body
118155
}

actions/users/AddUser.zip

1.72 MB
Binary file not shown.

actions/users/PrepareUserNotification.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func main(args: [String:Any]) -> [String:Any] {
2323
let message = args["message"] as? String
2424
else {
2525

26-
print("Error: missing a required parameter for writing a Cloudant document.")
26+
print("Error: missing a required parameter.")
2727
return responseData
2828
}
2929

1.71 MB
Binary file not shown.

0 commit comments

Comments
 (0)