Skip to content

Commit

Permalink
Add sync methods for base api class
Browse files Browse the repository at this point in the history
Add get sync Readme method
Update .podspec
  • Loading branch information
serhii-londar committed Sep 14, 2018
1 parent 01cb30d commit f9570f5
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 6 deletions.
6 changes: 5 additions & 1 deletion Example/GithubAPI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0830;
LastUpgradeCheck = 0830;
LastUpgradeCheck = 0940;
ORGANIZATIONNAME = CocoaPods;
TargetAttributes = {
607FACCF1AFB9204008FA782 = {
Expand Down Expand Up @@ -483,12 +483,14 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
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_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down Expand Up @@ -536,12 +538,14 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
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_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0900"
LastUpgradeVersion = "0940"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -40,7 +40,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand Down Expand Up @@ -70,7 +69,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
2 changes: 1 addition & 1 deletion GithubAPI.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'GithubAPI'
s.version = '0.0.4'
s.version = '0.0.5'
s.summary = 'Swift implementation of Github REST api v3'

# This description is used to generate tags and improve search results.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
61 changes: 61 additions & 0 deletions GithubAPI/Classes/GithubAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ public class GithubAPI: BaseAPI {
}
}

public func getSync<T:Decodable>(path: String, parameters: [String : String]? = nil, headers: [String: String]? = nil) -> (response: T?, error: Error?) {
let (newHeaders, newParameters) = self.addAuthenticationIfNeeded(headers, parameters: parameters)
let response = self.get(url: self.baseUrl + path, parameters: newParameters, headers: newHeaders)
if let data = response.data {
do {
let model = try JSONDecoder().decode(T.self, from: data)
return (model, response.error)
} catch {
return (nil, error)
}
} else {
return (nil, response.error)
}
}

public func put<T:Decodable>(path: String, parameters: [String : String]? = nil, headers: [String: String]? = nil, body: Data?, completion: @escaping (T?, Error?) -> Swift.Void) {
let (newHeaders, newParameters) = self.addAuthenticationIfNeeded(headers, parameters: parameters)
self.put(url: self.baseUrl + path, parameters: newParameters, headers: newHeaders, body: body) { (data, response, error) in
Expand All @@ -61,6 +76,21 @@ public class GithubAPI: BaseAPI {
}
}

public func putSync<T:Decodable>(path: String, parameters: [String : String]? = nil, headers: [String: String]? = nil, body: Data?) -> (response: T?, error: Error?) {
let (newHeaders, newParameters) = self.addAuthenticationIfNeeded(headers, parameters: parameters)
let response = self.put(url: self.baseUrl + path, parameters: newParameters, headers: newHeaders, body: body)
if let data = response.data {
do {
let model = try JSONDecoder().decode(T.self, from: data)
return (model, response.error)
} catch {
return (nil, error)
}
} else {
return (nil, response.error)
}
}

public func post<T:Decodable>(path: String, parameters: [String : String]? = nil, headers: [String: String]? = nil, body: Data?, completion: @escaping (T?, Error?) -> Swift.Void) {
let (newHeaders, newParameters) = self.addAuthenticationIfNeeded(headers, parameters: parameters)
self.post(url: self.baseUrl + path, parameters: newParameters, headers: newHeaders, body: body) { (data, response, error) in
Expand All @@ -77,6 +107,21 @@ public class GithubAPI: BaseAPI {
}
}

public func postSync<T:Decodable>(path: String, parameters: [String : String]? = nil, headers: [String: String]? = nil, body: Data?) -> (response: T?, error: Error?) {
let (newHeaders, newParameters) = self.addAuthenticationIfNeeded(headers, parameters: parameters)
let response = self.post(url: self.baseUrl + path, parameters: newParameters, headers: newHeaders, body: body)
if let data = response.data {
do {
let model = try JSONDecoder().decode(T.self, from: data)
return (model, response.error)
} catch {
return (nil, error)
}
} else {
return (nil, response.error)
}
}

public func patch<T:Decodable>(path: String, parameters: [String : String]? = nil, headers: [String: String]? = nil, body: Data?, completion: @escaping (T?, Error?) -> Swift.Void) {
let (newHeaders, newParameters) = self.addAuthenticationIfNeeded(headers, parameters: parameters)
self.patch(url: self.baseUrl + path, parameters: newParameters, headers: newHeaders, body: body) { (data, response, error) in
Expand All @@ -94,6 +139,22 @@ public class GithubAPI: BaseAPI {
}
}

public func patchSync<T:Decodable>(path: String, parameters: [String : String]? = nil, headers: [String: String]? = nil, body: Data?) -> (response: T?, error: Error?) {
let (newHeaders, newParameters) = self.addAuthenticationIfNeeded(headers, parameters: parameters)
let response = self.patch(url: self.baseUrl + path, parameters: newParameters, headers: newHeaders, body: body)
if let data = response.data {
do {
let model = try JSONDecoder().decode(T.self, from: data)
return (model, response.error)
} catch {
return (nil, error)
}
} else {
return (nil, response.error)
}
}


func addAuthenticationIfNeeded(_ headers: [String : String]?, parameters: [String : String]?) -> (headers: [String : String]?, parameters: [String : String]?) {
var newHeaders = headers
var newParameters = parameters
Expand Down
11 changes: 10 additions & 1 deletion GithubAPI/Classes/RepositoriesAPI/RepositoriesContentsAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Foundation
import BaseAPI

public class RepositoriesContentsAPI: GithubAPI {

public func getReadme(owner: String, repo: String, ref: String? = nil, completion: @escaping(RepositoryContentsReponse?, Error?) -> Void) {
let path = "/repos/\(owner)/\(repo)/readme"
var parameters: [String : String]? = nil
Expand All @@ -19,4 +18,14 @@ public class RepositoriesContentsAPI: GithubAPI {
}
self.get(path: path, parameters: parameters, completion: completion)
}

public func getReadmeSync(owner: String, repo: String, ref: String? = nil) -> (response: RepositoryContentsReponse?, error: Error?) {
let path = "/repos/\(owner)/\(repo)/readme"
var parameters: [String : String]? = nil
if let ref = ref {
parameters = [String : String]()
parameters!["ref"] = ref
}
return self.getSync(path: path, parameters: parameters, headers: nil)
}
}

0 comments on commit f9570f5

Please sign in to comment.