Skip to content

Commit a3943f0

Browse files
ShihabMehboobmattcolegate
authored andcommitted
Removed SwiftyRequest (#204)
* Removed SwiftyRequest * Minor adjustments
1 parent 6c5513d commit a3943f0

File tree

3 files changed

+52
-28
lines changed

3 files changed

+52
-28
lines changed

Package.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,13 @@ let package = Package(
4545
dependencies: [
4646
.package(url: "https://github.com/IBM-Swift/Kitura.git", from: "2.3.0"),
4747
webSocketPackage,
48-
.package(url: "https://github.com/IBM-Swift/SwiftyRequest.git", from: "1.0.0"),
4948
.package(url: "https://github.com/IBM-Swift/Swift-cfenv.git", from: "6.0.0"),
5049
.package(url: "https://github.com/RuntimeTools/omr-agentcore", .exact("3.2.4-swift4")),
5150
],
5251
targets: [
5352
.target(name: "SwiftMetrics", dependencies: ["agentcore", "hcapiplugin", "envplugin", "cpuplugin", "memplugin", "CloudFoundryEnv"]),
5453
.target(name: "SwiftMetricsKitura", dependencies: ["SwiftMetrics", "Kitura"]),
55-
.target(name: "SwiftBAMDC", dependencies: ["SwiftMetricsKitura", "SwiftyRequest", "Kitura-WebSocket"]),
54+
.target(name: "SwiftBAMDC", dependencies: ["SwiftMetricsKitura", "Kitura-WebSocket"]),
5655
.target(name: "SwiftMetricsBluemix", dependencies: ["SwiftMetricsKitura","SwiftBAMDC"]),
5756
.target(name: "SwiftMetricsDash", dependencies: ["SwiftMetricsBluemix"]),
5857
.target(name: "SwiftMetricsREST", dependencies: ["SwiftMetricsKitura"]),

Sources/SwiftBAMDC/SwiftMetricsBAMConfig.swift

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import LoggerAPI
2424
import Cryptor
2525
import Foundation
2626
import Dispatch
27-
import SwiftyRequest
2827
import Configuration
2928

3029
/*
@@ -526,7 +525,7 @@ public class BMConfig : IBAMConfig {
526525

527526
private static func makeKituraHttpRequest(apmData: Dictionary<String,Any>, urlString: String, reqType: String, headers: [String:String], taskCallback: @escaping (Bool, Int, Any?) -> () = doNothing) {
528527

529-
if(urlString == "") {
528+
guard let url = URL(string: urlString) else {
530529
Log.warning("[SwiftMetricsBAMConfig] IngressURL is not set")
531530
return
532531
}
@@ -541,21 +540,23 @@ public class BMConfig : IBAMConfig {
541540
headerCopy["X-TransactionId"] = UUID().uuidString.lowercased()
542541
}*/
543542

544-
var method = HTTPMethod.put
543+
var method = "PUT"
545544
if(reqType.uppercased() == "GET") {
546-
method = HTTPMethod.get
545+
method = "GET"
547546
}
548547

549-
let request = RestRequest(method: method, url: urlString)
550-
548+
var request = URLRequest(url: url)
549+
request.httpMethod = method
551550

552551
//TODO: don't log token in headers, temporarily changed to info
553552
//Log.info("Initiating http request Headers: \(headerCopy) APMData: \(apmData)")
554553
Log.debug("[SwiftMetricsBAMConfig] Initiating http request Headers: \(headers) APMData: \(apmData)")
555554

556-
request.messageBody = jsonData
557-
request.headerParameters = headers
558-
request.response(completionHandler: { (data, response, error) in
555+
request.httpBody = jsonData
556+
for (key, val) in headers {
557+
request.setValue(val, forHTTPHeaderField: key)
558+
}
559+
let task = URLSession.shared.dataTask(with: request) { data, response, error in
559560

560561
if let e = error {
561562
//client side error
@@ -570,26 +571,31 @@ public class BMConfig : IBAMConfig {
570571
//var result: String = NSString (data: receivedData, encoding: String.Encoding.utf8.rawValue)
571572
let json = try? JSONSerialization.jsonObject(with: receivedData, options: [])
572573

573-
switch (httpResponse.statusCode) {
574+
guard let httpURLResponse = httpResponse as? HTTPURLResponse else {
575+
Log.error("[SwiftMetricsBAMConfig] Failed to convert response to HTTPURLResponse")
576+
taskCallback(false, -1, nil)
577+
return
578+
}
579+
switch httpURLResponse.statusCode {
574580

575581
case 200...299:
576582

577583
// Temporarily put to info as static method is disabling it, will debug later
578-
Log.debug("[SwiftMetricsBAMConfig] \(String(describing:request.method)) successful: StatusCode: \(httpResponse.statusCode) Response: \(String(describing:response)), JSON: \(String(describing:json))")
584+
Log.debug("[SwiftMetricsBAMConfig] \(String(describing:request.httpMethod)) successful: StatusCode: \(httpURLResponse.statusCode) Response: \(String(describing:response)), JSON: \(String(describing:json))")
579585

580-
taskCallback(true, httpResponse.statusCode, json as Any?)
586+
taskCallback(true, httpURLResponse.statusCode, json as Any?)
581587

582588
default:
583-
Log.error("[SwiftMetricsBAMConfig] \(String(describing:request.method)) request got response \(httpResponse.statusCode) and response \(httpResponse)")
584-
taskCallback(false, httpResponse.statusCode, receivedData as Any?)
589+
Log.error("[SwiftMetricsBAMConfig] \(String(describing:request.httpMethod)) request got response \(httpURLResponse.statusCode) and response \(httpResponse)")
590+
taskCallback(false, httpURLResponse.statusCode, receivedData as Any?)
585591
}
586592
}
587593
else {
588594
Log.error("[SwiftMetricsBAMConfig] Error sending data: URL: \(urlString) : Response: \(String(describing:data)), Error: \(String(describing:error))")
589595
taskCallback(false, -1, nil)
590596
}
591597
}
592-
)
598+
task.resume()
593599

594600
} catch {
595601
Log.warning("[SwiftMetricsBAMConfig] Kitura request failed: \(error.localizedDescription)")

Sources/SwiftMetricsBluemix/SwiftMetricsBluemix.swift

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import Dispatch
1919
import LoggerAPI
2020
import Configuration
2121
import CloudFoundryEnv
22-
import SwiftyRequest
2322
import SwiftMetrics
2423
import SwiftMetricsKitura
2524
import SwiftBAMDC
@@ -386,34 +385,47 @@ public class SwiftMetricsBluemix {
386385
Log.debug("[Auto-scaling Agent] Attempting to send metrics to \(sendMetricsPath)")
387386

388387

389-
let request = RestRequest(method: .post, url: sendMetricsPath)
390-
request.headerParameters = ["Content-Type":"application/json", "Authorization":"Basic \(authorization)"]
388+
guard let url = URL(string: sendMetricsPath) else {
389+
Log.error("[Auto-scaling Agent] Invalid URL")
390+
return
391+
}
392+
var request = URLRequest(url: url)
393+
request.httpMethod = "POST"
394+
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
395+
request.setValue("Basic \(authorization)", forHTTPHeaderField: "Authorization")
391396
do {
392-
request.messageBody = try JSONSerialization.data(withJSONObject: asOBJ, options: .prettyPrinted)
397+
request.httpBody = try JSONSerialization.data(withJSONObject: asOBJ, options: .prettyPrinted)
393398
} catch {
394399
print("Error converting input to JSON object to post.")
395400
}
396-
request.response { (data, response, error) in
401+
let task = URLSession.shared.dataTask(with: request) { data, response, error in
397402
Log.debug("[Auto-scaling Agent] sendMetrics:Request: \(String(describing:request))")
398403
Log.debug("[Auto-scaling Agent] sendMetrics:Response: \(String(describing:response))")
399404
Log.debug("[Auto-scaling Agent] sendMetrics:Data: \(String(describing:data))")
400405
Log.debug("[Auto-scaling Agent] sendMetrics:Error: \(String(describing: error))")
401406
}
407+
task.resume()
402408

403409
}
404410

405411
private func notifyStatus() {
406412
let notifyStatusPath = "\(host):443/services/agent/status/\(appID)"
407413
Log.debug("[Auto-scaling Agent] Attempting notifyStatus request to \(notifyStatusPath)")
408414

409-
let request = RestRequest(method: .put, url: notifyStatusPath)
410-
request.headerParameters = ["Authorization":"Basic \(authorization)"]
411-
request.response { (data, response, error) in
415+
guard let url = URL(string: notifyStatusPath) else {
416+
Log.error("[Auto-scaling Agent] Invalid URL")
417+
return
418+
}
419+
var request = URLRequest(url: url)
420+
request.httpMethod = "PUT"
421+
request.setValue("Basic \(authorization)", forHTTPHeaderField: "Authorization")
422+
let task = URLSession.shared.dataTask(with: request) { data, response, error in
412423
Log.debug("[Auto-scaling Agent] notifyStatus:Request: \(String(describing:request))")
413424
Log.debug("[Auto-scaling Agent] notifyStatus:Response: \(String(describing:response))")
414425
Log.debug("[Auto-scaling Agent] notifyStatus:Data: \(String(describing: data))")
415426
Log.debug("[Auto-scaling Agent] notifyStatus:Error: \(String(describing: error))")
416427
}
428+
task.resume()
417429

418430
}
419431

@@ -423,9 +435,15 @@ public class SwiftMetricsBluemix {
423435
let refreshConfigPath = "\(host):443/v1/agent/config/\(serviceID)/\(appID)?appType=swift"
424436
Log.debug("[Auto-scaling Agent] Attempting requestConfig request to \(refreshConfigPath)")
425437

426-
let request = RestRequest(method: .get, url: refreshConfigPath)
427-
request.headerParameters = ["Content-Type":"application/json", "Authorization":"Basic \(authorization)"]
428-
request.response { (data, response, error) in
438+
guard let url = URL(string: refreshConfigPath) else {
439+
Log.error("[Auto-scaling Agent] Invalid URL")
440+
return
441+
}
442+
var request = URLRequest(url: url)
443+
request.httpMethod = "GET"
444+
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
445+
request.setValue("Basic \(authorization)", forHTTPHeaderField: "Authorization")
446+
let task = URLSession.shared.dataTask(with: request) { data, response, error in
429447
Log.debug("[Auto-scaling Agent] requestConfig:Request: \(String(describing:request))")
430448
Log.debug("[Auto-scaling Agent] requestConfig:Response: \(String(describing:response))")
431449
Log.debug("[Auto-scaling Agent] requestConfig:Data: \(String(describing: data))")
@@ -437,6 +455,7 @@ public class SwiftMetricsBluemix {
437455
print("Error updating configuration. Data doesn't exist.")
438456
}
439457
}
458+
task.resume()
440459

441460
}
442461

0 commit comments

Comments
 (0)