Skip to content

Commit

Permalink
progress without error
Browse files Browse the repository at this point in the history
  • Loading branch information
Amy committed Jan 23, 2023
1 parent 6912b93 commit cd00a64
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 5 deletions.
23 changes: 23 additions & 0 deletions Netable/Example/Managers/DataManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// DataManager.swift
// Example
//
// Created by Amy Oulton on 2023-01-20.
// Copyright © 2023 Steamclock Software. All rights reserved.
//

import Foundation

class DataManager {
static let shared = DataManager()

var user: User?

init(user: User? = nil) {
self.user = user
}

func printData() {
print("User: ", user)
}
}
5 changes: 5 additions & 0 deletions Netable/Example/Requests/GetUserRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ struct GetUserRequest: Request {
var jsonKeyDecodingStrategy: JSONDecoder.KeyDecodingStrategy? { .convertFromKebabCase }
var path = "user/profile"
var headers: [String : String]

func postProcess(result: FinalResource) -> FinalResource {
DataManager.shared.user = result
return result
}
}
1 change: 1 addition & 0 deletions Netable/Example/Views/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct HomeView: View {
}.padding()
.onAppear {
viewModel.bindViewModel()
DataManager.shared.printData()
}
.onDisappear {
viewModel.unbindViewModel()
Expand Down
12 changes: 12 additions & 0 deletions Netable/Netable.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
E17FBD532950E3FA00B6533E /* ObservableVM.swift in Sources */ = {isa = PBXBuildFile; fileRef = E17FBD522950E3FA00B6533E /* ObservableVM.swift */; };
E17FBD552950E64C00B6533E /* LoginVM.swift in Sources */ = {isa = PBXBuildFile; fileRef = E17FBD542950E64C00B6533E /* LoginVM.swift */; };
E186202129425EFF009B6E0C /* Netable.framework in Embeded Framworks */ = {isa = PBXBuildFile; fileRef = B8C9288023E9F68000DB2B37 /* Netable.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
E188440A297B3C63009EE74B /* DataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1884409297B3C63009EE74B /* DataManager.swift */; };
E18AAA1029312DF700756455 /* Version.swift in Sources */ = {isa = PBXBuildFile; fileRef = E18AAA0F29312DF700756455 /* Version.swift */; };
E18AAA19293524AA00756455 /* NetworkService.swift in Sources */ = {isa = PBXBuildFile; fileRef = E18AAA18293524AA00756455 /* NetworkService.swift */; };
E18AAA1B2935251400756455 /* GetVersionRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = E18AAA1A2935251400756455 /* GetVersionRequest.swift */; };
Expand Down Expand Up @@ -183,6 +184,7 @@
E17FBD4E2950E25600B6533E /* LoginView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginView.swift; sourceTree = "<group>"; };
E17FBD522950E3FA00B6533E /* ObservableVM.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObservableVM.swift; sourceTree = "<group>"; };
E17FBD542950E64C00B6533E /* LoginVM.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginVM.swift; sourceTree = "<group>"; };
E1884409297B3C63009EE74B /* DataManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataManager.swift; sourceTree = "<group>"; };
E18AAA0F29312DF700756455 /* Version.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Version.swift; sourceTree = "<group>"; };
E18AAA18293524AA00756455 /* NetworkService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkService.swift; sourceTree = "<group>"; };
E18AAA1A2935251400756455 /* GetVersionRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetVersionRequest.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -315,6 +317,7 @@
E1275823292E9C5A0047E5DD /* Example */ = {
isa = PBXGroup;
children = (
E1884408297B3C55009EE74B /* Managers */,
E18AAA2629368F9200756455 /* Helpers */,
E18AAA152935243100756455 /* Services */,
E1275848292EE7950047E5DD /* ViewModels */,
Expand Down Expand Up @@ -409,6 +412,14 @@
path = ViewModels;
sourceTree = "<group>";
};
E1884408297B3C55009EE74B /* Managers */ = {
isa = PBXGroup;
children = (
E1884409297B3C63009EE74B /* DataManager.swift */,
);
path = Managers;
sourceTree = "<group>";
};
E18AAA152935243100756455 /* Services */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -705,6 +716,7 @@
E17FBD4D2950DBB500B6533E /* ErrorView.swift in Sources */,
E17FBD4F2950E25600B6533E /* LoginView.swift in Sources */,
E1BAC49D293AAA190042BF60 /* CreatePostRequest.swift in Sources */,
E188440A297B3C63009EE74B /* DataManager.swift in Sources */,
E18AAA1B2935251400756455 /* GetVersionRequest.swift in Sources */,
E18AAA1029312DF700756455 /* Version.swift in Sources */,
E127583B292EB21C0047E5DD /* Post.swift in Sources */,
Expand Down
14 changes: 9 additions & 5 deletions Netable/Netable/Netable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ public actor Netable {
request.headers.forEach { key, value in
urlRequest.setValue(value, forHTTPHeaderField: key)
}

return try await startRequestTask(request, urlRequest: urlRequest, id: UUID().uuidString)
let task = try await startRequestTask(request, urlRequest: urlRequest, id: UUID().uuidString)
print("goog")
return task
} catch {
let netableError = (error as? NetableError) ?? NetableError.unknownError(error)
await log(.requestCreationFailed(urlString: request.path, error: netableError))
Expand Down Expand Up @@ -137,6 +138,7 @@ public actor Netable {
let task = Task {
do {
let result = try await self.request(request)
print("ASJHDASHJKF")
completion(.success(result))
} catch {
completion(.failure(error.netableError))
Expand Down Expand Up @@ -169,7 +171,8 @@ public actor Netable {
}
}
}

print("hello")
print(resultSubject)
return (task: task, subject: resultSubject.receive(on: RunLoop.main))
}

Expand Down Expand Up @@ -203,10 +206,11 @@ public actor Netable {

let decoded = try await request.decode(data, defaultDecodingStrategy: self.config.jsonDecodingStrategy)
let finalizedResult = try await request.finalize(raw: decoded)
let finalResult = try await request.postProcess(result: finalizedResult)

await self.log(.requestSuccess(request: requestInfo, taskTime: CACurrentMediaTime() - startTimestamp, statusCode: response.statusCode, responseData: data, finalizedResult: finalizedResult))
await self.log(.requestSuccess(request: requestInfo, taskTime: CACurrentMediaTime() - startTimestamp, statusCode: response.statusCode, responseData: data, finalizedResult: finalResult))

return finalizedResult
return finalResult
} catch {
let netableError = error.netableError
let time = CACurrentMediaTime() - startTimestamp
Expand Down
13 changes: 13 additions & 0 deletions Netable/Netable/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public protocol Request: Sendable {

/// Optional: The method to convert your RawResource returned by the server to FinalResource.
func finalize(raw: RawResource) async throws -> FinalResource

/// Optional: The method to perform a process after getting the result but before the request returns it.
func postProcess(result: FinalResource) async throws -> FinalResource
}

public extension Request {
Expand Down Expand Up @@ -105,6 +108,15 @@ public extension Request {
var jsonKeyEncodingStrategy: JSONEncoder.KeyEncodingStrategy? {
return nil
}

/// By default, `postProcess` just returns the FinalResource.
func postProcess(result: FinalResource) -> FinalResource {
print("HELLO")
print(result)
return result
}


}

public extension Request where Parameters == Empty {
Expand All @@ -119,6 +131,7 @@ public extension Request where FinalResource == RawResource {
func finalize(raw: RawResource) async throws -> FinalResource {
return raw
}

}

public extension Request where
Expand Down

0 comments on commit cd00a64

Please sign in to comment.