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

More complex parameters structure #2

Open
iosware opened this issue May 5, 2017 · 4 comments
Open

More complex parameters structure #2

iosware opened this issue May 5, 2017 · 4 comments

Comments

@iosware
Copy link

iosware commented May 5, 2017

I tried to use this great example to work with some Wordpress plugins.

But there can be situations when POST parameters should generate certain JSON structure.
E.g.
{ "title":"aaa", "post":23, "seo": { "keywords":"bmw,audi", "rating":"10", "optimize":"true" } }

Current approach to use enums won't work to generate such structure.
public enum basicParameters: EVAssociated { case title(String) case post(Number) case seo([seoParameters]) } public enum seoParameters: EVAssociated { case keywords(String) case rating(Number) case optimize(Bool) }
So possibly I'm doing it wrong though I didn't want to change anything in "EVWordPressAPI".
I would appreciate if you can suggest correct way to generate required JSON structure as it will allow to use "EVWordPressAPI" with some plugins too.

@evermeer
Copy link
Owner

evermeer commented May 6, 2017

I have chosen the enum approach as a standard for the API. It's not that difficult to create a call that accepts an object instead of an enum and then use the json of that object instead of the enum. Which API call(s) accept an object instead of a parameter list?

@iosware
Copy link
Author

iosware commented May 6, 2017

@evermeer
It is custom Worpress plugin which accepts object as parameter. So as I understand genericCall should be changed in EVWordPressAPI to accept object in parameter. But won't it require changing current parameters as enums approach including WordPressRequestConvertible where parameters are parsed?

Alamofire does allow it using JSON-Encoded Parameters but EVWordPressAPI accepts only enums as parameters.

@iosware iosware closed this as completed May 11, 2017
@evermeer
Copy link
Owner

Ah, but it would not be that hard to implement a notGenericCall function based on the existing code. The genericCall code looks like this:

    internal func genericCall<T:WPObject, P>(_ path:String, parameters:[P]?, completionHandler: @escaping (T?) -> Void) where P: EVAssociated {
        Alamofire.request(self.wordpressOauth2Settings.baseURL + path, parameters: Dictionary<String,AnyObject>(associated: parameters))
        .responseObject(completionHandler: {  (result:DataResponse<T>) -> Void in
            self.handleResponse(result, completionHandler: completionHandler)
        })
    }

I even think this is all that it takes.

    internal func noneGenericCall<T:WPObject>(_ path:String, parameters:[String:AnyObject]?, completionHandler: @escaping (T?) -> Void)  {
        Alamofire.request(self.wordpressOauth2Settings.baseURL + path, parameters:  parameters)
        .responseObject(completionHandler: {  (result:DataResponse<T>) -> Void in
            self.handleResponse(result, completionHandler: completionHandler)
        })
    }

Maybe I can do some tests this weekend and add it to EVWordpressAPI

@iosware
Copy link
Author

iosware commented May 16, 2017

Excellent - new method can solve such issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants