MailChimp.NET icon indicating copy to clipboard operation
MailChimp.NET copied to clipboard

Async would be awesome

Open xleon opened this issue 10 years ago • 9 comments

All my api is async but this piece of code. I understand you need to support old .net but what about a new branch for 4.5?

xleon avatar Feb 20 '15 05:02 xleon

Please see this wiki article and this issue. What changes do you think we'd need to make to support async (and probably remove ServiceStack.Text at the same time)?

danesparza avatar Feb 20 '15 13:02 danesparza

(FYI -- I'm asking because you seem opinionated and experienced with async)

danesparza avatar Feb 20 '15 14:02 danesparza

Well, the only thing I did with this library is install it and used the subscribe method with some merge tags. So I will have to take a deep look before evaluating that. I´ve never used ServiceStack.text before. To understand it better, Is there any reason not to use Json.NET ?

xleon avatar Feb 20 '15 15:02 xleon

I think the trick is to make this line async somehow (on MakeAPICall method) :

var resultString = fullUrl.PostJsonToUrl(args);

Then, making the method awaitable:

private async Task<T> MakeAPICall<T>(string apiAction, object args)

And then, making awaitable all the methods with calls to MakeApiCall(). For example:

public async Task<Opened> GetReportOpened(string cId, OpenedOptions opts = null)
        {
            //  Our api action:
            string apiAction = "reports/opened";

            //  Create our arguments object:
            object args = new
            {
                apikey = this.APIKey,
                cid = cId,
                opts = opts
            };

            //  Make the call:
            return await MakeAPICall<Opened>(apiAction, args);
        }

xleon avatar Feb 20 '15 15:02 xleon

Another option would be creating an extension library with all the async method extensions, if you want to keep compatibility with 3.5. I need to make some tests

xleon avatar Feb 20 '15 15:02 xleon

Hi again, Take a look at this:

Task<string> PostJsonToUrlAsync(...)

https://github.com/ServiceStack/ServiceStack/wiki/Http-Utils#async-http-utils

So, using that method, your code would look like this:

var resultString = await fullUrl.PostJsonToUrlAsync(args);

And I think that would be all. It seams that upgrading ServiceStack, making Mailchimp async would be easy. What do you think?

xleon avatar Feb 20 '15 21:02 xleon

The reason we don't upgrade the ServiceStack dependency is because it's not free anymore.

SteveVaneeckhout avatar Feb 21 '15 15:02 SteveVaneeckhout

I understand and so I would suggest to change the underlying REST client to something like https://github.com/paulcbetts/refit or other open source project (RestSharp?)

xleon avatar Feb 21 '15 18:02 xleon

+1 on this. I have a scenario where i need to subscribe a single user to 3 lists. Using the current implementation, can take up to 5-10 seconds. Unfortunately MailChimp don't support a batch operation for this (only support multiple emails to 1 list, not the other way around). So i need a SubscribeAsync method. Might fork this repo and submit a PR. Agree with @xleon - suggest changing to use HttpClient

RPM1984 avatar Jun 26 '15 00:06 RPM1984