Skip to content

Commit 818df54

Browse files
committed
Improving Success & Error Messages
1 parent b8f89ea commit 818df54

File tree

16 files changed

+269
-121
lines changed

16 files changed

+269
-121
lines changed

.DS_Store

2 KB
Binary file not shown.

SimpleAPI.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Pod::Spec.new do |spec|
33

44

55
spec.name = "SimpleAPI"
6-
spec.version = "2.0.6"
6+
spec.version = "2.0.7"
77
spec.summary = "Simple HTTP Networking in Swift based on UrlSession"
88

99

SimpleAPI.xcodeproj/project.pbxproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
41E6885B26EBE77700A04CAA /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 41E6885926EBE77700A04CAA /* LaunchScreen.storyboard */; };
2828
41E6886226EBE8BC00A04CAA /* API.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41E6886026EBE8BC00A04CAA /* API.swift */; };
2929
41E6886326EBE8BC00A04CAA /* APIService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41E6886126EBE8BC00A04CAA /* APIService.swift */; };
30+
41FEF84E27EBB56300111F2E /* HTTPMethod.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41FEF84D27EBB56300111F2E /* HTTPMethod.swift */; };
31+
41FEF85027EBB59900111F2E /* Encoding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41FEF84F27EBB59900111F2E /* Encoding.swift */; };
3032
/* End PBXBuildFile section */
3133

3234
/* Begin PBXContainerItemProxy section */
@@ -84,6 +86,8 @@
8486
41E6885C26EBE77700A04CAA /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
8587
41E6886026EBE8BC00A04CAA /* API.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = API.swift; sourceTree = "<group>"; };
8688
41E6886126EBE8BC00A04CAA /* APIService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIService.swift; sourceTree = "<group>"; };
89+
41FEF84D27EBB56300111F2E /* HTTPMethod.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTTPMethod.swift; sourceTree = "<group>"; };
90+
41FEF84F27EBB59900111F2E /* Encoding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Encoding.swift; sourceTree = "<group>"; };
8791
/* End PBXFileReference section */
8892

8993
/* Begin PBXFrameworksBuildPhase section */
@@ -147,6 +151,8 @@
147151
41E6883126EBE75100A04CAA /* SimpleAPI.h */,
148152
41E6883226EBE75100A04CAA /* Info.plist */,
149153
4108663327E3E894001DE5E1 /* Reachability.swift */,
154+
41FEF84D27EBB56300111F2E /* HTTPMethod.swift */,
155+
41FEF84F27EBB59900111F2E /* Encoding.swift */,
150156
41E6886126EBE8BC00A04CAA /* APIService.swift */,
151157
4108663727E3F54C001DE5E1 /* Model.swift */,
152158
41E6886026EBE8BC00A04CAA /* API.swift */,
@@ -324,10 +330,12 @@
324330
isa = PBXSourcesBuildPhase;
325331
buildActionMask = 2147483647;
326332
files = (
333+
41FEF85027EBB59900111F2E /* Encoding.swift in Sources */,
327334
41C26DBB27E5E93B0020783E /* ResultMessage.swift in Sources */,
328335
4108663827E3F54C001DE5E1 /* Model.swift in Sources */,
329336
41E6886326EBE8BC00A04CAA /* APIService.swift in Sources */,
330337
4108663627E3F4F6001DE5E1 /* QuickerVersion.swift in Sources */,
338+
41FEF84E27EBB56300111F2E /* HTTPMethod.swift in Sources */,
331339
41E6886226EBE8BC00A04CAA /* API.swift in Sources */,
332340
4108663427E3E894001DE5E1 /* Reachability.swift in Sources */,
333341
41C26DB927E5E8BE0020783E /* LongerVersion.swift in Sources */,
@@ -448,6 +456,7 @@
448456
SDKROOT = iphoneos;
449457
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
450458
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
459+
SWIFT_VERSION = 5.0;
451460
VERSIONING_SYSTEM = "apple-generic";
452461
VERSION_INFO_PREFIX = "";
453462
};
@@ -505,6 +514,7 @@
505514
SDKROOT = iphoneos;
506515
SWIFT_COMPILATION_MODE = wholemodule;
507516
SWIFT_OPTIMIZATION_LEVEL = "-O";
517+
SWIFT_VERSION = 5.0;
508518
VALIDATE_PRODUCT = YES;
509519
VERSIONING_SYSTEM = "apple-generic";
510520
VERSION_INFO_PREFIX = "";

SimpleAPI.xcodeproj/xcuserdata/belalsamy.xcuserdatad/xcschemes/xcschememanagement.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
<key>SimpleAPI.xcscheme_^#shared#^_</key>
88
<dict>
99
<key>orderHint</key>
10-
<integer>0</integer>
10+
<integer>1</integer>
1111
</dict>
1212
<key>SimpleAPIExamples.xcscheme_^#shared#^_</key>
1313
<dict>
1414
<key>orderHint</key>
15-
<integer>1</integer>
15+
<integer>0</integer>
1616
</dict>
1717
</dict>
1818
</dict>

SimpleAPI/API.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class API<M: Model> {
1212

1313
public enum ResultOfObject {
1414
case success(M?)
15+
case string(String?)
1516
case failure(String)
1617
}
1718

SimpleAPI/APIService.swift

Lines changed: 10 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,22 @@
77

88
import Foundation
99

10-
public enum HTTPMethod {
11-
case getWithoutID
12-
case get(_ id: String)
13-
case post
14-
case put(_ id: String)
15-
case delete(_ id: String)
16-
17-
var name: String {
18-
switch self {
19-
case .getWithoutID:
20-
return "GET"
21-
case .get:
22-
return "GET"
23-
case .post:
24-
return "POST"
25-
case .put:
26-
return "PUT"
27-
case .delete:
28-
return "DELETE"
29-
}
30-
}
31-
32-
var id: String {
33-
switch self {
34-
case .getWithoutID:
35-
return ""
36-
case .get(let id):
37-
return "with id \"\(id)\""
38-
case .post:
39-
return ""
40-
case .put(let id):
41-
return "with id \"\(id)\""
42-
case .delete(let id):
43-
return "with id \"\(id)\""
44-
}
45-
}
46-
47-
}
48-
49-
public enum Encoding {
50-
case json
51-
case url
52-
}
10+
//MARK: - TypeAlias
5311

5412
public typealias Params = [String: Any]
5513
public typealias Headers = [String: String]
14+
public typealias Endpoint = String
5615

16+
//MARK: - APIService
5717

5818
public class APIService {
5919

6020
public enum Response {
61-
case success(Data?)
62-
case failure(Error?)
21+
case success(data: Data?)
22+
case failure(error: Error?)
6323
}
6424

65-
public static func request(_ endpoint: String,
25+
public static func request(_ endpoint: Endpoint,
6626
method: HTTPMethod,
6727
params: Params? = nil,
6828
headers: Headers? = nil,
@@ -71,6 +31,7 @@ public class APIService {
7131

7232
// request from endpoint
7333
guard let allowArabicAndSpacesInEndPoint = endpoint.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return }
34+
7435
guard let url = URL(string: allowArabicAndSpacesInEndPoint) else { return }
7536
var request = URLRequest(url: url)
7637

@@ -108,18 +69,18 @@ public class APIService {
10869
let statusCode = (myResponse as! HTTPURLResponse).statusCode
10970

11071
if 200 ... 299 ~= statusCode {
111-
complete(.success(data))
72+
complete(.success(data: data))
11273

11374
}else{
114-
complete(.failure(error))
75+
complete(.failure(error: error))
11576
}
11677
}.resume()
11778

11879
}
11980
}
12081

12182

122-
func urlEncoded(dict: [String: Any]) -> String {
83+
func urlEncoded(dict: Params) -> String {
12384
var resultString = ""
12485
for (key, value) in dict {
12586
resultString = "\(resultString)&\(key)=\(value)"

SimpleAPI/Encoding.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// Encoding.swift
3+
// SimpleAPI
4+
//
5+
// Created by Belal Samy on 23/03/2022.
6+
//
7+
8+
import Foundation
9+
10+
public enum Encoding {
11+
case json
12+
case url
13+
}

SimpleAPI/EndPoint.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// EndPoint.swift
3+
// SimpleAPI
4+
//
5+
// Created by Belal Samy on 23/03/2022.
6+
//
7+
8+
import Foundation
9+
10+
public enum EndPoint {
11+
case url(String)
12+
case customize(paramsArr: [String]?, queryDict: [String: String]?)
13+
14+
var url: String? {
15+
switch self {
16+
case .url(let url):
17+
return "\(url)"
18+
19+
case .customize(let paramsArr, let queryDict):
20+
var endpoint = ""
21+
22+
if let paramsArr = paramsArr {
23+
for param in paramsArr {
24+
endpoint += "/\(param)"
25+
}
26+
}
27+
28+
if let queryDict = queryDict {
29+
for (key, value) in queryDict {
30+
endpoint += "?\(key)=\(value)"
31+
}
32+
}
33+
34+
return endpoint
35+
}
36+
}
37+
}

SimpleAPI/HTTPMethod.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// HTTPMethod.swift
3+
// SimpleAPI
4+
//
5+
// Created by Belal Samy on 23/03/2022.
6+
//
7+
8+
import Foundation
9+
10+
public enum HTTPMethod {
11+
case get(_ path: String = "")
12+
case post(_ path: String = "")
13+
case put(_ path: String = "")
14+
case delete(_ path: String = "")
15+
16+
var name: String {
17+
switch self {
18+
case .get:
19+
return "GET"
20+
case .post:
21+
return "POST"
22+
case .put:
23+
return "PUT"
24+
case .delete:
25+
return "DELETE"
26+
}
27+
}
28+
29+
var id: String {
30+
switch self {
31+
case .get(let path):
32+
return path == "" ? "" : "with path \"\(path)\""
33+
case .post(let path):
34+
return path == "" ? "" : "with path \"\(path)\""
35+
case .put(let path):
36+
return path == "" ? "" : "with path \"\(path)\""
37+
case .delete(let path):
38+
return path == "" ? "" : "with path \"\(path)\""
39+
}
40+
}
41+
42+
}

0 commit comments

Comments
 (0)