Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vinhnx/nvnetworkrequest
Simple Alamofire network layer.
https://github.com/vinhnx/nvnetworkrequest
alamofire network
Last synced: about 1 month ago
JSON representation
Simple Alamofire network layer.
- Host: GitHub
- URL: https://github.com/vinhnx/nvnetworkrequest
- Owner: vinhnx
- License: mit
- Created: 2021-10-30T10:21:55.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-10-30T12:07:50.000Z (about 3 years ago)
- Last Synced: 2024-10-25T04:26:06.370Z (3 months ago)
- Topics: alamofire, network
- Language: Swift
- Homepage:
- Size: 16.6 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NVNetworkRequest
Alamofire network layer
### Installation
Add this to your Package dependencies:
```swift
dependencies: [
.package(url: "https://github.com/vinhnx/NVNetworkRequest", .upToNextMajor(from: "0.1.3"))
],
```### Usage
Conform to `EndpointEnvironment` protocol with environment enum (eg: development, staging, production...):
```swift
import NVNetworkRequestenum Enviroment: EndpointEnvironment {
case development
case staging
case productionvar baseURL: String {
switch self {
case .development, .staging:
return "staging.quotable.io"
case .production:
return "api.quotable.io"
}
}
}
```Conform `EndpointPath` with any request request path:
```swift
enum APIPath: EndpointPath {
case randomvar path: String {
switch self {
case .random: return "random"
}
}
}
```Subclass `NVNetworkRequest`
```swift
class QuoteNetworkRequest: NVNetworkRequest {
func fetch(
tags: [String]? = nil,
completion: @escaping ((Result) -> Void)
) {
var params: Parameters = [:]
if let tags = tags {
params["tags"] = tags.joined(separator: ",")
}sendRequest(endpoint: APIPath.random.endpoint, model: Quote.self, method: .get, params: params, paramsEncoding: URLEncoding.queryString, completion: completion)
}
}
```Then, configure API environment on app start, typically from AppDelegate didFinishLaunchingWithOptions:
```swift
Endpoint.configureEnvironment(Enviroment.production)
```Usage:
```swift
QuoteNetworkRequest().fetch { result in
switch result {
case .success(let response):
debugPrint(response)
case .failure(let error):
debugPrint(error)
}
}
```