Skip to content

Commit 49c7446

Browse files
committed
- swift 5.0 migration
- Response added with tests & documentation
1 parent 89971e9 commit 49c7446

File tree

11 files changed

+89
-24
lines changed

11 files changed

+89
-24
lines changed

Cartfile.resolved

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
github "SwiftyJSON/SwiftyJSON" "4.1.0"
1+
github "SwiftyJSON/SwiftyJSON" "4.3.0"

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,18 @@ public protocol ValidRequestData {
276276

277277
One type must not implement this protocol twice or more, meaning it is not supported to conform to both `ValidFormData` and `JSONConvertible`.
278278

279+
## Response
280+
281+
A response wrapper is provided in the frameworj for all `ValidResponseData` in order to parse both the success and a possible error response. However, using the same structure, you can implement any other type of generic response to match your needs. An example cann with the built in one looks like the following:
282+
283+
```swift
284+
testServerApi.get("/endpoint") { (status, response: Response<ExampleResponse, APIEror>?) in
285+
//...
286+
}
287+
```
288+
289+
Where `APIEror` is an other custom `ValidResponseData` type, but `JSON` is accepted as well (since it also conforms to `ValidResponseData`). Note, that array of `ValidResponseData` also conforms to `ValidResponseData`.
290+
279291
# Debugging
280292

281293
To turn on request logging add `APIRequestLoggingEnabled` to 'Arguments passed on launch' at Schema/Run/Arguments.

RESTAPI.xcodeproj/project.pbxproj

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
219AEBC61E093AC3009C939B /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21493A621D427E3000B5CF8A /* Utils.swift */; };
4242
219AEBC71E093AC7009C939B /* JSONConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2176DDF31D86E3D8005A1B30 /* JSONConvertible.swift */; };
4343
21A9B5C21D475D4800D2B57A /* Cartfile in Resources */ = {isa = PBXBuildFile; fileRef = 21A9B5C11D475D4800D2B57A /* Cartfile */; };
44+
21ABC93F2264BBDC004ED369 /* UtilTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21ABC93E2264BBDC004ED369 /* UtilTypes.swift */; };
4445
21C3AC6B21959CEC004ACB75 /* RESTAPICodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21C3AC6921959CBD004ACB75 /* RESTAPICodable.swift */; };
4546
21C3AC6C21959CEC004ACB75 /* RESTAPICodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21C3AC6921959CBD004ACB75 /* RESTAPICodable.swift */; };
4647
21C3AC6D2195B1D4004ACB75 /* MultipartFormData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 213C5EA1215A44730039DC20 /* MultipartFormData.swift */; };
@@ -140,6 +141,7 @@
140141
219AEBBB1E09367A009C939B /* SwiftyJSON.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SwiftyJSON.framework; path = Carthage/Build/iOS/SwiftyJSON.framework; sourceTree = "<group>"; };
141142
219AEBC11E093773009C939B /* SwiftyJSON.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SwiftyJSON.framework; path = Carthage/Build/tvOS/SwiftyJSON.framework; sourceTree = "<group>"; };
142143
21A9B5C11D475D4800D2B57A /* Cartfile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Cartfile; sourceTree = "<group>"; };
144+
21ABC93E2264BBDC004ED369 /* UtilTypes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UtilTypes.swift; sourceTree = "<group>"; };
143145
21C3AC6921959CBD004ACB75 /* RESTAPICodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RESTAPICodable.swift; sourceTree = "<group>"; };
144146
21F570AB1FAF0ACE00E8BFDB /* API+Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "API+Utils.swift"; sourceTree = "<group>"; };
145147
/* End PBXFileReference section */
@@ -202,6 +204,7 @@
202204
isa = PBXGroup;
203205
children = (
204206
21493A611D427E3000B5CF8A /* JSONCodable.swift */,
207+
21ABC93E2264BBDC004ED369 /* UtilTypes.swift */,
205208
);
206209
path = ValidResponseData;
207210
sourceTree = "<group>";
@@ -469,7 +472,7 @@
469472
isa = PBXProject;
470473
attributes = {
471474
LastSwiftUpdateCheck = 1000;
472-
LastUpgradeCheck = 1000;
475+
LastUpgradeCheck = 1020;
473476
ORGANIZATIONNAME = gujci;
474477
TargetAttributes = {
475478
213C5EA7215A44DD0039DC20 = {
@@ -503,10 +506,11 @@
503506
};
504507
buildConfigurationList = 215D7C441D4279E8001F4C45 /* Build configuration list for PBXProject "RESTAPI" */;
505508
compatibilityVersion = "Xcode 3.2";
506-
developmentRegion = English;
509+
developmentRegion = en;
507510
hasScannedForEncodings = 0;
508511
knownRegions = (
509512
en,
513+
Base,
510514
);
511515
mainGroup = 215D7C401D4279E8001F4C45;
512516
productRefGroup = 215D7C4B1D4279E8001F4C45 /* Products */;
@@ -597,6 +601,7 @@
597601
21F570AC1FAF0ACE00E8BFDB /* API+Utils.swift in Sources */,
598602
2182D6371F13A14000B091CE /* FormEncodable.swift in Sources */,
599603
2124F72A21958E19007BD953 /* APIImplementation.swift in Sources */,
604+
21ABC93F2264BBDC004ED369 /* UtilTypes.swift in Sources */,
600605
213C5EA2215A44730039DC20 /* MultipartFormData.swift in Sources */,
601606
21493A651D427E3000B5CF8A /* Utils.swift in Sources */,
602607
21493A631D427E3000B5CF8A /* API.swift in Sources */,
@@ -702,7 +707,7 @@
702707
SKIP_INSTALL = YES;
703708
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
704709
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
705-
SWIFT_VERSION = 4.2;
710+
SWIFT_VERSION = 5.0;
706711
TARGETED_DEVICE_FAMILY = "1,2";
707712
};
708713
name = Debug;
@@ -740,7 +745,7 @@
740745
PRODUCT_BUNDLE_IDENTIFIER = com.gujci.RESTAPIImage;
741746
PRODUCT_NAME = RESTAPIImage;
742747
SKIP_INSTALL = YES;
743-
SWIFT_VERSION = 4.2;
748+
SWIFT_VERSION = 5.0;
744749
TARGETED_DEVICE_FAMILY = "1,2";
745750
};
746751
name = Release;
@@ -773,7 +778,7 @@
773778
PRODUCT_BUNDLE_IDENTIFIER = com.gujci.RESTAPIImageTests;
774779
PRODUCT_NAME = "$(TARGET_NAME)";
775780
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
776-
SWIFT_VERSION = 4.2;
781+
SWIFT_VERSION = 5.0;
777782
TARGETED_DEVICE_FAMILY = "1,2";
778783
};
779784
name = Debug;
@@ -804,7 +809,7 @@
804809
MTL_FAST_MATH = YES;
805810
PRODUCT_BUNDLE_IDENTIFIER = com.gujci.RESTAPIImageTests;
806811
PRODUCT_NAME = "$(TARGET_NAME)";
807-
SWIFT_VERSION = 4.2;
812+
SWIFT_VERSION = 5.0;
808813
TARGETED_DEVICE_FAMILY = "1,2";
809814
};
810815
name = Release;
@@ -813,6 +818,7 @@
813818
isa = XCBuildConfiguration;
814819
buildSettings = {
815820
ALWAYS_SEARCH_USER_PATHS = NO;
821+
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
816822
CLANG_ANALYZER_NONNULL = YES;
817823
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
818824
CLANG_CXX_LIBRARY = "libc++";
@@ -862,6 +868,7 @@
862868
ONLY_ACTIVE_ARCH = YES;
863869
SDKROOT = iphoneos;
864870
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
871+
SWIFT_VERSION = 5.0;
865872
TARGETED_DEVICE_FAMILY = "1,2";
866873
VERSIONING_SYSTEM = "apple-generic";
867874
VERSION_INFO_PREFIX = "";
@@ -872,6 +879,7 @@
872879
isa = XCBuildConfiguration;
873880
buildSettings = {
874881
ALWAYS_SEARCH_USER_PATHS = NO;
882+
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
875883
CLANG_ANALYZER_NONNULL = YES;
876884
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
877885
CLANG_CXX_LIBRARY = "libc++";
@@ -914,6 +922,7 @@
914922
MTL_ENABLE_DEBUG_INFO = NO;
915923
SDKROOT = iphoneos;
916924
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
925+
SWIFT_VERSION = 5.0;
917926
TARGETED_DEVICE_FAMILY = "1,2";
918927
VALIDATE_PRODUCT = YES;
919928
VERSIONING_SYSTEM = "apple-generic";
@@ -944,7 +953,7 @@
944953
PRODUCT_NAME = RESTAPI;
945954
SKIP_INSTALL = YES;
946955
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
947-
SWIFT_VERSION = 4.2;
956+
SWIFT_VERSION = 5.0;
948957
};
949958
name = Debug;
950959
};
@@ -971,7 +980,7 @@
971980
PRODUCT_NAME = RESTAPI;
972981
SKIP_INSTALL = YES;
973982
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
974-
SWIFT_VERSION = 4.2;
983+
SWIFT_VERSION = 5.0;
975984
};
976985
name = Release;
977986
};
@@ -990,7 +999,7 @@
990999
PRODUCT_BUNDLE_IDENTIFIER = com.gujci.RESTAPITests;
9911000
PRODUCT_NAME = "$(TARGET_NAME)";
9921001
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
993-
SWIFT_VERSION = 4.2;
1002+
SWIFT_VERSION = 5.0;
9941003
};
9951004
name = Debug;
9961005
};
@@ -1009,7 +1018,7 @@
10091018
PRODUCT_BUNDLE_IDENTIFIER = com.gujci.RESTAPITests;
10101019
PRODUCT_NAME = "$(TARGET_NAME)";
10111020
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
1012-
SWIFT_VERSION = 4.2;
1021+
SWIFT_VERSION = 5.0;
10131022
};
10141023
name = Release;
10151024
};
@@ -1036,7 +1045,7 @@
10361045
SDKROOT = appletvos;
10371046
SKIP_INSTALL = YES;
10381047
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
1039-
SWIFT_VERSION = 3.0;
1048+
SWIFT_VERSION = 5.0;
10401049
TARGETED_DEVICE_FAMILY = 3;
10411050
TVOS_DEPLOYMENT_TARGET = 9.0;
10421051
};
@@ -1064,7 +1073,7 @@
10641073
PRODUCT_NAME = RESTAPI;
10651074
SDKROOT = appletvos;
10661075
SKIP_INSTALL = YES;
1067-
SWIFT_VERSION = 3.0;
1076+
SWIFT_VERSION = 5.0;
10681077
TARGETED_DEVICE_FAMILY = 3;
10691078
TVOS_DEPLOYMENT_TARGET = 9.0;
10701079
};
@@ -1086,7 +1095,7 @@
10861095
PRODUCT_NAME = "$(TARGET_NAME)";
10871096
SDKROOT = appletvos;
10881097
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
1089-
SWIFT_VERSION = 3.0;
1098+
SWIFT_VERSION = 5.0;
10901099
TVOS_DEPLOYMENT_TARGET = 10.1;
10911100
};
10921101
name = Debug;
@@ -1106,7 +1115,7 @@
11061115
PRODUCT_BUNDLE_IDENTIFIER = "com.gujci.RESTAPI-tvOSTests";
11071116
PRODUCT_NAME = "$(TARGET_NAME)";
11081117
SDKROOT = appletvos;
1109-
SWIFT_VERSION = 3.0;
1118+
SWIFT_VERSION = 5.0;
11101119
TVOS_DEPLOYMENT_TARGET = 10.1;
11111120
};
11121121
name = Release;

RESTAPI.xcodeproj/xcshareddata/xcschemes/RESTAPI iOS.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1000"
3+
LastUpgradeVersion = "1020"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

RESTAPI.xcodeproj/xcshareddata/xcschemes/RESTAPI tvOS.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1000"
3+
LastUpgradeVersion = "1020"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

RESTAPI.xcodeproj/xcshareddata/xcschemes/RESTAPIImage iOS.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1000"
3+
LastUpgradeVersion = "1020"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

RESTAPI/APIImplementation.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fileprivate extension ContentType {
2323

2424
internal extension API {
2525

26-
internal func parseableRequest<T: ValidResponseData>(_ method: String, endpoint: String, query: [String: Queryable]? = nil,
26+
func parseableRequest<T: ValidResponseData>(_ method: String, endpoint: String, query: [String: Queryable]? = nil,
2727
data: ValidRequestData? = nil,
2828
completion: @escaping (_ status: ResponseStatus, _ object: T?) -> ()) {
2929
dataTask(clientURLRequest(endpoint, query: query, params: data), method: method) { status ,data in
@@ -36,7 +36,7 @@ internal extension API {
3636
}
3737
}
3838

39-
internal func dataTask(_ request: URLRequest, method: String, completion: @escaping (_ status: ResponseStatus, _ object: Data?) -> ()) {
39+
func dataTask(_ request: URLRequest, method: String, completion: @escaping (_ status: ResponseStatus, _ object: Data?) -> ()) {
4040

4141
var request = request
4242
request.httpMethod = method
@@ -71,7 +71,7 @@ internal extension API {
7171
}) .resume()
7272
}
7373

74-
internal func clientURLRequest(_ path: String, query: [String: Queryable]?, params: ValidRequestData?)
74+
func clientURLRequest(_ path: String, query: [String: Queryable]?, params: ValidRequestData?)
7575
-> URLRequest {
7676
var request = URLRequest(url: URL(string: baseURL + path, query: query))
7777
if let params = params, let httpData = try? params.requestData() {

RESTAPI/ValidRequestData/JSONConvertible.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ extension String: ValidJSONData {
5252

5353
public extension ValidJSONData where Self: Encodable {
5454

55-
public func JSONFormat() throws -> Data {
55+
func JSONFormat() throws -> Data {
5656
let encoder = JSONEncoder()
5757
return try encoder.encode(self)
5858
}

RESTAPI/ValidResponseData/JSONCodable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public protocol JSONCodable: ValidResponseData { }
88

99
public extension JSONCodable where Self: Decodable {
1010

11-
public static func createInstance(from data: Data) throws -> Self {
11+
static func createInstance(from data: Data) throws -> Self {
1212
let decoder = JSONDecoder()
1313
return try decoder.decode(Self.self, from: data)
1414
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// UtilTypes.swift
3+
// RESTAPI iOS
4+
//
5+
// Created by Gujgiczer Máté on 2019. 04. 15..
6+
// Copyright © 2019. gujci. All rights reserved.
7+
//
8+
9+
public struct Response<DataType: ValidResponseData, ErrorType: ValidResponseData>: ValidResponseData {
10+
public var data: DataType?
11+
public var error: ErrorType?
12+
13+
public static func createInstance(from data: Data) throws -> Response<DataType, ErrorType> {
14+
return Response(data: try? DataType.createInstance(from: data),
15+
error: try? ErrorType.createInstance(from: data))
16+
}
17+
}
18+
19+
20+
public struct APIEror: JSONParseable {
21+
22+
public var message: String
23+
24+
public init(withJSON data: JSON) throws {
25+
guard let text = data["message"].string else { throw ObjectParseError.jsonSerializeError }
26+
message = text
27+
}
28+
}

0 commit comments

Comments
 (0)