Ejemplo n.º 1
0
        public static RestfulClient ProvideRestClient(IServiceProvider serviceProvider)
        {
            if (serviceProvider is null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            var contextAccessor = serviceProvider.GetService <IHttpContextAccessor>();

            if (contextAccessor is null)
            {
                throw new NullReferenceException(nameof(contextAccessor));
            }

            var context = contextAccessor.HttpContext;

            if (context is null)
            {
                throw new NullReferenceException(nameof(context));
            }

            var client = new RestfulClient();

            var token = context.RetrieveCookieAuthToken();

            if (!string.IsNullOrEmpty(token))
            {
                client.Authenticate(token);
            }

            return(client);
        }