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

Custom ParameterEncodings #90

Closed
chipsterm opened this issue Dec 23, 2014 · 6 comments
Closed

Custom ParameterEncodings #90

chipsterm opened this issue Dec 23, 2014 · 6 comments
Assignees

Comments

@chipsterm
Copy link

I have been working with this framework on iOS and am having a compile issue when adding custom parameterEncodings. I was wondering if you could help me out.

My EyeQEndPoints enum does not implement URLRequestConvertible which I believe is ok.

I declare the custom closure as follows:

let xmlParameterEncoding:(URLRequestConvertible, [String: AnyObject]?) -> (NSURLRequest, NSError?) = {(URLRequest, parameters) in {

//this is just to get things working
return (NSURLRequest.URLRequest, nil)
}

In the endpointsClosure, I set the custom closure:

let endpointsClosure = { (target: EyeQEndPoints, method: Moya.Method, parameters: [String: AnyObject]) -> Endpoint in
return Endpoint(URL: url(target), sampleResponse: EndpointSampleResponse.Success(200, target.sampleData), method: method, parameters: parameters, parameterEncoding: .Custom(xmlParameterEncoding))
}

The compiler throws an error on .Custom(xmlParameterEncoding) of

error: type 'URLRequestConvertible' does not conform to protocol 'URLRequestConvertible'
return Endpoint(URL: url(target), sampleResponse: EndpointSampleResponse.Success(200, target.sampleData), method: method, parameters: parameters, parameterEncoding: .Custom(xmlParameterEncoding))

Thanks for your help.

@ashfurrow
Copy link
Member

Hi there! Got your email too. I'll take a look as soon as I can. Happy holidays!

@orta
Copy link
Member

orta commented Dec 25, 2014

Would strongly recommend taking a look at the Eidolon source code to see if it has something that you're looking for.

@ashfurrow
Copy link
Member

Hmm. Eidolon uses the default parameter encoding, so it won't be much help. I'm trying out your sample code now and I'll let you know what I find.

@ashfurrow
Copy link
Member

OK, I think I've got things working. First, the URLRequestConvertible actually belongs to Alamofire, so you need to have import Alamofire at the top of your file. This is a leaky abstraction on my part – I'll open an issue.

OK, so now for some Swift stuff. The syntax surrounding closures as variables is ... not intuitive 😄 I played around with things until I got the following to work:

let xmlParameterEncoding: (URLRequestConvertible, [String: AnyObject]?) -> (NSURLRequest, NSError?) = { convertible, parameters -> (NSURLRequest, NSError?) in
    //this is just to get things working
    return (convertible.URLRequest, nil)
}

let closure = { (target: EyeQEndPoints, method: Moya.Method, parameters: [String: AnyObject]) -> Endpoint< EyeQEndPoints> in
    return Endpoint< EyeQEndPoints>(URL: url(target), sampleResponse: .Success(200, target.sampleData), method: method, parameters: parameters, parameterEncoding: Moya.ParameterEncoding.Custom(xmlParameterEncoding))
}

let abcd = MoyaProvider(endpointsClosure: closure )

A few important notes: the xmlParameterEncoding closure has the variables names, not types, after the {, and I had to fully qualify the .Custom with Moya.ParameterEncoding, since Alamofire exposes an identical enum with the same name.

But that should do it! Let me know if I can clarify anything.

@chipsterm
Copy link
Author

Thank you for the response. I figured that out shortly after I created the ticket.

@ashfurrow
Copy link
Member

Awesome! I'll close this. Feel free to reopen or open a new issue if you have other questions.

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

No branches or pull requests

3 participants