Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions EndpointsExample/DependencyInjection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import Injection
enum DependencyInjection {
static func register() {
let postmanEchoUrl = URL(string: "https://postman-echo.com")!
let postmanClient = AnyClient(baseURL: postmanEchoUrl)
let postmanClient = DefaultClient(url: postmanEchoUrl)

let httpBinUrl = URL(string: "https://httpbin.org/")!
let httpBinClient = AnyClient(baseURL: httpBinUrl)
let httpBinClient = DefaultClient(url: httpBinUrl)

let manipulatedHttpBinSession = ManipulatedHTTPBinClient()
let customizedHttpBinSession = CustomizedHTTPBinClient()
Comment thread
kaulex99 marked this conversation as resolved.
Outdated

let api = ExampleAPI(
postmanClient: postmanClient,
httpBinClient: httpBinClient,
manipulatedHttpBinClient: manipulatedHttpBinSession
customizedHttpBinSession: customizedHttpBinSession,
)

DependencyInjector.register(api, as: API.self)
Expand Down
4 changes: 2 additions & 2 deletions EndpointsExample/ExampleAsyncReactor/ExampleReactor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Injection

class ExampleReactor: AsyncReactor {
Comment thread
kaulex99 marked this conversation as resolved.
enum Action {
case executeRequests
case executeRequest
}

struct State {
Expand All @@ -22,7 +22,7 @@ class ExampleReactor: AsyncReactor {

func action(_ action: Action) async {
switch action {
case .executeRequests:
case .executeRequest:
await executeRequest()
}
}
Expand Down
8 changes: 5 additions & 3 deletions EndpointsExample/ExampleAsyncReactor/ExampleReactorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import AsyncReactor
import SwiftUI

struct ExampleReactorView: View {
@EnvironmentObject var reactor: ExampleReactor

@EnvironmentObject
private var reactor: ExampleReactor

var body: some View {
VStack {
Expand All @@ -15,8 +17,8 @@ struct ExampleReactorView: View {
.font(.headline)
}
}
.onAppear {
reactor.send(.executeRequests)
.task {
await reactor.action(.executeRequest)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions EndpointsExample/ExampleMVVM/ExampleViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class ExampleViewModel: ObservableObject {
}

Task {
let (_, response) = try await api.loadManipulatedData(deliveredStatusCode: 220)
guard response.statusCode == 200 else { return }
let (_, response) = try await api.loadCustomizedData(deliveredStatusCode: 220)
guard response.statusCode == 220 else { return }
print("Success")
}
}
Expand Down
22 changes: 11 additions & 11 deletions EndpointsExample/Networking/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ import Foundation

protocol API: Actor {
func loadExampleData() async throws -> (ExampleModel, HTTPURLResponse)
func loadManipulatedData(deliveredStatusCode: Int) async throws -> (String, HTTPURLResponse)
func loadCustomizedData(deliveredStatusCode: Int) async throws -> (String, HTTPURLResponse)
}

actor ExampleAPI: API {
private let postmanSession: Session<AnyClient>
private let httpBinSession: Session<AnyClient>
private let manipulatedHttpBinSession: Session<ManipulatedHTTPBinClient>
private let postmanSession: Session<DefaultClient>
private let httpBinSession: Session<DefaultClient>
private let customizedHttpBinSession: Session<CustomizedHTTPBinClient>

init(
postmanClient: AnyClient,
httpBinClient: AnyClient,
manipulatedHttpBinClient: ManipulatedHTTPBinClient,
postmanClient: DefaultClient,
httpBinClient: DefaultClient,
customizedHttpBinSession: CustomizedHTTPBinClient,
Comment thread
kaulex99 marked this conversation as resolved.
Outdated
) {
self.postmanSession = Session(with: postmanClient)
self.httpBinSession = Session(with: httpBinClient)
self.manipulatedHttpBinSession = Session(with: manipulatedHttpBinClient)
self.customizedHttpBinSession = Session(with: customizedHttpBinSession)
}

func loadExampleData() async throws -> (ExampleModel, HTTPURLResponse) {
Expand All @@ -29,9 +29,9 @@ actor ExampleAPI: API {
)
}

func loadManipulatedData(deliveredStatusCode: Int) async throws -> (String, HTTPURLResponse) {
try await manipulatedHttpBinSession.dataTask(
for: ManipulatedHTTPBinClient.GetStatusCode(deliveredStatusCode: deliveredStatusCode)
func loadCustomizedData(deliveredStatusCode: Int) async throws -> (String, HTTPURLResponse) {
try await customizedHttpBinSession.dataTask(
for: CustomizedHTTPBinClient.GetStatusCode(deliveredStatusCode: deliveredStatusCode)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@
import Endpoints
import Foundation

struct ManipulatedHTTPBinClient: Client {
struct CustomizedHTTPBinClient: Client {
private var client: Client

init() {
let url = URL(string: "https://httpbin.org/")!
self.client = AnyClient(baseURL: url)
self.client = DefaultClient(url: url)
}

func encode(call: some Endpoints.Call) async throws -> URLRequest {
// Custom manipulation i.e. OAuth implementation
print("- MANIPULATED encode -")
print("- CUSTOMIZED encode -")
return try await client.encode(call: call)
}

func parse<C>(response: HTTPURLResponse?, data: Data?, for call: C) async throws -> C.Parser.OutputType
where C: Call {
// Custom manipulation i.e. react on error responses or invalid tokens
print("- MANIPULATED parse -")
print("- CUSTOMIZED parse -")
return try await client.parse(response: response, data: data, for: call)
}

func validate(response: HTTPURLResponse?, data: Data?) async throws {
// Custom validation if needed
print("- MANIPULATED validate -")
print("- CUSTOMIZED validate -")
return try await client.validate(response: response, data: data)
}

Expand Down