File tree Expand file tree Collapse file tree
Demo.playground/Pages/Basic.xcplaygroundpage Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -42,27 +42,19 @@ extension BasicAPI {
4242 /// The full api address is: [](https://postman-echo.com/post) .
4343 /// The api is entered as a **tuple** type and requires two parameters, where the second parameter can be `nil`.
4444 @POST ( " /post " )
45- static var postWithTuple : APIParameterBuilder < ( foo1: String , foo2: Int ? ) > ? = {
45+ static var postWithTuple : APIParameterBuilder < ( foo1: String , foo2: Int ? ) > ? = . init {
4646 [
4747 " foo1 " : $0. foo1,
48- " foo2 " : $0. foo2,
48+ " foo2 " : $0. foo2
4949 ]
50-
51- // Eliminate the warning by explicitly converting to `[String: Any?]`.
52- // Also ensure that `nil` parameters can be filtered.
53- as [ String : Any ? ]
5450 }
5551
5652 /// This is an api for requests using the **POST** method.
5753 ///
5854 /// The full api address is: [](https://postman-echo.com/post) .
5955 /// This api is referenced with the `Arg` type.
6056 @POST ( " /post " )
61- static var postWithModel : APIParameterBuilder < Arg > ? = {
62- // You can have your model follow the `APIParameterConvertible` protocol.
63- // or use `AnyAPIHashableParameter` to wrap your model in an outer layer.
64- AnyAPIHashableParameter ( $0)
65- }
57+ static var postWithModel : APIParameterBuilder < Arg > ? = . init { $0 }
6658}
6759
6860do {
Original file line number Diff line number Diff line change @@ -31,17 +31,16 @@ The good thing is that you can easily integrate `RaAPIWrapper` into your existin
3131static var noParamAPI: APIParameterBuilder<()>? = nil
3232
3333@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 ? ]
34+ static var tupleParamAPI: APIParameterBuilder<(id: Int , name: String ? )>? = .init {
35+ // `Dictionary` and `Array` can be used directly as parameters.
36+ [" id" : $0 .id , " name" : $0 .name ]
3837}
3938
4039@POST (" /post" )
41- static var postWithModel: APIParameterBuilder<Arg>? = {
42- // You can have your model follow the `APIParameterConvertible` protocol,
43- // or use `AnyAPIParameter` to wrap your model in an outer layer .
44- AnyAPIParameter ( $0 )
40+ static var postWithModel: APIParameterBuilder<Arg>? = . init {
41+ // When the parameter `Arg` complies with the `APIParameter` (`Encodable & Hashable`) protocol,
42+ // it can be used directly as a parameter .
43+ $0
4544}
4645```
4746
Original file line number Diff line number Diff line change 2929static var noParamAPI: APIParameterBuilder<()>? = nil
3030
3131@POST (" /api/v1/tuple_param" )
32- static var tupleParamAPI: APIParameterBuilder<(id: Int , name: String ? )>? = {
33- // 通过显式转换为 `[String: Any?]` 来消除警告,同时确保为 `nil` 的参数能够被过滤。
34- [" id" : $0 .id , " name" : $0 .name ] as [ String : Any ? ]
32+ static var tupleParamAPI: APIParameterBuilder<(id: Int , name: String ? )>? = . init {
33+ // 字典和数组可直接作为参数使用
34+ [" id" : $0 .id , " name" : $0 .name ]
3535}
3636
3737@POST (" /post" )
38- static var postWithModel: APIParameterBuilder<Arg>? = {
39- // 您可以让您的模型遵循 `APIParameterConvertible` 协议,或者使用 `AnyAPIParameter` 在外面包裹一层 。
40- AnyAPIParameter ( $0 )
38+ static var postWithModel: APIParameterBuilder<Arg>? = . init {
39+ // 当参数 `Arg` 遵守 `APIParameter`(`Encodable & Hashable`) 协议时,可直接作为参数使用 。
40+ $0
4141}
4242```
4343
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ Pod::Spec.new do |s|
55
66 s . name = 'RaAPIWrapper'
77
8- s . version = '1.0.2 '
8+ s . version = '1.1.0 '
99
1010 s . summary = 'Makes it easier to define a network request.'
1111
Original file line number Diff line number Diff line change 11//
2- // AnyAPIParameter .swift
2+ // APIParameter .swift
33// RaAPIWrapper
44//
55// Created by Rakuyo on 2023/01/13.
88
99import Foundation
1010
11+ /// Used to constrain what types can be used as api parameters.
12+ public typealias APIParameter = AnyAPIHashableParameter . Input
13+
1114/// Represents an arbitrary api parameter.
1215public typealias AnyAPIParameter = AnyAPIHashableParameter
Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -31,20 +31,35 @@ public struct APIRequestInfo {
3131 /// You can use this property to store some custom data.
3232 public let userInfo : APIRequestUserInfo
3333
34- public init (
34+ public init < ParamType > (
3535 path: String ,
3636 specialBaseURL: URL ? = nil ,
3737 httpMethod: APIHTTPMethod ,
3838 header: APIHeaders ? = nil ,
39- parameters: AnyAPIParameter ? = nil ,
39+ parameterBuild: APIParameterBuilder < ParamType > ? = nil ,
40+ parameterInput: ParamType ? = nil ,
4041 userInfo: APIRequestUserInfo = [ : ]
4142 ) {
4243 self . path = path
4344 self . specialBaseURL = specialBaseURL
4445 self . httpMethod = httpMethod
4546 self . header = header
46- self . parameters = parameters
4747 self . userInfo = userInfo
48+
49+ self . parameters = {
50+ guard
51+ let parameter = parameterInput,
52+ let build = parameterBuild
53+ else {
54+ return nil
55+ }
56+
57+ let result = build ( parameter)
58+ if let value = result as? AnyAPIParameter {
59+ return value
60+ }
61+ return . init( result)
62+ } ( )
4863 }
4964}
5065
Original file line number Diff line number Diff line change 88
99import Foundation
1010
11- /// Parameter constructor for the api. Supports passing one parameter.
12- public typealias APIParameterBuilder < ParamType> = ( ParamType ) -> APIParameterConvertible
13-
1411/// Used to encapsulate the `APIHTTPMethod` object provided to the `API`.
1512public protocol APIHTTPMethodWrapper {
1613 static var httpMethod : APIHTTPMethod { get }
@@ -75,7 +72,8 @@ public extension API {
7572 specialBaseURL: specialBaseURL,
7673 httpMethod: Self . httpMethod. httpMethod,
7774 header: headerBuilder ? ( parameter) ,
78- parameters: wrappedValue ? ( parameter) . toParameters,
75+ parameterBuild: wrappedValue,
76+ parameterInput: parameter,
7977 userInfo: userInfo
8078 )
8179 }
Original file line number Diff line number Diff line change 1+ //
2+ // APIParameterBuilder.swift
3+ // RaAPIWrapper
4+ //
5+ // Created by Rakuyo on 2023/01/13.
6+ // Copyright © 2022 Rakuyo. All rights reserved.
7+ //
8+
9+ import Foundation
10+
11+ public struct APIParameterBuilder < Input> {
12+ public typealias Output = any APIParameter
13+
14+ public typealias ParameterBuilder = ( Input ) -> Output
15+
16+ private let parameterBuild : ParameterBuilder
17+
18+ public init ( _ build: @escaping ParameterBuilder ) {
19+ self . parameterBuild = build
20+ }
21+ }
22+
23+ public extension APIParameterBuilder {
24+ init ( _ build: @escaping ( Input ) -> [ Any ? ] ) {
25+ self . parameterBuild = { build ( $0) . toParameters }
26+ }
27+
28+ init ( _ build: @escaping ( Input ) -> [ String : Any ? ] ) {
29+ self . parameterBuild = { build ( $0) . toParameters }
30+ }
31+ }
32+
33+ extension APIParameterBuilder {
34+ func callAsFunction( _ input: Input ) -> Output {
35+ return parameterBuild ( input)
36+ }
37+ }
You can’t perform that action at this time.
0 commit comments