An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# ElegantAPI

[![Swift Package Manager compatible](https://img.shields.io/badge/Swift%20Package%20Manager-compatible-brightgreen.svg)](https://github.com/apple/swift-package-manager)
[![Build](https://github.com/DominatorVbN/ElegantAPI/actions/workflows/build.yml/badge.svg?branch=master)](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
``` swift

guard 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.