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.
- Easy API Requests – Simplified request handling with
URLSessionandCombine - Multiple Execution Methods – Support for
DispatchQueue,DispatchSemaphore, andasync/await - Authentication Management – Built-in token-based authentication with
AuthenticationandAuthorizationType - 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
Add the following dependency to your Package.swift file:
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.
Before making requests, configure the API settings:
RestfulAPIConfiguration().setup { () -> APIConfiguration in
APIConfiguration(baseURL: "https://your-api.com",
headers: ["Authorization": "Bearer token"])
}This framework provides built-in authentication management:
You can set the type of authentication using AuthorizationType:
public enum AuthorizationType: Codable {
case bearerToken
case token
case basicAuth
}The Authentication class helps in managing tokens:
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()Use fetchDataTaskPublisherRequest for handling API responses using Combine:
var fetchRequest = RestfulAPI<Empty, Example>(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)
}
}Use sendURLSessionRequest for handling API responses with a completion handler:
RestfulAPI<Empty, Example>(path: "/todos/1")
.sendURLSessionRequest { result in
switch result {
case .success(let example):
print(example)
case .failure(let error):
print("Error:", error)
}
}Use this method when you need to wait for the API response synchronously:
let response = try? RestfulAPI<Empty, Example>(path: "/todos/1")
.sendURLSessionRequest()You can add custom headers to your API requests:
RestfulAPI<Empty, Example>(path: "/todos/1")
.with(headers: ["Custom-Header": "Value"])The framework provides built-in methods for encoding requests and decoding responses:
public func decodeResponse(data: Data) throws -> Response
public func encodeResponse() throws -> DataThe following HTTP methods are supported:
public enum Method: String {
case GET, POST, PUT, DELETE, PATCH
}This project is licensed under the MIT License. See the LICENSE file for details.
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.