Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made failedRequest a static method #50

Merged
merged 1 commit into from May 4, 2016

Conversation

vdka
Copy link
Contributor

@vdka vdka commented May 4, 2016

No description provided.

@pcantrell
Copy link
Member

Thanks for this. Can you give a little background on the problem it’s solving?

I’d thought is should be an instance method in order to leave the door open to future API support for asking which resource a response came from, which this change rules out. However, the use cases for that are fuzzy and might be achieved through other means as well. So let me know if making this static solves a concrete problem for you.

@vdka
Copy link
Contributor Author

vdka commented May 4, 2016

Using it when I cannot construct a correct resource endpoint. eg:

  public static func updateBusiness(business: Business) -> Request {
    guard let id = business.serverId else {
      return Resource.failedRequest(Siesta.Error(userMessage: "Object does not exist on server", cause: Error.objectNotOnServer))
    }
    return service.resource(.Businesses).child(id).request(.PUT, json: business, options: [.noSkipNull])
  }
extension Resource {
  /// Perform request with a JSON object.
  public func request(
    method: RequestMethod,
    json: JSONEncodable,
    options: [JSON.Serializer.Option] = [],
    contentType: String = "application/json",
    requestMutation: NSMutableURLRequest -> () = { _ in }
    ) -> Request {
    do {
      let rawBody = try json.serialized(options: options).dataUsingEncoding(NSUTF8StringEncoding) ?? NSData()
      return request(method, data: rawBody, contentType: contentType)
    } catch {
      return Resource.failedRequest(Error(userMessage: "Failed to encode JSON", cause: error))
    }
  }
}

I guess in the second example I can just call the member function

@pcantrell
Copy link
Member

Right — in the first example, the very reason the request fails is that there is no resource on which you could call a failedRequest() instance method. That seems decisive.

@pcantrell pcantrell merged commit bca0a13 into bustoutsolutions:master May 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants