GithubHelp home page GithubHelp logo

c-lamont / xablu.webapiclient Goto Github PK

View Code? Open in Web Editor NEW

This project forked from xablu/xablu.webapiclient

0.0 1.0 0.0 194 KB

The Xablu WebApiClient component is a C# HTTP library which aims to simplify consuming of Web API services in .NET projects.

License: MIT License

C# 89.67% PowerShell 6.70% Shell 3.63%

xablu.webapiclient's Introduction

Xablu.WebApiClient

The Xablu WebApiClient is a C# HTTP library which aims to simplify consuming of Web API services in .NET projects.

Setup & Usage

Build Status:

Build status GitHub tag NuGet MyGet

Usage

Standard

Create a new singleton of WebApiClient and use it to do REST operations.

var restApiClient = new RestApiClient("http://baseurl.com/");

MvvmCross

If you want to use the standard HTTP client handlers, just register IRestApiClient with a base url. You can set the type of native handler in your csproj settings.

Mvx.RegisterSingleton<IRestApiClient>(new RestApiClient(Constants.ApiBaseUrl));

When using custom HTTP handler, register a custom iOS Handler

var options = new RestApiClientOptions(Constants.ApiBaseUrl) 
{
    DefaultHttpMessageHandler = () => new NSUrlSessionHandler();
};
Mvx.RegisterSingleton<IRestApiClient>(new RestApiClient(options));

Register the custom handler in Android

var options = new RestApiClientOptions(Constants.ApiBaseUrl) 
{
    DefaultHttpMessageHandler = () => new AndroidClientHandler();
};
Mvx.RegisterSingleton<IRestApiClient>(new RestApiClient(options));

Create a client to handle Http traffic

public class AuthenticationClient : IAuthenticationClient
{
   public AuthenticationClient(IRestApiClient apiClient) : base(apiClient)
   {
   }

   public Task<int> AuthenticateWithCredentials(string username, string password)
   {
      var uri = new UriTemplate("user/logon");

      var content = new MultipartFormDataContent();
      content.Add(new StringContent(username), "username");
      content.Add(new StringContent(password), "password");

      return ExecuteRemoteRequest(async () => await apiClient.PostAsync<MultipartFormDataContent, int>(Priority.UserInitiated, uri.Resolve(), content).ConfigureAwait(false));
   }
}

Add a Service to change and handle user logic

public class AuthenticationService : IAuthenticationService
{
   private IBlobCache cache;
   private IAuthenticationClient authClient;

   public AuthenticationService(IAuthenticationClient authClient, IBlobCache cache = null)
      : base()
   {
      this.cache = (cache ?? BlobCache.Secure);
      this.authClient = authClient;
   }

   public async Task<bool> Login(string username, string password)
   {
      var userId = await authClient.AuthenticateWithCredentials(username, password).ConfigureAwait(false);

      if (userId != default(int))
      {
         await this.cache.InsertObject(Settings.UserIdCacheKey, userId);
	 return true;
      }

      return false;
   }

   public async Task<bool> IsAuthenticated()
   {
      try
      {
         var userId = await cache.GetObject<int>(Settings.UserIdCacheKey).ToTask().ConfigureAwait(false);
	 return true;
      }
      catch (KeyNotFoundException)
      {
         return false;
      }
   }

   public async Task Logout()
   {
      await cache.InvalidateAll().ToTask().ConfigureAwait(false);
   }
}

xablu.webapiclient's People

Contributors

marcbruins avatar martijn00 avatar mvanbeusekom avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.