Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/vanities/fourchanapi

Swift Combine adapter for 4chan's public API
https://github.com/vanities/fourchanapi

4chan combine swift

Last synced: about 1 month ago
JSON representation

Swift Combine adapter for 4chan's public API

Awesome Lists containing this project

README

        

# FourChan

A Swift package for the [4chan.org Read-only HTTP/JSON API](https://github.com/4chan/4chan-API).

# Features

- Typesafe URLs for
- the 4chan API (FourChanAPIEndpoint)
- the experimental 4chan mobile search API (FourChanAPIEndpoint.search)
- the 4chan web site. (FourChanWebEndpoint)
- Codable structs for all the 4chan API result types (e.g. Post).
- the structs implement Identifiable where possible.
- Helpers for making network requests in a variety of styles:
- using callbacks.
- using Combine publishers.
- using Combine/SwiftUI ObsevableObjects.

# Usage

This package supports both callback and Combine-based networking.

If you load the package in an environment (like Linux) that doesn't support Combine, then the Combine APIs won't be available.

An example of using the API with minimal helper functions:

```
import Foundation
import FourChan

let boards = try? JSONDecoder().decode(Boards.self,
from:Data(contentsOf:FourChanAPIEndpoint.boards.url()))

```

An example of callback-based networking is:

```
import FourChan

FourChanAPIService.shared.GET(endpoint:.boards) { (result: Result) in
print(result)
}
```

An example of Combine-based networking:

```
import Combine

FourChanService.shared.posts(board:"w")
.tryMap{ postInContext in
postInContext.imageURL
}
.sink(
receiveCompletion: { completion in
if case .failure(_) = completion {
print(".sink() failed ", String(describing: completion))
}
},
receiveValue: { imageURL in
print(imageURL)
}
)
```

A SwiftUI example:

```
import FourChan
import SwiftUI

struct FourChanBoardsView : View {
var loader: FourChanLoader = FourChanLoader()

var body: some View {
var categories = loader.data?.categories ?? []

return List {
ForEach(0..