https://github.com/anbalagand/eaglenet
Simple lightweight networking layer written on top of URLSession. This will provide a clean, separate layer for networking.
https://github.com/anbalagand/eaglenet
async async-await fileupload http https interceptor ios json macos networking parsing swift tvos urlsession
Last synced: 5 months ago
JSON representation
Simple lightweight networking layer written on top of URLSession. This will provide a clean, separate layer for networking.
- Host: GitHub
- URL: https://github.com/anbalagand/eaglenet
- Owner: AnbalaganD
- License: mit
- Created: 2024-08-19T14:29:28.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-18T06:12:06.000Z (over 1 year ago)
- Last Synced: 2024-11-07T16:48:11.913Z (over 1 year ago)
- Topics: async, async-await, fileupload, http, https, interceptor, ios, json, macos, networking, parsing, swift, tvos, urlsession
- Language: Swift
- Homepage:
- Size: 30.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
This library aims to provide a simple and elegant approach to writing network requests. It's designed to be lightweight, adding a thin layer on top of `URLSession` without excessive engineering overhead.
#### Our primary objectives are:
- **Lightweight networking library:** Keep the library small and efficient.
- **Easy to maintain:** Prioritize clear code and modular design for maintainability.
- **Beginner friendly:** Make the library accessible to developers of all levels.
- **Well documented:** Provide comprehensive documentation to guide usage.
- **Customizable and testable:** Allow for flexibility and ensure code quality through testing.
Currently, this library supports basic HTTP data requests (`GET`, `POST`, `PUT`, `DELETE`), custom HTTP methods, and includes a small file upload feature using `multipart/form-data`. These capabilities address the majority of network communication needs in most applications.
For detailed information on feature status, please refer to the [Roadmap](https://github.com/AnbalaganD/EagleNet/wiki/Roadmap) file.
See the complete documentation here: [Documentation](https://swiftpackageindex.com/AnbalaganD/EagleNet/main/documentation/eaglenet)
### Swift Package Manager (SPM)
[](https://swiftpackageindex.com/AnbalaganD/EagleNet) [](https://swiftpackageindex.com/AnbalaganD/EagleNet)
EagleNet is available through [SPM](https://swiftpackageindex.com/AnbalaganD/EagleNet). Use the URL below to add it as a dependency.
```swift
dependencies: [
.package(url: "https://github.com/AnbalaganD/EagleNet", .upToNextMajor(from: "2.0.3"))
]
```
### Making a GET Request
```swift
import EagleNet
struct User: Decodable {
let id: Int
let name: String
let email: String
}
// Basic GET request
let user: User = try await EagleNet.get(
url: "https://api.example.com/users/1"
)
// Get raw response data
let (data, response) = try await EagleNet.get(
url: "https://api.example.com/users/1"
)
```
### Making a POST Request
```swift
struct CreateUser: Encodable {
let name: String
let email: String
}
struct UserResponse: Decodable {
let id: Int
let name: String
}
let newUser = CreateUser(name: "Anbalagan D", email: "anbu94p@gmail.com")
let response: UserResponse = try await EagleNet.post(
url: "https://api.example.com/users",
body: newUser
)
// Get raw response data
let (data, urlResponse) = try await EagleNet.post(
url: "https://api.example.com/users",
body: newUser
)
```
### File Upload
```swift
let imageData = // ... your image data ...
let response: UploadResponse = try await EagleNet.upload(
url: "https://api.example.com/upload",
parameters: [
.file(
key: "avatar",
fileName: "profile.jpg",
data: imageData,
mimeType: .jpegImage
),
.text(key: "username", value: "Anbu")
],
progress: { bytesTransferred, totalBytes in
let progress = Float(bytesTransferred) / Float(totalBytes)
print("Upload progress: \(Int(progress * 100))%")
}
)
// Get raw response data
let (data, urlResponse) = try await EagleNet.upload(
url: "https://api.example.com/upload",
parameters: [
.file(key: "avatar", fileName: "profile.jpg", data: imageData, mimeType: .jpegImage)
]
)
```
### Request Interceptors
```swift
struct AuthInterceptor: RequestInterceptor {
let token: String
func modify(request: URLRequest) async throws -> URLRequest {
var modifiedRequest = request
modifiedRequest.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
return modifiedRequest
}
}
// Add interceptor to network service
EagleNet.addRequestInterceptor(
AuthInterceptor(token: "your-auth-token")
)
```
### Response Interceptors
```swift
struct LoggingInterceptor: ResponseInterceptor {
func modify(data: Data, urlResponse: URLResponse) async throws -> (Data, URLResponse) {
if let httpResponse = urlResponse as? HTTPURLResponse {
print("Response Status Code: \(httpResponse.statusCode)")
print("Response Headers: \(httpResponse.allHeaderFields)")
if let responseString = String(data: data, encoding: .utf8) {
print("Response Body: \(responseString)")
}
}
return (data, urlResponse)
}
}
// Add response interceptor to network service
EagleNet.addResponseInterceptor(LoggingInterceptor())
```
### Custom HTTP Methods
```swift
// Using custom HTTP methods like PATCH
struct UpdateStatus: Encodable {
let status: String
}
let patchRequest = DataRequest(
url: "https://api.example.com/users/1",
httpMethod: .custom("PATCH"),
body: UpdateStatus(status: "active")
)
let response: User = try await EagleNet.execute(patchRequest)
```
### Configuration
```swift
// Custom URLSession configuration
let customService = EagleNet.defaultService(
urlSession: URLSession(configuration: .ephemeral),
jsonEncoder: JSONEncoder(),
jsonDecoder: JSONDecoder()
)
EagleNet.configure(networkService: customService)
```
## Author
[Anbalagan D](mailto:anbu94p@gmail.com)
## License
EagleNet is available under the MIT license. See the [LICENSE](LICENSE) file for more info.