You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
letuserComment=["justin":"wow this is cool"]letendpoint=POST<[String:AnyObject]>("https://api.bridge.com/comments")
endpoint.execute(params: userComment, success:{(commentResponse:[String:AnyObject])->()in
// Handle success
}, failure:{(error, data, request, response)in
// Handle failure
})
Interceptors
The power of Bridge is that it lets you create custom "Interceptors" to intercept process your requests before they are sent off to the internets, or to intercept and process your responses before they are returned to the caller's success block.
Attach custom headers based on the endpoint, write retry handling, write authentication handlers, use method tunneling. Interceptors allow Bridge to be extremely extensible to your project needs.
/**
* Conform to the `ResponseInterceptor` protocol for any Bridge that
* needs to work with or alter a request before it's sent over the wire
*/
publicprotocolResponseInterceptor{func process<ReturnType>(endpoint:Endpoint<ReturnType>, inout mutableRequest:NSMutableURLRequest)}
/**
* Conform to the `ResponseInterceptor` protocol to work with data after
* the request is returned with a response.
*/
publicprotocolResponseInterceptor{func process<ReturnType>(endpoint:Endpoint<ReturnType>, response:NSHTTPURLResponse?, responseObject:ResponseObject)->ProcessResults}
Examples:
Retry (Retries requests on response if not 2xx code)
It is left completely up to the developer on how you want to implement the Parseable protocol. You can manually create and serialize your objects:
classUser:AnyObject,Parseable{varname:String?varemail:String?varpictureURL:NSURL?staticfunc parseResponseObject(responseObject:AnyObject)throws->AnyObject{iflet dict = responseObject as?Dictionary<String,AnyObject>{letuser=User()
user.name =dict["name"]as?String
user.email =dict["email"]as?String
user.pictureURL =NSURL(string:dict["avatar_url"]as!String)return user
}
// If parsing encounters an error, throw enum that conforms to ErrorType.
throwBridgeErrorType.Parsing
}}
Or you can also serialize them using whatever serialization libraries you like. This gist is an example of out out-of-box working solution for Mantle if you're already using Mantle models. No code change is required to your Mantle models.
Once models are setup, making calls are as simple as:
Similar to how Rails maps :id for resources, # is used as the character where a variable would be inserted into the path.
GET<Dict>("/photos/#") will map to /photos/1 if you pass in 1 in the first variadic parameter when you call execute(). You can have multiple variables, they will be mapped in order respectively.