Skip to content

Commit

Permalink
Merge pull request #49 from skladek/develop
Browse files Browse the repository at this point in the history
Version 2.0.0 Release
  • Loading branch information
skladek authored Jul 9, 2018
2 parents 5789210 + 50e34af commit 1aee183
Show file tree
Hide file tree
Showing 18 changed files with 130 additions and 65 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
# Change Log
All notable changes to this project will be documented in this file.

#### 2.x Releases
- `2.0.x` Releases - [2.0.0](#200)

#### 1.x Releases
- `1.2.x` Releases - [1.2.0](#120), [1.2.1](#121)
- `1.1.x` Releases - [1.1.0](#110)
- `1.0.x` Releases - [1.0.0](#100)

## [2.0.0](https://github.com/skladek/SKWebServiceController/releases/tag/2.0.0)

#### Added
- Generic types to the delete, get, post, and put methods to allow specifying the expected JSON object type. If the type is unknown or the old behavior is preferred, specify `Any?`

#### Removed
- The untyped delete, get, post, and put methods.

## [1.2.1](https://github.com/skladek/SKWebServiceController/releases/tag/1.2.1)

#### Updated
Expand Down
6 changes: 5 additions & 1 deletion Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,19 @@ The WebServiceController subclass will be used to perform requests. There are me

### JSON Methods

These methods are used to interact with endpoints that send and receive JSON. All requests have a `JSONCompletion` object that is executed when the request is complete.
These methods are used to interact with endpoints that send and receive JSON. All requests have a `JSONCompletion` object that is executed when the request is complete. The methods require a generic type to be specified for the expected type of the JSON. For instance, a GET call that returns an array would call the following:

get { (json: [Any]?, response, error) in

}

If the return data is expected to be a dictionary, that GET call would be written as such:

get { (json: [String: Any]?, response, error) in

}

This will support any type that can be output by `JSONSerialization.jsonObject(with:options:)`. If the call should return `Data` or a `UIImage`, there are separate methods to retrieve that. See Data Methods and Image Methods below. If the type is unknown or the version 1.x behavior is preferred, `Any?` can be specified as the type.

#### Delete

Expand All @@ -72,6 +84,14 @@ Performs a post request on the provided endpoint. This method has an optional js

Performs a put request on the provided endpoint. This method has an optional json parameter. This object must be a valid JSON object. This will be converted to data and sent with the request.

### Data Methods

There is currently a single method for getting data from a URL. This method has a `DataCompletion` object that is executed when the request is complete. This does not attempt to parse or format the data in any way, it simply returns whatever data is sent by the web service.

#### Get Data

This method takes a full URL and will return any data returned by the web service without attempting to format or manipulate it in any way.

### Image Methods

There is currently a single method for getting an image from a URL. This method has an `ImageCompletion` object that is executed when the request is complete.
Expand Down
2 changes: 1 addition & 1 deletion SKWebServiceController.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'SKWebServiceController'
spec.version = '1.2.1'
spec.version = '2.0.0'
spec.license = 'MIT'
spec.summary = 'A barebones network controller.'
spec.homepage = 'https://github.com/skladek/SKWebServiceController'
Expand Down
6 changes: 5 additions & 1 deletion SKWebServiceController.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0830;
LastUpgradeCheck = 0900;
LastUpgradeCheck = 0940;
ORGANIZATIONNAME = "Sean Kladek";
TargetAttributes = {
147FBB2A1EE6482000D2441D = {
Expand Down Expand Up @@ -494,13 +494,15 @@
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_DOCUMENTATION_COMMENTS = YES;
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 @@ -554,13 +556,15 @@
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_DOCUMENTATION_COMMENTS = YES;
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 All @@ -26,9 +26,8 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
codeCoverageEnabled = "YES"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand Down Expand Up @@ -57,7 +56,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
6 changes: 5 additions & 1 deletion SampleProject/SampleProject.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0830;
LastUpgradeCheck = 0900;
LastUpgradeCheck = 0940;
ORGANIZATIONNAME = "Sean Kladek";
TargetAttributes = {
14053C841ED394A80094E780 = {
Expand Down Expand Up @@ -443,13 +443,15 @@
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_DOCUMENTATION_COMMENTS = YES;
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 @@ -499,13 +501,15 @@
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_DOCUMENTATION_COMMENTS = YES;
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
Expand Up @@ -3,6 +3,6 @@ import SKWebServiceController
class MyWebServiceController: WebServiceController {
init() {
super.init(baseURL: "https://jsonplaceholder.typicode.com/")
self.useLocalFiles = true
self.useLocalFiles = false
}
}
10 changes: 5 additions & 5 deletions SampleProject/SampleProject/Networking/PostController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class PostController: NSObject {

let endpoint = String(format: Endpoints.postsWithId, postId)

webServiceController.delete(endpoint) { (_, _, error) in
webServiceController.delete(endpoint) { (_: Any?, _, error) in
completion(error)
}
}

func getPosts(completion: @escaping ([Post]?, Error?) -> Void) {
webServiceController.get(Endpoints.posts) { (objects, _, error) in
guard let objects = objects as? [[String: Any]] else {
webServiceController.get(Endpoints.posts) { (objects: [[String: Any]]?, _, error) in
guard let objects = objects else {
completion(nil, error)
return
}
Expand All @@ -39,13 +39,13 @@ class PostController: NSObject {

let endpoint = String(format: Endpoints.postsWithId, postId)

webServiceController.put(endpoint, json: post.toJSON(), requestConfiguration: nil) { (_, _, error) in
webServiceController.put(endpoint, json: post.toJSON(), requestConfiguration: nil) { (_: Any?, _, error) in
completion(error)
}
}

func uploadNew(_ post: Post, completion: @escaping (Error?) -> Void) {
webServiceController.post(Endpoints.posts, json: post.toJSON()) { (_, _, error) in
webServiceController.post(Endpoints.posts, json: post.toJSON()) { (_: Any?, _, error) in
completion(error)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.2.1</string>
<string>2.0.0</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
25 changes: 18 additions & 7 deletions Source/JSONHandler.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Foundation

typealias ConvertedJSON = (object: Any?, error: Error?)
typealias ConvertedJSON<T> = (object: T?, error: Error?)

protocol JSONHandling {
func dataToJSON(_ data: Data?) -> ConvertedJSON
func jsonToData(_ jsonObject: Any?) -> ConvertedJSON
func dataToJSON<T>(_ data: Data?) -> ConvertedJSON<T>
func jsonToData(_ jsonObject: Any?) -> ConvertedJSON<Data>
}

class JSONHandler: JSONHandling {
Expand All @@ -21,7 +21,7 @@ class JSONHandler: JSONHandling {

// MARK: Instance Methods

func dataToJSON(_ data: Data?) -> ConvertedJSON {
func dataToJSON<T>(_ data: Data?) -> ConvertedJSON<T> {
guard let data = data else {
let error = WebServiceError(code: .noData, message: "The server returned without error and without data.")
return (nil, error)
Expand All @@ -36,10 +36,21 @@ class JSONHandler: JSONHandling {
serializationError = error
}

return (jsonObject, serializationError)
guard let typedJSONObject = jsonObject as? T else {
var receivedTypeString = "nil"

if let jsonObject = jsonObject {
receivedTypeString = String(describing: type(of: jsonObject))
}

let typeError = WebServiceError(code: .invalidData, message: "The JSON object was not the expected type. Received \(receivedTypeString), expected \(T.self)")
return (nil, typeError)
}

return (typedJSONObject, serializationError)
}

func jsonToData(_ jsonObject: Any?) -> ConvertedJSON {
func jsonToData(_ jsonObject: Any?) -> ConvertedJSON<Data> {
guard let jsonObject = jsonObject else {
let error = WebServiceError(code: .noData, message: "The JSON object to be converted was nil.")
return (nil, error)
Expand All @@ -50,7 +61,7 @@ class JSONHandler: JSONHandling {
return (nil, error)
}

var jsonData: Any? = nil
var jsonData: Data? = nil
var serializationError: Error? = nil

do {
Expand Down
8 changes: 4 additions & 4 deletions Source/RequestController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ protocol Requesting {

func dataCompletion(data: Data?, response: URLResponse?, error: Error?, completion: @escaping WebServiceController.DataCompletion)
func imageCompletion(data: Data?, response: URLResponse?, error: Error?, completion: @escaping WebServiceController.ImageCompletion)
func jsonCompletion(data: Data?, response: URLResponse?, error: Error?, completion: @escaping WebServiceController.JSONCompletion)
func jsonCompletion<T>(data: Data?, response: URLResponse?, error: Error?, completion: @escaping WebServiceController.JSONCompletion<T>)
func performRequest(_ request: URLRequest, headers: [AnyHashable: Any]?, httpMethod: WebServiceController.HTTPMethod, json: Any?, completion: @escaping RequestCompletion) -> URLSessionDataTask?
func performRequest(endpoint: String?, httpMethod: WebServiceController.HTTPMethod, json: Any?, requestConfiguration: RequestConfiguration?, completion: @escaping RequestCompletion) -> URLSessionDataTask?
}
Expand Down Expand Up @@ -73,14 +73,14 @@ class RequestController: Requesting {
}
}

func jsonCompletion(data: Data?, response: URLResponse?, error: Error?, completion: @escaping WebServiceController.JSONCompletion) {
func jsonCompletion<T>(data: Data?, response: URLResponse?, error: Error?, completion: @escaping WebServiceController.JSONCompletion<T>) {
DispatchQueue.main.async {
if let error = error {
completion(nil, response, error)
return
}

let result = self.jsonHandler.dataToJSON(data)
let result: ConvertedJSON<T> = self.jsonHandler.dataToJSON(data)
completion(result.object, response, result.error)
}
}
Expand Down Expand Up @@ -110,7 +110,7 @@ class RequestController: Requesting {
return nil
}

data = convertedJSON.object as? Data
data = convertedJSON.object
}

switch httpMethod {
Expand Down
Loading

0 comments on commit 1aee183

Please sign in to comment.