https://github.com/sinakhanjani/restfulapi
RestfulAPI is a lightweight yet powerful Swift framework for making RESTful API requests using URLSession and Combine, featuring built-in authentication management, custom headers, and support for synchronous and asynchronous execution. π
https://github.com/sinakhanjani/restfulapi
combine-framework restful-api swift urlsession
Last synced: 10 months ago
JSON representation
RestfulAPI is a lightweight yet powerful Swift framework for making RESTful API requests using URLSession and Combine, featuring built-in authentication management, custom headers, and support for synchronous and asynchronous execution. π
- Host: GitHub
- URL: https://github.com/sinakhanjani/restfulapi
- Owner: sinakhanjani
- Created: 2025-02-03T14:43:32.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-03T17:23:05.000Z (over 1 year ago)
- Last Synced: 2025-02-17T19:18:49.024Z (over 1 year ago)
- Topics: combine-framework, restful-api, swift, urlsession
- Language: Swift
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# RestfulAPI
**RestfulAPI** is a lightweight, yet powerful Swift framework designed for seamless interaction with RESTful web services. It provides an easy-to-use API built on top of `URLSession` and `Combine`, supporting modern asynchronous programming patterns. With built-in authentication management, multiple request execution methods, and structured response handling, this framework simplifies network operations in iOS applications.
## β¨ Key Features
- **Easy API Requests** β Simplified request handling with `URLSession` and `Combine`
- **Multiple Execution Methods** β Support for `DispatchQueue`, `DispatchSemaphore`, and `async/await`
- **Authentication Management** β Built-in token-based authentication with `Authentication` and `AuthorizationType`
- **Custom Headers Support** β Easily attach custom headers to API requests
- **Automatic Encoding & Decoding** β JSON serialization support for request bodies and responses
- **Swift Package Manager (SPM) Support** β Easy integration into projects
## π Installation
### Swift Package Manager (SPM)
Add the following dependency to your `Package.swift` file:
```swift
dependencies: [
.package(url: "https://github.com/your-repo/RestfulAPI.git", from: "1.0.0")
]
```
Or use Xcodeβs `File > Swift Packages > Add Package Dependency` and enter the repository URL.
## βοΈ Configuration
Before making requests, configure the API settings:
```swift
RestfulAPIConfiguration().setup { () -> APIConfiguration in
APIConfiguration(baseURL: "https://your-api.com",
headers: ["Authorization": "Bearer token"])
}
```
## π Authentication
This framework provides built-in authentication management:
### 1οΈβ£ Setting Authentication Type
You can set the type of authentication using `AuthorizationType`:
```swift
public enum AuthorizationType: Codable {
case bearerToken
case token
case basicAuth
}
```
### 2οΈβ£ Managing Authentication Tokens
The `Authentication` class helps in managing tokens:
```swift
var auth = Authentication.auth1
// Authenticate a user
auth.authenticate(token: "your_token_here")
// Check if user is logged in
if auth.isLogin {
print("User is logged in")
}
// Logout user
auth.logout()
```
## π Usage
### 1οΈβ£ Fetch with `Combine`
Use `fetchDataTaskPublisherRequest` for handling API responses using `Combine`:
```swift
var fetchRequest = RestfulAPI(path: "/todos/1")
.with(method: .GET)
fetchRequest
.fetchDataTaskPublisherRequest { result in
switch result {
case .success(let example):
print(example)
case .failure(let error):
print("Error:", error)
}
}
```
### 2οΈβ£ Fetch with `URLSession` (Completion Handler)
Use `sendURLSessionRequest` for handling API responses with a completion handler:
```swift
RestfulAPI(path: "/todos/1")
.sendURLSessionRequest { result in
switch result {
case .success(let example):
print(example)
case .failure(let error):
print("Error:", error)
}
}
```
### 3οΈβ£ Fetch with `DispatchSemaphore` (Synchronous Request)
Use this method when you need to wait for the API response synchronously:
```swift
let response = try? RestfulAPI(path: "/todos/1")
.sendURLSessionRequest()
```
### 4οΈβ£ Adding Headers
You can add custom headers to your API requests:
```swift
RestfulAPI(path: "/todos/1")
.with(headers: ["Custom-Header": "Value"])
```
### 5οΈβ£ Encoding & Decoding Response
The framework provides built-in methods for encoding requests and decoding responses:
```swift
public func decodeResponse(data: Data) throws -> Response
public func encodeResponse() throws -> Data
```
### 6οΈβ£ Available HTTP Methods
The following HTTP methods are supported:
```swift
public enum Method: String {
case GET, POST, PUT, DELETE, PATCH
}
```
## π License
This project is licensed under the MIT License. See the `LICENSE` file for details.
## π‘ Contributing
Contributions are welcome! Feel free to create issues or submit pull requests.
---
This `README.md` now includes structured documentation with headers, proper explanations, and a cleaner format for better readability.