TABResourceLoader icon indicating copy to clipboard operation
TABResourceLoader copied to clipboard

Should network requests which aren't GET require data to be returned

Open jonhocking opened this issue 7 years ago • 8 comments

It's a pretty common pattern to just use status codes for some of the HTTP methods. For example, I want to send a push notification token to my server. I don't care about the response other than if it's a 200 or not as it's quite fire-and-forget.

My understanding of the current Resource Loader implementation is that we need to return and parse some data even on POST etc.

What do people think?

jonhocking avatar Feb 28 '17 11:02 jonhocking

We could add a protocol extension for the DataResourceType when the Model is Void to return .success() by default to eliminate this step for the developer as this is common on most non GET requests

Neil3079 avatar Feb 28 '17 12:02 Neil3079

If you don't care about the data you can make the Model on the resource be Void and implement the DataResourceType to always return .success().

lucianomarisi avatar Feb 28 '17 12:02 lucianomarisi

@Neil3079 I don't think we should add a default implementation since it's nice to have the compiler error when the method is not implemented. This case is only relevant when the Model is Void.

lucianomarisi avatar Feb 28 '17 12:02 lucianomarisi

@lucianomarisi Well we would still have the compiler error as the extension would only be on the case when the Model is Void?

Neil3079 avatar Feb 28 '17 12:02 Neil3079

ahh yes, makes sense, we could add something like this:

extension DataResourceType where Model == Void {
  func model(from data: Data) throws -> Model {
    return Void()
  }
}

Not sure if the syntax is correct.

lucianomarisi avatar Feb 28 '17 12:02 lucianomarisi

I seem to remember in Swift Void is just the empty Tuple type ()

Neil3079 avatar Feb 28 '17 12:02 Neil3079

You're correct, trying a bit of code in Xcode, this seems to work:

public extension DataResourceType where Model == Void {
  func model(from data: Data) throws {}
}

This would be the defined resource:

struct VoidResource: DataResourceType {
  typealias Model = Void
}

lucianomarisi avatar Feb 28 '17 12:02 lucianomarisi