Simple Dynamic Framework, providing abstraction over Foundation
's networking classes
NetworkingKit
contains wrapper classes of URLSession
that give you the option to make a API call by subclassing Endpoint
and providing network specific settings as overrides in your class.
By doing this, NetworkingKit
is able to achieve syntax of web call at user level (app level) that look like:
SampleGet.Call { result, error in
// Where `result` is of your custom type set as Generic Type
// and `error` is of kind Endpoint.Error
}
or
SamplePost.Call(withRequestData: data) { result, error in
// Where `data` is of your custom type set as Generic Type
// Where `result` is of your custom type set as Generic Type
// and `error` is of kind Endpoint.Error
}
Build using Swift 5, XCode 13, supports iOS 12.0+
- Import the
NetworkingKit
framework to your file.
import NetworkingKit
- Subclass either
Endpoint.WithRequestAndResponseType<RequestDataType: Encodable, ResponseDataType: Codable>
or
Endpoint.WithResponseTypeOnly<ResponseDataType: Codable>
Depending on whether you would like to send http request body in your call.
- Provide custom
RequestDataType
and/orResponseDataType
to your liking - Set various communication parameters like
host
,scheme
,port
,timeout
, etc... by overriding the inherited computed variables in your subclass - Call the inherited constructor of your subclass with appropriate completion block (the parameter types are inherited automatically) and in the case of
Endpoint.WithRequestAndResponseType
provide your reqest data too as init parameter. - By default
NetworkingKit
, usesJSONEncoder
andJSONDecoder
classes for parsing and conversion of the request data type to raw http body data and response data to the custom type, but if these conversions are not suitable for your case, you can overrideencodeRequest()
anddecodeResponse()
to provide custom conversions to fit your needs.
/// This is how you define a sample GET endpoint
final class Call: Endpoint.WithResponseTypeOnly<Response> {
// Keep in mind that you should override only properties
// for which you don't like the inherited default values
override var host: String { return "postman-echo.com" }
override var path: String { return "/get" }
override var queryItems: [URLQueryItem] {
return [URLQueryItem(name: "foo1", value: "bar1"),
URLQueryItem(name: "foo2", value: "bar2")] }
}
where Response
is:
struct Response: Codable {
let args: Argument
let headers: Headers
let url: String
enum CodingKeys: String, CodingKey {
case args = "args"
case headers = "headers"
case url = "url"
}
}
struct Argument: Codable {...}
struct Headers: Codable {...}
and you call the enpoint like:
Call { result, error in
// here result will be of type `Response`
// error will be of enum case of type `Endpoint.Error`
}
- Go to your XCode project settings tap on the project name under section "PROJECT"
- Tap on tab "Package Dependencies"
- Add the following package
https://github.com/stoqn4opm/NetworkingKit.git
- Link with appropriate targets
- In your
Cartfile
addgithub "stoqn4opm/NetworkingKit"
- Link the build framework with the target in your XCode project
For detailed instructions check the official Carthage guides here
- In your
Podfile
addpod 'NetworkingKit', :git => 'https://github.com/stoqn4opm/NetworkingKit.git'
- execute
pod install
For detailed instructions check the official CocoaPods guides here
- Download the project and build the shared target called
NetworkingKit
- Add the product in the list of "embed frameworks" list inside your project's target or create a work space with NetworkingKit and your project and link directly the product of NetworkingKit's target to your target "embed frameworks" list
The framework is licensed under MIT licence. For more information see file LICENCE