Skip to content

Instantly share code, notes, and snippets.

View malcommac's full-sized avatar
👋
Nice to meet u

Daniele Margutti malcommac

👋
Nice to meet u
View GitHub Profile
public func fetch() async throws {
// prepare the request and execute it
let task = try await request.urlSessionTask(inClient: client)
let response = try await fetch(request, task: sessionTask: task)
// ask to validator the action to perform on this request
let action = client.validate(response: response, forRequest: request)
switch action {
case .failChain(let error):
return HTTPResponse(error: error) // fail with error
let loggedUser = try await login.fetch(User.self)
let result: HTTPResponse = try await req.fetch()
let myObject = ...
req.body = .json(myObject)
req.body = .multipart(boundary: nil, {
$0.add(string: "value", name: "param_1")
$0.add(fileURL: fileURL, name: "image", mimeType: .gif)
$0.add(string: "some other", name: "param_2")
})
req.headers[.contentType] = .bmp
req.headers = .init([
.contentType: .bmp
"X-Custom-Header": "abc"
])
let req = HTTPRequest {
$0.url = URL(string: "https://.../login")!
$0.method = .post
$0.timeout = 15
$0.redirectMode = redirect
$0.maxRetries = 4
$0.headers = HTTPHeaders([
.init(name: .userAgent, value: myAgent),
.init(name: "X-API-Experimental", value: "true")
])
let todo = try await HTTPRequest("https://jsonplaceholder.typicode.com/todos/1")
.fetch(Todo.self)
@malcommac
malcommac / UserExperiments_10.swift
Created January 22, 2022 22:56
UserExperiments_10.swift
public class FFService {
// ...
private var flagsController: FlagsBrowserController
func showFlagsBrowser() {
flagsController = .create(loaders: [user, ...])
mainController.present(flagsController, animated: true, completion: nil)
}
}
struct UserExperiments: FlagCollectionProtocol {
@Flag(default: false, computedValue: MiscFlags.computedRememberLogin)
var rememberLogin: Bool
private static func computedRememberLogin() -> Bool? {
Language.main.code == "it" && allowsSSON == true
}
...