Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/darrarski/swiftendpoint

Lightweight library providing higher level of abstraction for implementing networking in iOS and macOS applications
https://github.com/darrarski/swiftendpoint

combine-framework endpoint networking swift swift-package

Last synced: about 1 month ago
JSON representation

Lightweight library providing higher level of abstraction for implementing networking in iOS and macOS applications

Awesome Lists containing this project

README

        

# 📦 SwiftEndpoint

![Swift v5.1](https://img.shields.io/badge/swift-v5.1-orange.svg)
![Swift Package Manager](https://img.shields.io/badge/swift%20package%20manager-✓-green.svg)
![platforms iOS macOS](https://img.shields.io/badge/platforms-iOS%20macOS-blue.svg)

**SwiftEndpoint** is a lightweight library providing higher level of abstraction for implementing networking in iOS and macOS applications.

## 🛠 Tech stack

- [Swift](https://swift.org/) 5.1
- [Xcode](https://developer.apple.com/xcode/) 11.3.1
- [iOS](https://www.apple.com/pl/ios/) 13.0
- [macOS](https://www.apple.com/pl/macos/) 10.15

## 📝 Description

**`Endpoint`** is a generic function that transforms some `Request` into some `Response` publisher:

```swift
typealias Endpoint = (Request) -> AnyPublisher
```

### 🧩 Foundation URL networking

Set of helpers for building API clients based on the native [Foundation](https://developer.apple.com/documentation/foundation)'s networking.

**`urlEndpoint`** function creates an `Endpoint` that uses Foundation's networking:

```swift
func urlEndpoint(
requestFactory: @escaping URLRequestFactory,
publisherFactory: @escaping URLResponsePublisherFactory,
responseValidator: @escaping URLResponseValidator,
responseDecoder: @escaping URLResponseDecoder
) -> Endpoint
```

**`URLRequestFactory`** is a generic function that transforms some `Request` into `URLRequest`, optionally throwing an error:

```swift
typealias URLRequestFactory = (Request) throws -> URLRequest
```

**`URLResponsePublisherFactory`** is a function that transforms `URLRequest` into `URLResponsePublisher`:

```swift
typealias URLResponsePublisherFactory = (URLRequest) -> URLResponsePublisher
```

Convenience extension allows to use `URLSession` as a `URLResponsePublisherFactory`:

```swift
extension URLSession {
var urlResponsePublisherFactory: URLResponsePublisherFactory { get }
}
```

**`URLResponsePublisher`** is a combine publisher emitting network responses or failing with networking error:

```swift
typealias URLResponsePublisher = AnyPublisher<(data: Data, response: URLResponse), Error>
```

**`URLResponseValidator`** is a function that validates response `Data` and `URLResponse`, optionally throwing validation error:

```swift
typealias URLResponseValidator = (Data, URLResponse) throws -> Void
```

**`URLResponseDecoder`** is a generic function that transforms response `Data` and `URLResponse` into some `Response`, optionally throwing decoding error:

```swift
typealias URLResponseDecoder = (Data, URLResponse) throws -> Response
```

## 🧰 Installation

**SwiftEndpoint** is compatible with [Swift Package Manager](https://swift.org/package-manager/). You can add it as a dependency to your [Xcode project](https://developer.apple.com/documentation/xcode/adding_package_dependencies_to_your_app) or [swift package](https://github.com/apple/swift-package-manager/blob/master/Documentation/Usage.md#defining-dependencies).

## 🛠 Development

Running tests:

```sh
swift test
```

Developing in [Xcode](https://developer.apple.com/xcode/):

```sh
swift package generate-xcodeproj
open -a SwiftEndpoint.xcodeproj
```

## ☕️ Do you like the project?

Buy Me A Coffee

## 📄 License

Copyright © 2020 Dariusz Rybicki Darrarski

License: [GNU GPLv3](LICENSE)