A nifty little networking library designed for the swift development of APIs and the execution of network requests with minimal setup. Perfect for when you want to get things up and running without the fuss.
fun fact: this library was named by GPT-3.5-turbo
Streaming Support: Pulse now handles streaming data
extension API {
static var weather: API {
var api = API("https://api.weatherapi.com/v1/current.json")
api.authenticationKeyName = "key"
api.authenticationStyle = .parameter // also supports .bearer, .header, and .none
api.authenticationKeyValue = "your weather api key"
return api
}
}
extension Endpoint {
static func getWeather(for location: String) -> Endpoint {
Endpoint(.weather, "?q=\(location)")
}
}
For static endpoints without dynamic parameters, static vars
are your best friend.
Where WeatherResponse
is a Codable
matching the shape of your data:
func getWeather() async throws -> WeatherResponse {
try await Networker.execute(.getWeather(for: "new york"))
}
Or, for a more direct approach:
func getWeather() async throws -> WeatherResponse {
try await Endpoint.getWeather(for: "new york").run()
}
Imagine a live blog or news feed that streams updates:
extension API {
static var liveTextStream: API {
API("https://api.livetextstream.com/v1/updates")
}
}
extension Endpoint {
static func liveTextUpdates(eventId: String) -> Endpoint {
Endpoint(.liveTextStream, "/stream?eventId=\(eventId)", method: .get)
}
}
struct TextUpdate: Codable {
let content: String
}
class TextUpdateStreamParser: StreamParser {
typealias ResultType = TextUpdate
func parse(data: Data) throws -> [TextUpdate] {
// Assuming each data chunk represents a single TextUpdate in JSON format.
// This example simply tries to decode a TextUpdate from the provided data.
// In a real-world scenario, you might need to accumulate data until a complete
// JSON object can be parsed or implement more complex parsing logic.
let decoder = JSONDecoder()
if let update = try? decoder.decode(TextUpdate.self, from: data) {
return [update]
} else {
// Handling cases where data might not be a complete JSON object for decoding
// or implement your logic to accumulate partial data chunks.
return []
}
}
func isStreamComplete(data: Data) -> Bool {
// Implement logic to determine if the stream is complete.
// This could be based on specific markers in the data or external conditions.
// For simplicity, this example does not implement stream completion detection.
return false
}
}
- This is liable to get updated so you don't have to do manual decoding...
Assuming TextUpdate
is your model for each text chunk:
func watchLiveText(eventId: String) async {
let stream = Networker.stream(from: .liveTextUpdates(eventId: eventId), using: TextUpdateStreamParser())
var completeText = ""
for await result in stream {
switch result {
case .success(let update):
completeText += update.content
print("Latest Text: \(completeText)")
case .failure(let error):
print("Streaming Error: \(error)")
}
}
}
Made with ❤️ on Long Island.