|
1 | | -# RaAPIWrapper |
| 1 | +# RaAPIWrapper |
| 2 | + |
| 3 | +<p align="center"> |
| 4 | +<a><img src="https://img.shields.io/badge/language-swift-ffac45.svg"></a> |
| 5 | +<a href="https://github.com/rakuyoMo/RaAPIWrapper/releases"><img src="https://img.shields.io/cocoapods/v/RaAPIWrapper.svg"></a> |
| 6 | +<a href="https://github.com/rakuyoMo/RaAPIWrapper/blob/main/LICENSE"><img src="https://img.shields.io/cocoapods/l/RaAPIWrapper.svg?style=flat"></a> |
| 7 | +</p> |
| 8 | + |
| 9 | +> [中文](https://github.com/rakuyoMo/RaAPIWrapper/blob/main/README_CN.md) |
| 10 | +
|
| 11 | +`RaAPIWrapper` uses `@propertyWrapper` to achieve a similar effect to that of defining network requests in the Android `Retrofit` library. |
| 12 | + |
| 13 | +When you have a large number of network request apis in the same file `RaAPIWrapper` can help you define each request in a more aggregated form, so you don't have to jump back and forth within the file. |
| 14 | + |
| 15 | +## Say it before |
| 16 | + |
| 17 | +**Special Note! **: `RaAPIWrapper` is just a syntactic sugar for **defining** web requests. You need to use `Alamofire`, `Moya`, other third-party web framework or call `URLSession` directly to initiate web requests on this basis. |
| 18 | + |
| 19 | +The good thing is that you can easily integrate `RaAPIWrapper` into your existing project with few or no code changes, and `RaAPIWrapper` can coexist very well with the existing web framework in your project. |
| 20 | + |
| 21 | +## Prerequisites |
| 22 | + |
| 23 | +- **iOS 11**、**macOS 10.13**、**watchOS 4.0**、**tvOS 11** or later. |
| 24 | +- **Xcode 14** or later required. |
| 25 | +- **Swift 5.7** or later required. |
| 26 | + |
| 27 | +## Example |
| 28 | + |
| 29 | +```swift |
| 30 | +@GET(/api/v1/no_param) |
| 31 | +static var noParamAPI: APIParameterBuilder<()>? = nil |
| 32 | + |
| 33 | +@POST(/api/v1/tuple_param) |
| 34 | +static var tupleParamAPI: APIParameterBuilder<(id: Int, name: String?)>? = { |
| 35 | + // Eliminate the warning by explicitly converting to `[String: Any?]`. |
| 36 | + // Also ensure that `nil` parameters can be filtered. |
| 37 | + ["id": $0.id, "name": $0.name] as [String: Any?] |
| 38 | +} |
| 39 | + |
| 40 | +@POST("/post") |
| 41 | +static var postWithModel: APIParameterBuilder<Arg>? = { |
| 42 | + // You can have your model follow the `APIParameterConvertible` protocol, |
| 43 | + // or use `AnyAPIHashableParameter` to wrap your model in an outer layer. |
| 44 | + AnyAPIHashableParameter($0) |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +## Install |
| 49 | + |
| 50 | +### CocoaPods |
| 51 | + |
| 52 | +```ruby |
| 53 | +pod 'RaAPIWrapper' |
| 54 | +``` |
| 55 | + |
| 56 | +If your project relies on `Alamofire`, then you may also consider relying on `RaAPIWrapper/AF`. This module provides a wrapper for `ParameterEncoding`. |
| 57 | + |
| 58 | +### Swift Package Manager |
| 59 | + |
| 60 | +- File > Swift Packages > Add Package Dependency |
| 61 | +- Add https://github.com/rakuyoMo/RaAPIWrapper.git |
| 62 | +- SSelect "Up to Next Major" and fill in the corresponding version number |
| 63 | + |
| 64 | +Or add the following to your `Package.swift` file: |
| 65 | + |
| 66 | +```swift |
| 67 | +dependencies: [ |
| 68 | + .package( |
| 69 | + url: "https://github.com/rakuyoMo/RaAPIWrapper.git", |
| 70 | + .upToNextMajor(from: "1.0.0") |
| 71 | + ) |
| 72 | +] |
| 73 | +``` |
| 74 | + |
| 75 | +## Usage |
| 76 | + |
| 77 | +Please refer to the example in `Demo.playground`. |
| 78 | + |
| 79 | +> Since playground depends on `RaAPIWrapper` in the form of Swift Package Manager, please open the project via `Package.swift` first, then select `Demo.playground` from the left navigation bar and run the content. |
| 80 | +
|
| 81 | +## License |
| 82 | + |
| 83 | +`RaAPIWrapper` is available under the **MIT** license. For more information, see [LICENSE](LICENSE). |
0 commit comments