Skip to content

Commit

Permalink
🔀 Merge pull request #28 from MrSkwiggs/release/v0.3.1
Browse files Browse the repository at this point in the history
🚀 Release: v0.3.1
  • Loading branch information
MrSkwiggs authored Aug 24, 2020
2 parents 73dbd21 + bdf8029 commit 572479a
Show file tree
Hide file tree
Showing 13 changed files with 501 additions and 385 deletions.
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
# Change Log
All notable changes to this project will be documented in this file

## Unreleased
## [0.3.1 (20200824)]
### Changed

### Fixed
- `NetswiftError.Category` now conforms to `CustomDebugStringConvertible`

## [0.3.0 (20200225)]
### Changed
Expand Down
8 changes: 4 additions & 4 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- Netswift (0.1.5)
- Netswift (0.3.0)

DEPENDENCIES:
- Netswift (from `../`)
Expand All @@ -9,8 +9,8 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
Netswift: 15396e3163f29d7de5555d64ee51b7e3f69bd184
Netswift: 18fab9cac3c3bf03d84eb7cc954503cad4dc5896

PODFILE CHECKSUM: 1b86feeadcafbfdd5e60a1ce0c28934e40ab4904
PODFILE CHECKSUM: 22292e32b33db613b2b3c9b9c1965633bc1528df

COCOAPODS: 1.8.4
COCOAPODS: 1.9.1
4 changes: 2 additions & 2 deletions Example/Pods/Local Podspecs/Netswift.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

566 changes: 282 additions & 284 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions Example/Pods/Target Support Files/Netswift/Netswift.debug.xcconfig

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 28 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,31 @@
[![Swift Package Manager](https://img.shields.io/badge/SPM-compatible-brightGreen)](https://swift.org/package-manager/)

## What
This library takes care of the heavy lifting required to have a reusable & maintainable networking layer in your Swift apps.
It currently allows you to easily write network calls in a very structured and type-safe way. It does so by using protocols with associated types & generic classes & structs very extensively.
**Type-safe network calls made easy**

## Why
Networking in Swift can be tedious from the get go. Type safety & reusability are often overlooked for the sake of getting up to speed. This is where Netswift aims to shine!

Over the past years, my team & I struggled with spaghetti code & an unmaintainable codebase when it came to performing network requests. Due to the nature of api calls, it is quite tough to find a single solution that accommodates most of your needs, all the while remaining flexible & future-proof.

So as a move to improve my Swift skills and get us out of messy situation, I took it upon myself to research & implement a solution that would alleviate those pain-points.
Netswift offers an easy way to perform network calls in a structured and type-safe way.

This framework was heavily inspired by blog posts written by the brilliant [John Sundell](https://www.swiftbysundell.com) and [Ray Wenderlich](https://www.raywenderlich.com/). I highly recommend them if you need to learn & improve your programming skills!.
## Why
Networking in Swift can be tedious from the get go. Type safety & reusability are often overlooked for the sake of getting up to speed. Have a huge file with all your API calls which is getting hard to maintain? This is where Netswift comes in.

# Using Netswift

## TL;DR?
This is how easy it is to perform network calls with Netswift:
```swift
Netswift().peform(StripeAPI.Charges.get(byID: "1234")) { result in
switch result {
case .failure(let error):
// Our request failed: we can use the error to debug it
print(error)

case .success(let charge):
// Our request succeeded: we now have a Charge object from the Stripe API
print(charge.amount)
}
}
```

## Prerequisites
I'm assuming you have an available iOS project set up with this cocoapod, ready to go. If not, please follow the [installation steps](#installation).

Expand All @@ -42,7 +53,7 @@ To facilitate this tutorial, I went ahead and set up a mock API for you to query
In this particular case, and to keep things simple, we can go ahead and define a new `enum`. We'll use it to implement the minimum required protocol functions which will allow us to perform our request.

So go ahead; add a new file to your project and name it however you like. I chose `MyAPI`. Then, don't forget to `import Netswift`, and create your API Container like such:
```
```swift
import Netswift

enum MyAPI {
Expand All @@ -57,7 +68,7 @@ The great thing about Swift's `enum` is that they can also have associated value
So we have our enum. Great. But it doesn't do much. Let's fix that.

Go ahead and define an extension for it which implements the `NetswiftRoute` protocol:
```
```swift
extension MyAPI: NetswiftRoute {
}
```
Expand All @@ -67,12 +78,12 @@ Immediately, the compiler starts complaining. Pressing 'Add protocol stubs' will
- `path`: A specific resource on our API. Unless you're just GET-ing a website, you'll need to define a path.

So let's go ahead and implement those two.
```
```swift
var host: String {
return "my-json-server.typicode.com"
}

var path: String {
var path: String? {
switch self {
case .helloWorld: return "MrSkwiggs/Netswift-HelloWorld/Netswift"
}
Expand All @@ -89,7 +100,7 @@ And that's pretty much everything we need for now. A lot of work is done under t

### Step 3
Now that we have our route setup, all we need to do is implement the `NetswiftRequest` protocol. Let us do just that in another extension:
```
```swift
extension MyAPI: NetswiftRequest {
}
```
Expand All @@ -98,7 +109,7 @@ This time, we don't want to let the compiler add protocol stubs for us just yet.
- A `Response` type. Since Netswift is generic, it doesn't know what kind of data we want from our API's endpoint. If our request defines a type called `Response`, we're good to go. And the best part is, we could also use a `typealias`, and it would just work 👍

So for now, let's just add an internal type named `Response` in our extension:
```
```swift
struct Response: Decodable {
let title: String
}
Expand All @@ -113,7 +124,7 @@ Then, we told the compiler that our `Response` type implements the `Decodable` p
Yet, the compiler is still unhappy. Now's however a good time to let it 'Add protocol stubs'. We're now given a new function called `serialise`. This is the last part we need to define before we are good to go.

So let us implement our `URLRequest` serialisation then, shall we ?
```
```swift
func serialise(_ handler: @escaping NetswiftHandler<URLRequest>) {
handler(.success(URLRequest(url: self.url)))
}
Expand All @@ -131,7 +142,7 @@ Great, that's us pretty much done now!
Now's the moment we've been waiting for: sending out our request!

All we need to do is to actually perform our request. To do so, we can use an instance of the default `Netswift` class. All we need to do is call this:
```
```swift
Netswift().perform(MyAPI.helloWorld) { result in
switch result {
case .failure(let error):
Expand Down
Loading

0 comments on commit 572479a

Please sign in to comment.