Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/barredewe/moyanetworkclient

NetworkClient based on Moya
https://github.com/barredewe/moyanetworkclient

asynchronous cache clean future layer moya network protocol requests sugar swift

Last synced: 16 days ago
JSON representation

NetworkClient based on Moya

Awesome Lists containing this project

README

        




Simple Swift Requests




CocoaPods
Swift5
License
Platform


CodeFactor
CodeFactor
Swift Package Manager

---

## Introduction

MoyaNC is an abstraction over abstraction (thanks to [Moya](https://github.com/Moya/Moya) 🖤🖤🖤) allowing you not to worry about the data mapping layer and just use your requests. The Codable protocol is used for JSON data mapping, all that is needed is to comply with this protocol and specify the type! Additionally, there is the ability to easily and simply cache any requests.

## Example

To run the example project, clone the repo, and run `pod install` from the Example directory first. After some setup, using MoyaNC is really simple. You can access an API like this:

```swift
client = DefaultMoyaNC()

// type 'Test' must be Codable
client.request(.zen) { (result: Result) in
switch result {
case let .success(test):
// do something with the finished object
case let .failure(error):
// do something with error
}
}
```
That's a basic example. Many API requests need parameters.

## Extensions

Moya provides extensions for:
- **Cache**: Allows you to cache request data using a flexible caching policy.
Need to add `pod 'MoyaNetworkClient/Cache'` to the Podfile.
- **FutureResult**: A Future is used to retrieve the result of a concurrent, asynchronous operation. It is a key building block in asynchronous, non-blocking code.
Need to add `pod 'MoyaNetworkClient/Future'` to the Podfile.

### Cache

A simple example to use caching requests:

Definition of caching at the API level:
```swift
enum TestAPI {
case .zen
}

extension TestAPI: MoyaTargetType, CacheTarget {
var cachePolicy: MoyaCachePolicy {
return .returnCacheDataElseLoad
}
}
```
Definition of caching at the request level:
```swift
client = DefaultMoyaNC()

client.request(.zen, cache: .returnCacheDataElseLoad)
```

### FutureResult

Here is an example of the kinds of complex logic possible with Futures:

```swift
client = DefaultMoyaNC()

// type 'Test' must be Codable
client.request(.zen)
.observeSuccess { (test: Test) in /* do something with the finished object */ }
.observeError { error in /* do something with error */) }
.execute()
```

## Installation
### CocoaPods

For MoyaNetworkClient, use the following entry in your Podfile:

```rb
pod 'MoyaNetworkClient'
```

Then run `pod install`.

In any file you'd like to use Moya in, don't forget to
import the framework with `import MoyaNetworkClient`.

### Swift Package Manager

To integrate using Apple's Swift package manager, add the following as a dependency to your `Package.swift`:

```swift
.package(url: "https://github.com/BarredEwe/MoyaNetworkClient.git", .upToNextMajor(from: "1.0.0"))
```

and then specify `"MoyaNetworkClient"` as a dependency of the Target in which you wish to use MoyaNetworkClient.

### Migration Guides

**MoyaNC 3.0**:
- Now the validation of the request occurs by the standard Moya parameter `var validationType: ValidationType`

## Author

BarredEwe, [email protected]

## License

MoyaNC is released under an MIT license. See License for more information.