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

CODE RUB: Some thoughts from watching https://www.youtube.com/watch?v=5Htj8JFglb4 #3

Open
TehWardy opened this issue Jul 30, 2021 · 0 comments

Comments

@TehWardy
Copy link

TehWardy commented Jul 30, 2021

Interesting Hassan.

Whilst following your guide video I couldn't see what ApiBroker did beyond providing some sort of wrapper around a HttpClient instance. My initial thoughts were ... could he not just use HttpClient directly?

That got me thinking, most API's (like mine) are built to a standard like OData so some common extensions might be all that's needed, or did I miss something?

I would do something like this ...

public static class HttpClientExtensions
{
       ILogger log; 
       
       static HttpClientExtensions(ILogger log) => this.log = log;

        public static async Task<Token> Authenticate(this HttpClient client, string user, string pass)
        {
            try
            {
                var auth = new { User = user, Pass = pass };
                var payload = new StringContent(auth.ToJsonForOdata(), Encoding.UTF8, "application/json");
                var response = await client.PostAsync("Account/Login", payload);
                response.EnsureSuccessStatusCode();
                var token = await response.Content.ReadAsAsync<Token>();
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", token.Id);
                return token;
            }
            catch { /* if we get here the server returned a json repsonse but it wasn't a token, it was more than likely an auth failure. */ }

            log.Warn("Auth request Failed for user " + user);
            return null;
        }

        public static Task<ODataCollection<T>> GetODataCollection(this HttpClient client, string query)
             => client.GetAsync(query)
                .ContinueWith(async t => await t.Result.Content.ReadAsAsync<ODataCollection<T>>())
                .Unwrap();
}

Then in my "broker consuming" code ...

var api = new HttpClient("base url");
await api.Authenticate("user", "pass");
var someFoos = (await api.GetODataCollection<Foo>("Foo?filter= ...")).ToArray();

Unless there's some function beyond this wrapper function for Brokers you could remove that layer altogether.
I did then noticed that you built a "LoggingBroker", would inject as a dependency an Ilogger instance through the usual mechanisms and if need be build my own ILogger structure that uses a http client within it as logging is something that happens literally everywhere in an application.

@hassanhabib hassanhabib changed the title Some thoughts from watching https://www.youtube.com/watch?v=5Htj8JFglb4 CODE RUB: Some thoughts from watching https://www.youtube.com/watch?v=5Htj8JFglb4 Oct 24, 2021
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

1 participant