Skip to content

Commit

Permalink
Merge pull request #16 from nerdishbynature/swift-3.0
Browse files Browse the repository at this point in the history
Swift 3.0
  • Loading branch information
pietbrauer committed Sep 20, 2016
2 parents 19e4c72 + e4521fa commit c8981a3
Show file tree
Hide file tree
Showing 15 changed files with 203 additions and 185 deletions.
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "nerdishbynature/RequestKit" ~>1.1
github "nerdishbynature/RequestKit" ~>2.0
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "nerdishbynature/RequestKit" "1.1.0"
github "nerdishbynature/RequestKit" "2.0.0"
4 changes: 2 additions & 2 deletions TanukiKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "TanukiKit"
s.version = "0.3.1"
s.version = "0.4.0"
s.summary = "A Swift API Client for GitLab CE/EE"
s.description = <<-DESC
You are looking at the A Swift API Client for GitLabe CE/EE.
Expand All @@ -12,7 +12,7 @@ Pod::Spec.new do |s|
s.source = { :git => "https://github.com/nerdishbynature/tanukikit.git", :tag => s.version.to_s }
s.social_media_url = "https://twitter.com/pietbrauer"
s.module_name = "TanukiKit"
s.dependency "NBNRequestKit", "~> 1.1"
s.dependency "NBNRequestKit", "~> 2.0"
s.requires_arc = true
s.source_files = "TanukiKit/*.swift"
s.ios.deployment_target = '8.0'
Expand Down
24 changes: 17 additions & 7 deletions TanukiKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -713,8 +713,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand Down Expand Up @@ -744,7 +746,7 @@
PRODUCT_NAME = TanukiKit;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand All @@ -764,8 +766,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand All @@ -787,7 +791,8 @@
PRODUCT_BUNDLE_IDENTIFIER = com.nerdishbynature.TanukiKit;
PRODUCT_NAME = TanukiKit;
SDKROOT = iphoneos;
SWIFT_VERSION = 2.3;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -799,6 +804,7 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -819,6 +825,7 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -831,7 +838,6 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
};
name = Release;
};
Expand All @@ -848,7 +854,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.nerdishbynature.TanukiKitTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -865,14 +871,15 @@
PRODUCT_BUNDLE_IDENTIFIER = com.nerdishbynature.TanukiKitTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Release;
};
23A4CE011C7C6BD700261CFB /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
"CODE_SIGN_IDENTITY[sdk=watchos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -895,6 +902,7 @@
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
"CODE_SIGN_IDENTITY[sdk=watchos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -917,6 +925,7 @@
23A4CE211C7C6BF700261CFB /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
"CODE_SIGN_IDENTITY[sdk=appletvos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -938,6 +947,7 @@
23A4CE221C7C6BF700261CFB /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
"CODE_SIGN_IDENTITY[sdk=appletvos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand Down Expand Up @@ -993,7 +1003,7 @@
23A4CE461C7C6C5600261CFB /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_IDENTITY = "";
COMBINE_HIDPI_IMAGES = YES;
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
Expand All @@ -1016,7 +1026,7 @@
23A4CE471C7C6C5600261CFB /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_IDENTITY = "";
COMBINE_HIDPI_IMAGES = YES;
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
Expand Down
83 changes: 45 additions & 38 deletions TanukiKit/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,29 @@ public struct OAuthConfiguration: Configuration {
self.redirectURI = redirectURI
}

public func authenticate() -> NSURL? {
return OAuthRouter.Authorize(self, redirectURI).URLRequest?.URL
public func authenticate() -> URL? {
return OAuthRouter.authorize(self, redirectURI).URLRequest?.url
}

public func authorize(session: RequestKitURLSession = NSURLSession.sharedSession(), code: String, completion: (config: TokenConfiguration) -> Void) {
let request = OAuthRouter.AccessToken(self, code, redirectURI).URLRequest
public func authorize(session: RequestKitURLSession = URLSession.shared, code: String, completion: @escaping (_ config: TokenConfiguration) -> Void) {
let request = OAuthRouter.accessToken(self, code, redirectURI).URLRequest
if let request = request {
let task = session.dataTaskWithRequest(request) { data, response, err in
if let response = response as? NSHTTPURLResponse {
let task = session.dataTask(with: request) { data, response, err in
if let response = response as? HTTPURLResponse {
if response.statusCode != 200 {
return
} else {
if let data = data, json = try? NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments), accessToken = json["access_token"] as? String {
let config = TokenConfiguration(accessToken, url: self.apiEndpoint)
completion(config: config)
guard let data = data else {
return
}
do {
let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any]
if let json = json, let accessToken = json["access_token"] as? String {
let config = TokenConfiguration(accessToken, url: self.apiEndpoint)
completion(config)
}
} catch {
return
}
}
}
Expand All @@ -71,72 +79,71 @@ public struct OAuthConfiguration: Configuration {
}
}

public func handleOpenURL(session: RequestKitURLSession = NSURLSession.sharedSession(), url: NSURL, completion: (config: TokenConfiguration) -> Void) {
let absoluteString: String? = url.absoluteString
if let code = absoluteString?.componentsSeparatedByString("=").last {
authorize(session, code: code) { (config) in
completion(config: config)
public func handleOpenURL(_ session: RequestKitURLSession = URLSession.shared, url: URL, completion: @escaping (_ config: TokenConfiguration) -> Void) {
if let code = url.absoluteString.components(separatedBy: "=").last {
authorize(session: session, code: code) { (config) in
completion(config)
}
}
}
}

enum OAuthRouter: Router {
case Authorize(OAuthConfiguration, String)
case AccessToken(OAuthConfiguration, String, String)
case authorize(OAuthConfiguration, String)
case accessToken(OAuthConfiguration, String, String)

var configuration: Configuration {
switch self {
case .Authorize(let config, _): return config
case .AccessToken(let config, _, _): return config
case .authorize(let config, _): return config
case .accessToken(let config, _, _): return config
}
}

var method: HTTPMethod {
switch self {
case .Authorize:
case .authorize:
return .GET
case .AccessToken:
case .accessToken:
return .POST
}
}

var encoding: HTTPEncoding {
switch self {
case .Authorize:
return .URL
case .AccessToken:
return .FORM
case .authorize:
return .url
case .accessToken:
return .form
}
}

var path: String {
switch self {
case .Authorize:
case .authorize:
return "oauth/authorize"
case .AccessToken:
case .accessToken:
return "oauth/token"
}
}

var params: [String: AnyObject] {
var params: [String: Any] {
switch self {
case .Authorize(let config, let redirectURI):
return ["client_id": config.token, "response_type": "code", "redirect_uri": redirectURI]
case .AccessToken(let config, let code, let rediredtURI):
return ["client_id": config.token, "client_secret": config.secret, "code": code, "grant_type": "authorization_code", "redirect_uri": rediredtURI]
case .authorize(let config, let redirectURI):
return ["client_id": config.token as AnyObject, "response_type": "code" as AnyObject, "redirect_uri": redirectURI as AnyObject]
case .accessToken(let config, let code, let rediredtURI):
return ["client_id": config.token as AnyObject, "client_secret": config.secret as AnyObject, "code": code as AnyObject, "grant_type": "authorization_code" as AnyObject, "redirect_uri": rediredtURI as AnyObject]
}
}

var URLRequest: NSURLRequest? {
var URLRequest: Foundation.URLRequest? {
switch self {
case .Authorize(let config, _):
let url = NSURL(string: path, relativeToURL: NSURL(string: config.webEndpoint))
let components = NSURLComponents(URL: url!, resolvingAgainstBaseURL: true)
case .authorize(let config, _):
let url = URL(string: path, relativeTo: URL(string: config.webEndpoint)!)
let components = URLComponents(url: url!, resolvingAgainstBaseURL: true)
return request(components!, parameters: params)
case .AccessToken(let config, _, _):
let url = NSURL(string: path, relativeToURL: NSURL(string: config.webEndpoint))
let components = NSURLComponents(URL: url!, resolvingAgainstBaseURL: true)
case .accessToken(let config, _, _):
let url = URL(string: path, relativeTo: URL(string: config.webEndpoint)!)
let components = URLComponents(url: url!, resolvingAgainstBaseURL: true)
return request(components!, parameters: params)
}
}
Expand Down
28 changes: 14 additions & 14 deletions TanukiKit/Keys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,54 @@ import RequestKit
// MARK: request

public extension TanukiKit {
public func postPublicKey(session: RequestKitURLSession = NSURLSession.sharedSession(), publicKey: String, title: String, completion: (response:Response<String>) -> Void) {
let router = PublicKeyRouter.PostPublicKey(publicKey, title, configuration)
router.loadJSON(session, expectedResultType: [String: AnyObject].self) { json, error in
public func postPublicKey(_ session: RequestKitURLSession = URLSession.shared, publicKey: String, title: String, completion: @escaping (_ response:Response<String>) -> Void) -> URLSessionDataTaskProtocol? {
let router = PublicKeyRouter.postPublicKey(publicKey, title, configuration)
return router.loadJSON(session, expectedResultType: [String: AnyObject].self) { json, error in
if let error = error {
completion(response: Response.Failure(error))
completion(Response.failure(error))
} else {
if let _ = json {
completion(response: Response.Success(publicKey))
completion(Response.success(publicKey))
}
}
}
}
}

enum PublicKeyRouter: Router {
case PostPublicKey(String, String, Configuration)
case postPublicKey(String, String, Configuration)

var configuration: Configuration {
switch self {
case .PostPublicKey(_, _, let config): return config
case .postPublicKey(_, _, let config): return config
}
}

var method: HTTPMethod {
switch self {
case .PostPublicKey:
case .postPublicKey:
return .POST
}
}

var encoding: HTTPEncoding {
switch self {
case .PostPublicKey:
return .FORM
case .postPublicKey:
return .form
}
}

var path: String {
switch self {
case .PostPublicKey:
case .postPublicKey:
return "user/keys"
}
}

var params: [String: AnyObject] {
var params: [String: Any] {
switch self {
case .PostPublicKey(let publicKey, let title, _):
return ["title": title, "key": publicKey]
case .postPublicKey(let publicKey, let title, _):
return ["title": title as AnyObject, "key": publicKey as AnyObject]
}
}
}
Loading

0 comments on commit c8981a3

Please sign in to comment.