Simple declarative HTTP API framework
First step is to build a request. You make requests by providing extension on top of Request
type:
extension Request {
static let func login(_ body: UserBody) -> Self where Output == UserResponse {
.post("login", body: body)
}
}
And... voila! We defined a login(_:)
request which will request login endpoint by sending a UserBody
and waiting for a UserResponse
. Now it's time to use it.
You can also use an enum to define your Request path:
enum MyAppEndpoint: String, Path {
case login
}
extension Request {
static let func login(_ body: UserBody) -> Self where Output == UserResponse {
.post(MyAppEndpoint.login, body: body)
}
}
To send a request use a Session
instance. Session
is somewhat similar to URLSession
but providing additional functionalities.
let session = Session(baseURL: URL(string: "https://github.com")!, encoder: JSONEncoder(), decoder: JSONDecoder())
session.publisher(for: .login(UserBody(username: "pjechris", password: "MyPassword")))
You can now use the returned publisher however you want. Its result is similar to what you have received with URLSession.shared.dataTaskPublisher(for: ...).decode(type: UserResponse.self, decoder: JSONDecoder())
.
A few words about Session:
baseURL
will be prepended to all call endpointsURLSession
instance if ever neededYou will build your request by sending your body
to construct it:
struct UserBody: Encodable {}
extension Request {
static func login(_ body: UserBody) -> Self where Output == LoginResponse {
.post("login", body: .encodable(body))
}
}
We defined a login(_:)
request which will request login endpoint by sending a UserBody
and waiting for a LoginResponse
You we build 2 requests:
URL
Data
extension Request {
static func send(audio: URL) throws -> Self where Output == SendAudioResponse {
var multipart = MultipartFormData()
try multipart.add(url: audio, name: "define_your_name")
return .post("sendAudio", body: .multipart(multipart))
}
static func send(audio: Data) throws -> Self where Output == SendAudioResponse {
var multipart = MultipartFormData()
try multipart.add(data: data, name: "your_name", fileName: "your_fileName", mimeType: "right_mimeType")
return .post("sendAudio", body: .multipart(multipart))
}
}
We defined the 2 send(audio:)
requests which will request sendAudio
endpoint by sending an URL
or a Data
and waiting for a SendAudioResponse
We can add multiple Data
/URL
to the multipart
extension Request {
static func send(audio: URL, image: Data) throws -> Self where Output == SendAudioImageResponse {
var multipart = MultipartFormData()
try multipart.add(url: audio, name: "define_your_name")
try multipart.add(data: image, name: "your_name", fileName: "your_fileName", mimeType: "right_mimeType")
return .post("sendAudioImage", body: .multipart(multipart))
}
}
Protocol Interceptor
enable powerful request interceptions. This include authentication, logging, request retrying, etc...
RequestInterceptor
RequestInterceptor
allow to adapt a or retry a request whenever it failed:
adaptRequest
method is called before making a request and allow you to transform it adding headers, changing path, ...rescueRequestError
is called whenever the request fail. You'll have a chance to retry the request. This can be used to re-authenticate the user for instanceResponseInterceptor
ResponseInterceptor
is dedicated to intercept and server responses:
adaptResponse
change the server outputreceivedResponse
notify about the server final response (a valid output or error)link |
Stars: 2 |
Last commit: 6 days ago |
Full Changelog: https://github.com/pjechris/SimpleHTTP/compare/0.2.0...0.3.0
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics