Skip to content

Commit

Permalink
adding not found to the get request
Browse files Browse the repository at this point in the history
  • Loading branch information
Ondrej Rafaj authored and Ondrej Rafaj committed Dec 18, 2016
1 parent 9732d11 commit ffb5e75
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Sources/S3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public enum Error: Swift.Error {
case missingBucketName
case badStringData
case missingData
case notFound
}


Expand Down Expand Up @@ -194,19 +195,26 @@ public class S3 {
*/
public func get(fileAtPath filePath: String, bucketName: String? = nil) throws -> Data {
let fileUrl: URL? = try self.buildUrl(bucketName: bucketName, fileName: filePath)

guard let url = fileUrl else {
throw Error.invalidUrl
}

let headers: [String: String] = try signer.authHeaderV4(httpMethod: .get, urlString: url.absoluteString, headers: [:], payload: .none)
let result: Response = try BasicClient.get(fileUrl!.absoluteString, headers: self.vaporHeaders(headers))

if result.status == .notFound {
throw Error.notFound
}

guard result.status == .ok else {
throw Error.badResponse(result)
}

guard let bytes: Bytes = result.body.bytes else {
throw Error.missingData
}

let data: Data = Data.init(bytes: bytes)

return data
Expand Down

0 comments on commit ffb5e75

Please sign in to comment.