-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from afterxleep/develop
Changes and updates to work with the demo app && documentation updates
- Loading branch information
Showing
14 changed files
with
192 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,6 @@ | |
// Package.swift | ||
// WireKit | ||
// | ||
// © 2020 - Les Mobiles | ||
// MIT License | ||
|
||
import PackageDescription | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,39 @@ | ||
# WireKit | ||
|
||
|
||
A functional, (yet simple) networking library based on Combine, Codable and URLSession publishers. | ||
A simple networking library based on Combine, Codable and URLSession publishers. | ||
|
||
WireKit is designed to facilitate consumption of RestFul APIs and takes care of fetching and decoding JSON data, gracefully handling errors so you can focus on what's important in your app. | ||
|
||
### Usage | ||
## Features | ||
- Super simple configuration | ||
- Simple REST API consumption | ||
- Automatic JSON encoding/decoding via Codable | ||
- Combine backed async requests | ||
- Easy error handling | ||
|
||
Coming soon... | ||
## Requirements | ||
- iOS 13+ / macOS 10.15+ / tvOS 13.0+ / watchOS 5.0+ | ||
- Xcode 12+ | ||
- Swift 5.3+ | ||
|
||
## Installation | ||
|
||
### Swift Package Manager | ||
|
||
The fastest way to install is via SPM. Just add a new package using this repo URL and point it to the `main` branch. | ||
|
||
Versioning and support for other package managers (Cocoapods & Carthage) coming soon. | ||
|
||
## Sample App | ||
The sample application is a simple Todo List app that leverages Wirekit to easily manage items. | ||
|
||
It's available [in this Repo](https://github.com/afterxleep/WireKitSample). | ||
|
||
## Usage | ||
|
||
Check out the Quick Start guide [here](docs/quickStart.md) | ||
|
||
## WKRequest | ||
|
||
Futher documentation on WKRequest is available [here](docs/wkrequest.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,6 @@ | |
// | ||
// | ||
// Created by Daniel Bernal on 14/11/20. | ||
// © 2020 - Les Mobiles | ||
// MIT License | ||
// | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,6 @@ | |
// WireKit | ||
// | ||
// Created by Daniel Bernal on 18/11/20. | ||
// © 2020 - Les Mobiles | ||
// MIT License | ||
// | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,6 @@ | |
// WireKit | ||
// | ||
// Created by Daniel Bernal on 16/11/20. | ||
// © 2020 - Les Mobiles | ||
// MIT License | ||
// | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,6 @@ | |
// WireKit | ||
// | ||
// Created by Daniel Bernal on 8/11/20. | ||
// © 2020 - Les Mobiles | ||
// MIT License | ||
// | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,6 @@ | |
// WireKit | ||
// | ||
// Created by Daniel Bernal on 16/11/20. | ||
// © 2020 - Les Mobiles | ||
// MIT License | ||
// | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,6 @@ | |
// WireKit | ||
// | ||
// Created by Daniel Bernal on 8/11/20. | ||
// © 2020 - Les Mobiles | ||
// MIT License | ||
// | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,6 @@ | |
// WireKit | ||
// | ||
// Created by Daniel Bernal on 18/11/20. | ||
// © 2020 - Les Mobiles | ||
// MIT License | ||
// | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# Quick Start | ||
|
||
### Define a Model, based on your Rest API | ||
|
||
Define your model, make sure it conforms to Codable and it matches your API JSON structure. | ||
|
||
``` swift | ||
import Foundation | ||
|
||
struct Todo: Codable, Identifiable { | ||
let userId: Int | ||
let id: Int? | ||
let title: String | ||
let completed: Bool | ||
} | ||
``` | ||
|
||
### Define a set of WireKit requests | ||
|
||
All your REST API actions are defined using `WKRequest`. You can also group multiple requests based on and endpoint or specific functionality. | ||
|
||
Each request has it's own set of properties that you can define as needed. (Incluiding Output Types, URL/Path, HTTPMethod, Body, Headers and more) | ||
|
||
Here we are defining Find, Delete and Add requests for a simple Todo API. | ||
|
||
``` swift | ||
import WireKit | ||
|
||
struct TodoAPI { | ||
private struct APIConstants { | ||
static var path = "/todos" | ||
static var root = "/" | ||
} | ||
|
||
// Find all Todo Items | ||
struct FindAll: WKRequest { | ||
typealias ReturnType = [Todo] | ||
var path: String = APIConstants.path | ||
} | ||
|
||
// Delete item with ID | ||
struct Delete: WKRequest { | ||
typealias ReturnType = Empty | ||
var path: String | ||
var method: WKHTTPMethod = .delete | ||
|
||
init(_ id: Int) { | ||
path = "\(APIConstants.path)/\(id)" | ||
} | ||
} | ||
|
||
// Adds a new Item | ||
struct Add: WKRequest { | ||
var path: String = APIConstants.path | ||
typealias ReturnType = Todo | ||
var method: WKHTTPMethod = .post | ||
var body: WKHTTPParams? | ||
|
||
init(_ todoItem: Todo) { | ||
self.body = todoItem.asDictionary | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### Setup your APIClient and Perform a request | ||
|
||
Initialize your APIClient and use the internal `dispatch` function to dispatch a requests. | ||
|
||
Since Wirekit relies on combine, and it automatically decodes API responses based on your defined Return Type, performin async requests is super simple. | ||
|
||
``` swift | ||
|
||
import Foundation | ||
import WireKit | ||
import Combine | ||
|
||
class YourModel { | ||
|
||
private var apiClient: WKAPIClient | ||
private var cancellables = [AnyCancellable]() | ||
|
||
private enum Constants { | ||
static let apiURL = "https://jsonplaceholder.typicode.com" | ||
} | ||
|
||
init() { | ||
apiClient = WKAPIClient(baseURL: Constants.apiURL) | ||
loadData() | ||
} | ||
|
||
private func loadData() { | ||
|
||
// Perform a request from the TodoAPI | ||
// You can pass parameters and other data here. | ||
apiClient.dispatch(TodoAPI.FindAll()) | ||
|
||
// Since we are displaying results in the UI, receive on Main Thread | ||
.receive(on: DispatchQueue.main) | ||
|
||
// Observe for Returned values | ||
.sink( | ||
receiveCompletion: { result in | ||
|
||
// Hide our loader on completion | ||
self.isLoading = false | ||
|
||
// Act on when we get a result or failure | ||
switch result { | ||
case .failure(let error): | ||
// Handle API response errors here (WKNetworkRequestError) | ||
print("Error loading data: \(error)") | ||
default: break | ||
} | ||
}, | ||
receiveValue: { value in | ||
// Do something with the received data here... | ||
// ... | ||
}) | ||
|
||
// Store the cancellable in a set (to hold a ref) | ||
.store(in: &cancellables) | ||
} | ||
|
||
} | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# WKRequest | ||
|
||
All your API requests should go through `WKRequest`. Here's the parameter list. | ||
|
||
### Path | ||
`String` - The URL or path to call. | ||
|
||
### Method | ||
`Enum ` - The HTTP method for this specific endpoint. Available values are: | ||
|
||
* .get | ||
* .post | ||
* .put | ||
* .delete | ||
|
||
### Query Params | ||
`[String: String]` - A set of query parameters to append to your URL | ||
|
||
|
||
### Body | ||
`[String: String]` - Dictionary to be sent as the request body. WireKit provides an `asDictionary` extension method to convert any Codable object into a valid dictionary. | ||
|
||
### Headers | ||
`[String: String]` - Dictionary of headers to be sent. By default Wirekit automatically adds `Content-Type` and `Accept` headers so you don't have to manually addd them here. | ||
|