Skip to content

Commit

Permalink
Merge pull request #1 from afterxleep/develop
Browse files Browse the repository at this point in the history
Changes and updates to work with the demo app && documentation updates
  • Loading branch information
afterxleep authored Dec 27, 2020
2 parents a494801 + 295e912 commit f9eecd8
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 27 deletions.
2 changes: 0 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// Package.swift
// WireKit
//
// © 2020 - Les Mobiles
// MIT License

import PackageDescription

Expand Down
35 changes: 32 additions & 3 deletions README.md
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)
5 changes: 1 addition & 4 deletions Sources/WireKit/Extensions/Encodable+Dictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
// WireKit
//
// Created by Daniel Bernal on 8/11/20.
// © 2020 - Les Mobiles
// MIT License
//


import Foundation

extension Encodable {
public extension Encodable {

var asDictionary: [String: Any] {
guard let data = try? JSONEncoder().encode(self) else { return [:] }
Expand Down
3 changes: 1 addition & 2 deletions Sources/WireKit/Protocols/FNRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// FunctionalNetworking
//
// Created by Daniel Bernal on 8/11/20.
// © 2020 - Les Mobiles
// MIT License
//

import Foundation
Expand Down Expand Up @@ -49,6 +47,7 @@ public extension WKRequest {
var queryParams: WKHTTPParams? { return nil }
var body: WKHTTPParams? { return nil }
var headers: WKHTTPHeaders? { return nil }
var debug: Bool { return false }

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
//
//
// Created by Daniel Bernal on 14/11/20.
// © 2020 - Les Mobiles
// MIT License
//


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
//
//
// Created by Daniel Bernal on 15/11/20.
// © 2020 - Les Mobiles
// MIT License
//


Expand Down Expand Up @@ -88,4 +86,10 @@ public struct WKNetworkDispatcher {
}
}

private func debugMessage(_ message: String) {
#if DEBUG
print("--- WK Request \(message)")
#endif
}

}
2 changes: 0 additions & 2 deletions Tests/WireKitTests/FNAPIClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// WireKit
//
// Created by Daniel Bernal on 18/11/20.
// © 2020 - Les Mobiles
// MIT License
//


Expand Down
2 changes: 0 additions & 2 deletions Tests/WireKitTests/FNDispatcherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// WireKit
//
// Created by Daniel Bernal on 16/11/20.
// © 2020 - Les Mobiles
// MIT License
//


Expand Down
2 changes: 0 additions & 2 deletions Tests/WireKitTests/FNRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// WireKit
//
// Created by Daniel Bernal on 8/11/20.
// © 2020 - Les Mobiles
// MIT License
//


Expand Down
2 changes: 0 additions & 2 deletions Tests/WireKitTests/Mocks/URLProtocolMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// WireKit
//
// Created by Daniel Bernal on 16/11/20.
// © 2020 - Les Mobiles
// MIT License
//


Expand Down
2 changes: 0 additions & 2 deletions Tests/WireKitTests/SampleRequest/Todo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// WireKit
//
// Created by Daniel Bernal on 8/11/20.
// © 2020 - Les Mobiles
// MIT License
//


Expand Down
2 changes: 0 additions & 2 deletions Tests/WireKitTests/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// WireKit
//
// Created by Daniel Bernal on 18/11/20.
// © 2020 - Les Mobiles
// MIT License
//


Expand Down
127 changes: 127 additions & 0 deletions docs/quickStart.md
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)
}

}

```
25 changes: 25 additions & 0 deletions docs/wkrequest.md
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.

0 comments on commit f9eecd8

Please sign in to comment.