This pacakge was created to give you a simple easey to use genric network layer to add to your project. And even if you want to build your own genric network layer this will give you a good start. Good Luck and enjoy 😉
Utilizing this API is designed to be straightforward and effortles. first thing you need to do is to create enum
that conforms to EndPointProtocol
like this one:
enum EndpointMock: EndPointProtocol {
case validUrl
var scheme: APISchemeType {
switch self {
case .validUrl:
.https
}
}
var host: String? {
switch self {
case .validUrl:
return ""
}
}
var path: String {
switch self {
case .validUrl:
return ""
}
}
var queryItems: [URLQueryItem]? {
return nil
}
var urlRequest: URLRequest? {
guard let url else { return nil }
let urlRequest = URLRequest(url: url)
switch self {
case .validUrl:
return nil
}
}
}
At this point every case in the enum will represent a certain api call. The url
proprty in the EndPointProtocol
have it's own implementation so you don't have to do it:
public extension EndPointProtocol {
var url: URL? {
var urlComponents = URLComponents()
urlComponents.scheme = scheme.rawValue
urlComponents.host = host
urlComponents.path = path
urlComponents.queryItems = queryItems
return urlComponents.url
}
}
if you want to make a custome url session or add headers to the request. Use the urlRequest
property in the EndPointProtocol
and custome the request based on the enum case.
Then you are ready to use the GenricNetworkLayer
struct.
GenricNetworkLayer
is a struct that containes two funcations public func data<T: Codable>(for endpoint: EndPointProtocol) async throws -> (result: T, statusCode: Int) where T: Codable
and public func data(from endPoint: EndPointProtocol) async throws -> urlDataResult
both of them take instance of EndPointProtocol
as an input. But the diffrent is the output.
The first one will fetch the data from the api then decode it using the JsonDecoder
and output the result with the status code in a tuple.
let resultedData: (result: Int, statusCode: Int) = try! await genericNetworkLayer.data<Int>(for: endPoint)
// This will decode the data after fetching it
// from the api to Int and output
// the result with the status code
The second one was made to give you the ablitiy to have your own decoding logic, so it just fetches the data and output it with the status code in a tuple calle urlDataResult
, which is a typealias.
let resultedData = try await genericNetworkLayer.data(from: endpoint)
// The resultedData will contain (Data, statuscode) as a tuple
For the testing part all you have to do to test the GenricNetworkLayer
implementation is to make a subclass of the URlProtocol
for refrence look at the GenericNetworkLayerTests.swift
- Adding more URlSession methods to the struct.
- Scalible for any app size
This pacakge was created by Eng.Omar Elsayed to help the iOS comuntity and make there life easir. if you have any suggestions contact me at eng.omar.elsayed@hotmail.com