Git Product home page Git Product logo

kulman.wpa81.baserestservice's Introduction

Base REST service for Universal Apps

NuGet version Build status MIT licensed UWP ready WinRT ready WPA81 ready

Base class for a Windows Phone 8.1 (Silverlight), Windows Phone 8.1 XAML and Windows 8.1 REST service implementation. Also works fin in Windows 10 projects (UWP).

Installation

PM> Install-Package Kulman.WPA81.BaseRestService

Usage

Create your service class and inherit from BaseRestService. The minimum you need to do to make it work is to override the GetBaseUrl() method to set the base url for all the requests.

public class MyDataService: BaseRestService
{
    protected override string GetBaseUrl()
    {
        return "my base url";
    }
}

You can (but do not have to) also override the GetRequestHeaders() method to set the default request headers.

protected override Dictionary<string, string> GetRequestHeaders(string requestUrl)
{
    return new Dictionary<string, string>
    {
        { "Accept-Encoding", "gzip, deflate" },
        { "Accept", "application/json" },
    };
}

You can also override the CreateJsonSerializerSettings method with you need custom JSON deserialization settings.

Now you can use the following methods in your class:

Task<T> Get<T>(string url);
Task<T> Put<T>(string url, object request);
Task<T> Post<T>(string url, object request);
Task<T> Patch<T>(string url, object request);
Task Delete(string url);
Task<Dictionary<string, string>> Head(string url);

If you need to get the raw request, there are overloads returning HttpResponseMessage:

Task<HttpResponseMessage> Get(string url);
Task<HttpResponseMessage> Put(string url, object request);
Task<HttpResponseMessage> Post(string url, object request);
Task<HttpResponseMessage> Patch(string url, object request);

All the available methods have overloads accepting and CancellationToken.

Methods in your service may then look like this

public Task<List<Account>> GetAccounts()
{
    return Get<List<Account>>("/accounts");
}
 
public Task<Account> UpdateAccount(Account account)
{
    return Patch<Account>("/accounts",account);
}

For more information, see my blog post REST service base class for Windows Phone 8.1 XAML apps.

For changes, see the changelog.

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.