EasyNet
is a simple and efficient networking library for iOS/macOS, built in Swift. It supports making HTTP requests using the most common HTTP methods (GET, POST, PUT, DELETE), integrates with the Combine
framework for reactive programming, and makes it easy to work with JSON responses, multipart form uploads, and custom headers.
- GET, POST, PUT, DELETE Requests: Support for standard HTTP methods.
- Codable Encoding/Decoding: Automatically encodes and decodes JSON data using Swift's
Codable
. - Multipart File Uploads: Upload images using
multipart/form-data
. - Authorization Header: Support for adding authorization tokens for secure communication.
- Combine Integration: Built-in support for Combine framework to handle asynchronous responses.
- Custom Headers: Allows the addition of custom headers to requests.
- Error Handling: Handles HTTP errors like unauthorized access, validation errors, and server errors.
You can integrate EasyNet
into your iOS or macOS project using Swift Package Manager.
-
Open your Xcode project.
-
Go to
File
→Swift Packages
→Add Package Dependency
. -
Enter the URL of this repository:
https://github.com/rezaulislamtarek/EasyNet.git
-
Choose the version and complete the integration.
To start using EasyNet
, initialize it with your base URL and optionally with an authorization token.
let easyNet = EasyNet(baseUrl: "https://api.example.com", token: "your-api-token")
Fetch data using a GET request. Here’s an example of how to decode the response into a Codable object:
easyNet.fetchData(endPoint: "users", responseType: User.self)
.sink(receiveCompletion: { completion in
switch completion {
case .finished:
print("Request successful")
case .failure(let error):
print("Error: \(error)")
}
}, receiveValue: { response in
print("User Data: \(response)")
})
Send a POST request with a request body. Here's how to send data and receive a response:
let user = User(name: "John Doe", email: "[email protected]")
easyNet.postData(endPoint: "users", requestBody: user, responseType: UserResponse.self)
.sink(receiveCompletion: { completion in
switch completion {
case .finished:
print("Request successful")
case .failure(let error):
print("Error: \(error)")
}
}, receiveValue: { response in
print("User Created: \(response)")
})
Update an existing resource with a PUT request:
easyNet.putData(endPoint: "users/1", requestBody: updatedUser, responseType: UserResponse.self)
.sink(receiveCompletion: { completion in
switch completion {
case .finished:
print("Request successful")
case .failure(let error):
print("Error: \(error)")
}
}, receiveValue: { response in
print("User Updated: \(response)")
})
Delete a resource with a DELETE request:
easyNet.deleteData(endPoint: "users/1", responseType: UserResponse.self)
.sink(receiveCompletion: { completion in
switch completion {
case .finished:
print("Request successful")
case .failure(let error):
print("Error: \(error)")
}
}, receiveValue: { response in
print("User Deleted: \(response)")
})
To upload an image (multipart form-data):
let image = UIImage(named: "profile.jpg")!
easyNet.uploadMultipartFormData(endPoint: "upload", responseType: UploadResponse.self, image: image, keyForImage: "file")
.sink(receiveCompletion: { completion in
switch completion {
case .finished:
print("Request successful")
case .failure(let error):
print("Error: \(error)")
}
}, receiveValue: { response in
print("File Uploaded: \(response)")
})
EasyNet handles various types of errors through the EasyNetError enum:
invalidURL: The URL provided is invalid.
badServerResponse: The server response is not valid.
unauthorized: The request returns a 401 Unauthorized error.
validationError: The request returns a 422 Validation error.
bodyParseError: Issue encoding/decoding the request body.
unknown: Any other error not covered by the above.
easyNet.fetchData(endPoint: "your-endpoint", responseType: YourResponseType.self)
.sink(receiveCompletion: { completion in
switch completion {
case .finished:
print("Request successful")
case .failure(let error):
if let easyNetError = error as? EasyNetError {
switch easyNetError {
case .unauthorized:
print("Unauthorized request")
case .validationError:
print("Validation error")
default:
print("Error: \(easyNetError)")
}
}
}
}, receiveValue: { response in
print("Response: \(response)")
})
We welcome contributions to EasyNet! If you find a bug or want to add a feature, feel free to fork the repository and submit a pull request.
Fork the repository.
Create a new branch (git checkout -b feature-branch).
Make your changes (git commit -am 'Add new feature').
Push to your branch (git push origin feature-branch).
Open a pull request.
Rezaul Islam