https://github.com/vanities/fourchanapi
Swift Combine adapter for 4chan's public API
https://github.com/vanities/fourchanapi
combine swift
Last synced: 8 months ago
JSON representation
Swift Combine adapter for 4chan's public API
- Host: GitHub
- URL: https://github.com/vanities/fourchanapi
- Owner: vanities
- License: mit
- Created: 2022-05-28T02:19:45.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-03-28T23:59:12.000Z (over 1 year ago)
- Last Synced: 2025-02-03T11:23:31.112Z (10 months ago)
- Topics: combine, swift
- Language: Swift
- Homepage:
- Size: 28.3 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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..