I'm mostly concerned around the appropriate use of
ConfigureAwait(false)…
Your usage is correct. In library code like this, every await should use ConfigureAwait(false).
Though one thing I would do differently are the methods that call Get. Instead of:
public async Task<List<Note>> GetNotes()
{
return await Get<List<Note>>("notes/").ConfigureAwait(false);
}
You can write just:
public Task<List<Note>> GetNotes()
{
return Get<List<Note>>("notes/");
}
… and possible poor handling and duplication of the
HttpClientinInvestandGet<T>.
You could reduce the duplication by creating a helper function like:
private async Task<T> GetResponse<T>(
Func<HttpClient, Task<HttpResponseMessage>> createRequestFunc)
Both Get and Invest would call this method, with the different parts passed in as a lambda.
var investment = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("listingId", listingId),
new KeyValuePair<string, string>("amount", amount)
};
var content = new FormUrlEncodedContent(investment);
Here, investment has to be IEnumerable<KeyValuePair<string, string>>. Dictionary<string, string> also implements that interface and since order doesn't matter here and it would make the code simpler, I would use that:
var investment = new Dictionary<string, string>
{
{ "listingId", listingId },
{ "amount", amount }
};
--
Additionally, I'd also like thoughts on the fact that for usage of these methods, one must call .Result to get the actual results.
No! To use these methods, one should use await. If you're going to use Result:
- You didn't have to bother with
async, it won't given you any advantage, sinceResultwill block a thread. - You are likely to get a deadlock if you forgot
ConfigureAwait(false)somewhere.