https://github.com/dominatorvbn/elegantapi
A api management API inspired from moya but for URLSession
https://github.com/dominatorvbn/elegantapi
hacktoberfest hacktoberfest-2022 hacktoberfest-accepted
Last synced: 6 months ago
JSON representation
A api management API inspired from moya but for URLSession
- Host: GitHub
- URL: https://github.com/dominatorvbn/elegantapi
- Owner: DominatorVbN
- License: mit
- Created: 2020-05-02T16:19:19.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-10-28T13:41:07.000Z (almost 2 years ago)
- Last Synced: 2025-03-24T14:11:32.627Z (7 months ago)
- Topics: hacktoberfest, hacktoberfest-2022, hacktoberfest-accepted
- Language: Swift
- Homepage:
- Size: 62.6 MB
- Stars: 23
- Watchers: 3
- Forks: 5
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ElegantAPI
[](https://github.com/apple/swift-package-manager)
[](https://github.com/DominatorVbN/ElegantAPI/actions/workflows/build.yml)An URSessionDataTaskPublisher abstraction layer inspired by [Moya](https://github.com/Moya/Moya).
## Documentation
https://amitsamant.in/ElegantAPI/documentation/elegantapi/
## Installation
Elegant api is available through Swift Package Manager
add this url to `File -> Swift Packages -> Add new Dependecy`
```
https://github.com/DominatorVbN/ElegantAPI.git
```or
add the following as a dependency to your Package.swift:
```
.package(url: "https://github.com/DominatorVbN/ElegantAPI.git", .upToNextMajor(from: "0.0.9"))
```## Usage
import ElegentAPI
``` swift
import ElegantAPI
```You have to declare an enum representing your api endpoints
``` swift
enum APIEndpoint{
case login(emailId: String, password: String)
case getProfile
case updateProfile(user: User)
case changeProfileImage(profileImage: UIImage)
}
```Implement the enum with protocol API which contains all the requirement
``` swift
extension APIEndpoint: API{var baseURL: URL {
// You can provide diffrent base urls for diffrent endpoint enum cases
URL(string: "")!
}
// End point path
var path: String {
switch self {
case login:
return "login"
case getProfile:
return "get_profile"
case updateProfile:
return "update_profile"
case changeProfileImage:
return change_profile_image
}
}
// HTTP method of endpoint
var method: ElegantAPI.Method {
switch self {
case .login:
return .post
case .getProfile:
return .get
case .updateProfile:
return .patch
case changeProfileImage:
return .put
}
}
var sampleData: Data {
// for mocking requests
Data()
}
var task: Task {
switch self {
case .login(let email,let password):
return .requestParameters(parameters: [
"email": email,
"password": password
], encoding: .JSONEncoded)
case .getProfile:
return .requestPlain
case .updateProfile(let user):
return .requestJSONEncodable(user)
case .changeProfileImage(let image):
var data: [MultipartFormData] = [
MultipartFormData(
data: image.jpegData(compressionQuality: 0.4) ?? Data(),
name: "image",
fileName: "\(UUID().uuidString).png",
mimeType: "image/jpeg"
)
]
return .uploadMultipart(data)
}
}
// Headers for request
var headers: [String : String]? {
switch self {
case .login:
return ["Content-Type": "application/json"]
case .getProfile, .updateProfile, .changeProfileImage:
return ["Authorization": "Bearer \(Persistance.shared.accessKey)"]
}
}
}```
And that's you can define all api requests
Now to perform this request## Using Combine
``` swiftguard let request = APIEndpoint.login.getURLRequest() else {return}
// NetworkLogger is an micro library included inside Elegant api for logging network response and requests
NetworkLogger.log(request: request)let publisher = URLSession.shared.dataTaskPublisher(for: req)
publisher.map { output in
NetworkLogger.log(data: output.data, response: output.response as? HTTPURLResponse, error: nil)
return output
}.sink(
receiveCompletion: { },
receiveValue: { output in
print(output.data)
}
)```
> You can also you the URLRequest for plain dataTask too.