Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit 34574b7

Browse files
authored
Merge pull request #3 from nodes-vapor/vapor-engine
Replacing cURL with vapor/engine
2 parents 5deb434 + 919466c commit 34574b7

File tree

9 files changed

+101
-64
lines changed

9 files changed

+101
-64
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
*.xcodeproj
3+
Packages/

.travis.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
os:
2+
- linux
3+
language: generic
4+
sudo: required
5+
dist: trusty
6+
script:
7+
- eval "$(curl -sL https://swift.vapor.sh/ci)"

Package.swift

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import PackageDescription
22

33
let package = Package(
4-
name: "AWS",
5-
targets: [
6-
Target(name: "AWS"),
7-
Target(name: "EC2", dependencies: ["AWS"]),
8-
Target(name: "S3", dependencies: ["AWS"])
9-
],
10-
dependencies: [
11-
.Package(url: "https://github.com/vapor/crypto.git", majorVersion: 1),
12-
]
13-
)
4+
name: "AWS",
5+
/*targets: [
6+
Target(name: "AWS"),
7+
Target(name: "EC2", dependencies: ["AWS"]),
8+
Target(name: "S3", dependencies: ["AWS"])
9+
],*/
10+
dependencies: [
11+
.Package(url: "https://github.com/vapor/crypto.git", majorVersion: 1),
12+
.Package(url: "https://github.com/vapor/engine.git", majorVersion: 1)
13+
]
14+
)

Sources/AWS/AWSApi/CallAWS.swift

+32-24
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1-
import Foundation
2-
import Console
1+
import HTTP
2+
import Transport
33

44
class CallAWS {
5-
let console: ConsoleProtocol = Terminal(arguments: CommandLine.arguments)
6-
7-
public func call(method: String, service: String, host: String, region: String, baseURL: String, key: String, secret: String, requestParam: String) throws -> String {
5+
enum Error: Swift.Error {
6+
case unsupported(String)
7+
}
8+
9+
public func call(
10+
method: Authentication.Method,
11+
service: String,
12+
host: String,
13+
region: String,
14+
baseURL: String,
15+
key: String,
16+
secret: String,
17+
requestParam: String
18+
) throws -> Response {
819
let headers = Authentication(
920
method: method,
1021
service: service,
@@ -13,24 +24,21 @@ class CallAWS {
1324
baseURL: baseURL,
1425
key: key,
1526
secret: secret,
16-
requestParam: requestParam).getAWSHeaders()
17-
18-
let header = headers.joined(separator: " ")
19-
20-
print("curl -X \(method) \(header) \(baseURL)?\(requestParam)")
21-
22-
do {
23-
let output = try console.backgroundExecute(program: "/bin/sh", arguments: [
24-
"-c",
25-
"curl -X \(method) \(header) \(baseURL)?\(requestParam)"
26-
27-
])
28-
29-
return output
30-
} catch ConsoleError.backgroundExecute(_, let message, _) {
31-
console.error(message.string)
27+
requestParam: requestParam
28+
).getAWSHeaders()
29+
30+
let response: Response
31+
switch method {
32+
case .get:
33+
response = try BasicClient.get(
34+
"\(baseURL)?\(requestParam)",
35+
headers: headers
36+
)
37+
38+
case .post:
39+
throw Error.unsupported("POST")
3240
}
33-
34-
return ""
41+
42+
return response
3543
}
36-
}
44+
}

Sources/AWS/Authentication/Authentication.swift

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
import Foundation
2-
import HMAC
1+
import Core
32
import Hash
3+
import HMAC
4+
import HTTP
45
import Essentials
5-
import Core
6+
import Foundation
67

78
class Authentication {
8-
9-
let method: String
9+
enum Method {
10+
case get
11+
case post
12+
}
13+
14+
let method: Method
1015
let service: String
1116
let host: String
1217
let region: String
@@ -16,7 +21,7 @@ class Authentication {
1621
let secret: String
1722
let requestParam: String!
1823

19-
public init(method: String, service: String, host: String, region: String, baseURL: String, key: String, secret: String, requestParam: String!) {
24+
public init(method: Method, service: String, host: String, region: String, baseURL: String, key: String, secret: String, requestParam: String!) {
2025
self.method = method
2126
self.service = service
2227
self.host = host
@@ -84,11 +89,12 @@ class Authentication {
8489
return "\(algorithm) Credential=\(self.key)/\(credentialScope), SignedHeaders=host;x-amz-date, Signature=\(signature)"
8590
}
8691

87-
public func getAWSHeaders() -> Array<String> {
92+
public func getAWSHeaders() -> [HeaderKey : String] {
8893
amzDateHeader()
89-
90-
return ["-H \"Authorization: \(authorizationHeader())\"",
91-
"-H \"x-amz-date: \(amzDate)\""]
94+
return [
95+
"Authorization": authorizationHeader(),
96+
"x-amz-date": amzDate
97+
]
9298
}
9399

94100
public func amzDateHeader() {
@@ -116,4 +122,4 @@ extension Data {
116122
func hexEncodedString() -> String {
117123
return map { String(format: "%02hhx", $0) }.joined()
118124
}
119-
}
125+
}

Sources/AWS/EC2/EC2.swift

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Foundation
22

33
class EC2 {
4-
54
let accessKey: String
65
let secretKey: String
76
let region: String
@@ -19,20 +18,18 @@ class EC2 {
1918
}
2019

2120
public func describeInstances() throws -> String {
22-
do {
23-
return try CallAWS().call(
24-
method: "GET",
25-
service: self.service,
26-
host: self.host,
27-
region: self.region,
28-
baseURL: self.baseURL,
29-
key: self.accessKey,
30-
secret: self.secretKey,
31-
requestParam: "Action=DescribeRegions&Version=2015-10-01")
32-
} catch {
33-
34-
}
35-
36-
return ""
21+
//TODO(Brett): wrap this result in a model instead of a string type
22+
let response = try CallAWS().call(
23+
method: .get,
24+
service: service,
25+
host: host,
26+
region: region,
27+
baseURL: baseURL,
28+
key: accessKey,
29+
secret: secretKey,
30+
requestParam: "Action=DescribeRegions&Version=2015-10-01"
31+
)
32+
33+
return response.description
3734
}
3835
}

Sources/AWS/S3/S3.swift

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import Foundation
2-
import Console
32

43
class S3 {
5-
let console: ConsoleProtocol = Terminal(arguments: CommandLine.arguments)
6-
74
let accessKey: String
85
let keySecret: String
96
let region: String

Tests/AWSTests/AWSTests.swift

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import XCTest
2+
@testable import AWS
3+
4+
class AWSTests: XCTestCase {
5+
static var allTests = [
6+
("testExample", testExample)
7+
]
8+
9+
func testExample() {
10+
XCTAssertEqual(2+2, 4)
11+
}
12+
}

Tests/LinuxMain.swift

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import XCTest
2+
@testable import AWSTests
3+
4+
XCTMain([
5+
testCase(AWSTests.allTests)
6+
])

0 commit comments

Comments
 (0)