Skip to content

Commit

Permalink
Improving Success & Error Messages
Browse files Browse the repository at this point in the history
  • Loading branch information
belalsamyyy committed Apr 2, 2022
1 parent b8f89ea commit 818df54
Show file tree
Hide file tree
Showing 16 changed files with 269 additions and 121 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion SimpleAPI.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Pod::Spec.new do |spec|


spec.name = "SimpleAPI"
spec.version = "2.0.6"
spec.version = "2.0.7"
spec.summary = "Simple HTTP Networking in Swift based on UrlSession"


Expand Down
10 changes: 10 additions & 0 deletions SimpleAPI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
41E6885B26EBE77700A04CAA /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 41E6885926EBE77700A04CAA /* LaunchScreen.storyboard */; };
41E6886226EBE8BC00A04CAA /* API.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41E6886026EBE8BC00A04CAA /* API.swift */; };
41E6886326EBE8BC00A04CAA /* APIService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41E6886126EBE8BC00A04CAA /* APIService.swift */; };
41FEF84E27EBB56300111F2E /* HTTPMethod.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41FEF84D27EBB56300111F2E /* HTTPMethod.swift */; };
41FEF85027EBB59900111F2E /* Encoding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41FEF84F27EBB59900111F2E /* Encoding.swift */; };
/* End PBXBuildFile section */

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

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -147,6 +151,8 @@
41E6883126EBE75100A04CAA /* SimpleAPI.h */,
41E6883226EBE75100A04CAA /* Info.plist */,
4108663327E3E894001DE5E1 /* Reachability.swift */,
41FEF84D27EBB56300111F2E /* HTTPMethod.swift */,
41FEF84F27EBB59900111F2E /* Encoding.swift */,
41E6886126EBE8BC00A04CAA /* APIService.swift */,
4108663727E3F54C001DE5E1 /* Model.swift */,
41E6886026EBE8BC00A04CAA /* API.swift */,
Expand Down Expand Up @@ -324,10 +330,12 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
41FEF85027EBB59900111F2E /* Encoding.swift in Sources */,
41C26DBB27E5E93B0020783E /* ResultMessage.swift in Sources */,
4108663827E3F54C001DE5E1 /* Model.swift in Sources */,
41E6886326EBE8BC00A04CAA /* APIService.swift in Sources */,
4108663627E3F4F6001DE5E1 /* QuickerVersion.swift in Sources */,
41FEF84E27EBB56300111F2E /* HTTPMethod.swift in Sources */,
41E6886226EBE8BC00A04CAA /* API.swift in Sources */,
4108663427E3E894001DE5E1 /* Reachability.swift in Sources */,
41C26DB927E5E8BE0020783E /* LongerVersion.swift in Sources */,
Expand Down Expand Up @@ -448,6 +456,7 @@
SDKROOT = iphoneos;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand Down Expand Up @@ -505,6 +514,7 @@
SDKROOT = iphoneos;
SWIFT_COMPILATION_MODE = wholemodule;
SWIFT_OPTIMIZATION_LEVEL = "-O";
SWIFT_VERSION = 5.0;
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
<key>SimpleAPI.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
<integer>1</integer>
</dict>
<key>SimpleAPIExamples.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>0</integer>
</dict>
</dict>
</dict>
Expand Down
1 change: 1 addition & 0 deletions SimpleAPI/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class API<M: Model> {

public enum ResultOfObject {
case success(M?)
case string(String?)
case failure(String)
}

Expand Down
59 changes: 10 additions & 49 deletions SimpleAPI/APIService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,22 @@

import Foundation

public enum HTTPMethod {
case getWithoutID
case get(_ id: String)
case post
case put(_ id: String)
case delete(_ id: String)

var name: String {
switch self {
case .getWithoutID:
return "GET"
case .get:
return "GET"
case .post:
return "POST"
case .put:
return "PUT"
case .delete:
return "DELETE"
}
}

var id: String {
switch self {
case .getWithoutID:
return ""
case .get(let id):
return "with id \"\(id)\""
case .post:
return ""
case .put(let id):
return "with id \"\(id)\""
case .delete(let id):
return "with id \"\(id)\""
}
}

}

public enum Encoding {
case json
case url
}
//MARK: - TypeAlias

public typealias Params = [String: Any]
public typealias Headers = [String: String]
public typealias Endpoint = String

//MARK: - APIService

public class APIService {

public enum Response {
case success(Data?)
case failure(Error?)
case success(data: Data?)
case failure(error: Error?)
}

public static func request(_ endpoint: String,
public static func request(_ endpoint: Endpoint,
method: HTTPMethod,
params: Params? = nil,
headers: Headers? = nil,
Expand All @@ -71,6 +31,7 @@ public class APIService {

// request from endpoint
guard let allowArabicAndSpacesInEndPoint = endpoint.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return }

guard let url = URL(string: allowArabicAndSpacesInEndPoint) else { return }
var request = URLRequest(url: url)

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

if 200 ... 299 ~= statusCode {
complete(.success(data))
complete(.success(data: data))

}else{
complete(.failure(error))
complete(.failure(error: error))
}
}.resume()

}
}


func urlEncoded(dict: [String: Any]) -> String {
func urlEncoded(dict: Params) -> String {
var resultString = ""
for (key, value) in dict {
resultString = "\(resultString)&\(key)=\(value)"
Expand Down
13 changes: 13 additions & 0 deletions SimpleAPI/Encoding.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Encoding.swift
// SimpleAPI
//
// Created by Belal Samy on 23/03/2022.
//

import Foundation

public enum Encoding {
case json
case url
}
37 changes: 37 additions & 0 deletions SimpleAPI/EndPoint.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// EndPoint.swift
// SimpleAPI
//
// Created by Belal Samy on 23/03/2022.
//

import Foundation

public enum EndPoint {
case url(String)
case customize(paramsArr: [String]?, queryDict: [String: String]?)

var url: String? {
switch self {
case .url(let url):
return "\(url)"

case .customize(let paramsArr, let queryDict):
var endpoint = ""

if let paramsArr = paramsArr {
for param in paramsArr {
endpoint += "/\(param)"
}
}

if let queryDict = queryDict {
for (key, value) in queryDict {
endpoint += "?\(key)=\(value)"
}
}

return endpoint
}
}
}
42 changes: 42 additions & 0 deletions SimpleAPI/HTTPMethod.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// HTTPMethod.swift
// SimpleAPI
//
// Created by Belal Samy on 23/03/2022.
//

import Foundation

public enum HTTPMethod {
case get(_ path: String = "")
case post(_ path: String = "")
case put(_ path: String = "")
case delete(_ path: String = "")

var name: String {
switch self {
case .get:
return "GET"
case .post:
return "POST"
case .put:
return "PUT"
case .delete:
return "DELETE"
}
}

var id: String {
switch self {
case .get(let path):
return path == "" ? "" : "with path \"\(path)\""
case .post(let path):
return path == "" ? "" : "with path \"\(path)\""
case .put(let path):
return path == "" ? "" : "with path \"\(path)\""
case .delete(let path):
return path == "" ? "" : "with path \"\(path)\""
}
}

}
Loading

0 comments on commit 818df54

Please sign in to comment.