https://github.com/akxo/api-kit
networking framework for making api calls
https://github.com/akxo/api-kit
api client http networking requests rest swift
Last synced: about 1 year ago
JSON representation
networking framework for making api calls
- Host: GitHub
- URL: https://github.com/akxo/api-kit
- Owner: akxo
- License: mit
- Created: 2023-05-05T21:48:30.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-05-10T14:58:49.000Z (almost 3 years ago)
- Last Synced: 2025-03-11T11:42:27.822Z (about 1 year ago)
- Topics: api, client, http, networking, requests, rest, swift
- Language: Swift
- Homepage:
- Size: 33.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# api-kit
a lightweight, flexible, and easy to use networking abstraction layer for making http requests
## installation
### swift package manager (spm)
1. in xcode, select “file” → “add packages...”
2. enter https://github.com/akxo/apikit
### cocoapods
add the pod to your podfile
```ruby
pod 'api-kit'
```
install pod
```ruby
pod install
```
## usage
import the module
```swift
import APIKit
```
create the api client with your baseurl
```swift
let clientBuilder = APIClientBuilder(baseUrl: "your-base-url-here")
let client = clientBuilder.build()
```
there are 3 methods for making network requests depending on your preference:
1. completion handler
```swift
func load(resource: Resource, completion: @escaping (Result) -> Void)
```
2. combine
```swift
func load(resource: Resource) -> AnyPublisher
```
3. async/await
```swift
func load(resource: Resource) async -> Result
```
### example 1 (GET movies)
create response object (make sure it's decodable)
```swift
struct APIMovie: Decodable {
let title: String
let director: String
}
```
build resource
```swift
let resource = Resource<[APIMovie]>(
jsonDecoder: JSONDecoder(),
path: "/movies",
method: .get,
params: ["upcoming": true]
)
```
make the request
#### completion handler
```swift
client.load(resource: resource) { result in
switch result {
case let .success(movies):
// handle movies
case let .failure(error):
// handle error
}
}
```
#### combine
```swift
client.load(resource: resource)
.sink { completion in
switch completion {
case .finished:
// handle completion
case let .failure(error):
// handle error
}
} receiveValue: { movies in
// handle movies
}
```
#### async/await
```swift
let result = await client.load(resource: resource)
switch result {
case let .success(movies):
// handle movies
case let .failure(error):
// handle error
}
```
### example 2 (POST movie)
build resource
```swift
let resource = Resource(
jsonDecoder: JSONDecoder(),
path: "/movies",
method: .post,
params: [
"title": "inception",
"director": "christopher nolan"
]
)
```
make the request like above