Skip to content

Commit

Permalink
[92] progress without error (#117)
Browse files Browse the repository at this point in the history
* post processing working

* remove print statement

* updated data manager print function

* updated functionality to not include return

* cleared 'any' warning

* readded return to postprocess

* updated readme

---------

Co-authored-by: Amy <[email protected]>
  • Loading branch information
amyoulton and Amy authored Feb 13, 2023
1 parent efdd8df commit 920c0e9
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 10 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("Data Manager User: ", user ?? "No User Found")
}
}
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 @@ -56,6 +56,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 @@ -161,6 +162,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 @@ -283,6 +285,7 @@
E1275823292E9C5A0047E5DD /* Example */ = {
isa = PBXGroup;
children = (
E1884408297B3C55009EE74B /* Managers */,
E18AAA2629368F9200756455 /* Helpers */,
E18AAA152935243100756455 /* Services */,
E1275848292EE7950047E5DD /* ViewModels */,
Expand Down Expand Up @@ -377,6 +380,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 @@ -633,6 +644,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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1410"
LastUpgradeVersion = "1420"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
4 changes: 2 additions & 2 deletions Netable/Netable/Netable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ 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 result = try await startRequestTask(request, urlRequest: urlRequest, id: UUID().uuidString)
return try await request.postProcess(result: result)
} catch {
let netableError = (error as? NetableError) ?? NetableError.unknownError(error)
await log(.requestCreationFailed(urlString: request.path, error: netableError))
Expand Down
8 changes: 8 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,11 @@ public extension Request {
var jsonKeyEncodingStrategy: JSONEncoder.KeyEncodingStrategy? {
return nil
}

/// Sets the default `postProcess` to just return the FinalResource.
func postProcess(result: FinalResource) -> FinalResource {
return result
}
}

public extension Request where Parameters == Empty {
Expand Down
33 changes: 26 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ netable.request(GetCatImages()) { result in

#### Canceling A Request

You're able to easily cancel a request using `.cancel()`, which you can see in action in the [AuthNetworkService](https://github.com/steamclock/netable/blob/master/Netable/Example/Services/AuthNetworkService.swift#L82) within the Example Project.
You're able to easily cancel a request using `.cancel()`, which you can see in action in the [AuthNetworkService](https://github.com/steamclock/netable/blob/main/Netable/Example/Services/AuthNetworkService.swift#L82) within the Example Project.

To cancel a task, we first need to ensure we retain a reference to the task, like so:

Expand Down Expand Up @@ -348,13 +348,32 @@ Note: to access the LossyArray's elements, you have to access `.element` within,
}
```

#### Perform an optional process before returning the result using postProcess

This is helpful for managing data in places like caches or data managers. You can see this more indepth in our [UserRequest](https://github.com/steamclock/netable/blob/main/Netable/Example/Requests/GetUserRequest.swift)

To use `postProcess` inside of the request, add the code you want to run before the return statement:

```swift

struct GetUserRequest: Request {
// ...

func postProcess(result: FinalResource) -> FinalResource {
DataManager.shared.user = result
return result
}
}

```

### Handling Errors

In addition to handling errors locally that are thrown, or returned through `Result` objects, we provide two ways to handle errors globally. These can be useful for doing things like presenting errors in the UI for common error cases across multiple requests, or catching things like failed authentication requests to clear a stored user.

#### Using `requestFailureDelegate`

See [GlobalRequestFailureDelegate](https://github.com/steamclock/netable/blob/master/Netable/Example/Services/ErrorService.swift) in the Example project for a more detailed example.
See [GlobalRequestFailureDelegate](https://github.com/steamclock/netable/blob/main/Netable/Example/Services/ErrorService.swift) in the Example project for a more detailed example.

```swift
extension GlobalRequestFailureDelegateExample: RequestFailureDelegate {
Expand All @@ -369,7 +388,7 @@ extension GlobalRequestFailureDelegateExample: RequestFailureDelegate {

If you prefer `Combine`, you can subscribe to this publisher to receive `NetableErrors` from elsewhere in your app.

See [GlobalRequestFailurePublisher](https://github.com/steamclock/netable/blob/master/Netable/Example/Services/AuthNetworkService.swift#L34) in the Example project for a more detailed example.
See [GlobalRequestFailurePublisher](https://github.com/steamclock/netable/blob/main/Netable/Example/Services/AuthNetworkService.swift#L34) in the Example project for a more detailed example.

```swift
netable.requestFailurePublisher.sink { error in
Expand Down Expand Up @@ -400,11 +419,11 @@ struct GetCatRequest: Request {
typealias RawResource: CoolCat
typealias FallbackResource: Cat

\\
// ...
}
```

See [FallbackDecoderViewController](https://github.com/steamclock/netable/blob/master/Netable/Example/Requests/GetVersionRequest.swift) in the Example project for a more detailed example.
See [FallbackDecoderViewController](https://github.com/steamclock/netable/blob/main/Netable/Example/Requests/GetVersionRequest.swift) in the Example project for a more detailed example.

### GraphQL Support

Expand All @@ -420,7 +439,7 @@ struct GetAllPostsQuery: GraphQLRequest {
}
```

See [UpdatePostsMutation](https://github.com/steamclock/netable/blob/master/Netable/Example/Requests/GraphQL/UpdatePostsMutation.swift) in the Example Project for a more detailed example. Note that by default it's important that your `.graphql` file's name matches _exactly_ with your request.
See [UpdatePostsMutation](https://github.com/steamclock/netable/blob/main/Netable/Example/Requests/GraphQL/UpdatePostsMutation.swift) in the Example Project for a more detailed example. Note that by default it's important that your `.graphql` file's name matches _exactly_ with your request.

We recommend using a tool like [Postman](https://www.postman.com/) to document and test your queries. Also note that currently, shared fragments are not supported.

Expand Down Expand Up @@ -450,4 +469,4 @@ Since Netable 2.0 leverages `async`/`await` under the hood, if you want to build

## License

Netable is available under the MIT license. See the [License.md](https://github.com/steamclock/netable/blob/master/LICENSE.md) for more info.
Netable is available under the MIT license. See the [License.md](https://github.com/steamclock/netable/blob/main/LICENSE.md) for more info.

0 comments on commit 920c0e9

Please sign in to comment.