From 694296b444ab14b8db2cb2c1483aec3b30ee65c9 Mon Sep 17 00:00:00 2001 From: Rafael Bravo Date: Sun, 8 Apr 2018 13:21:29 -0300 Subject: [PATCH] Update Readme to include code sample (taken from reactxp docs) (#14) --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/README.md b/README.md index b6d3ec0..d488037 100644 --- a/README.md +++ b/README.md @@ -11,3 +11,43 @@ Wraps a single web request. Has lots of overrides for priorization, delays, ret Wraps SimpleWebRequest for usage across a single RESTful service. In our codebase, we have several specific RESTful service interaction classes that each implement GenericRestClient so that all of the requests get the same error handling, authentication, header-setting, etc. + +## GenericRestClient Sample Usage + +``` +import { GenericRestClient, ApiCallOptions } from 'simplerestclients'; +import SyncTasks = require('synctasks'); + +interface User { + id: string; + firstName: string; + lastName: string; +} + +class MyRestClient extends GenericRestClient { + constructor(private _appId: string) { + super('https://myhost.com/api/v1/'); + } + + // Override _getHeaders to append a custom header with the app ID. + protected _getHeaders(options: ApiCallOptions): { [key: string]: string } { + let headers = super._getHeaders(options); + headers['X-AppId'] = this._appId; + return headers; + } + + // Define public methods that expose the APIs provided through + // the REST service. + getAllUsers(): SyncTasks.Promise { + return this.performApiGet('users'); + } + + getUserById(id: string): SyncTasks.Promise { + return this.performApiGet('user/' + id); + } + + setUser(user: User): SyncTasks.Promise { + return this.performApiPut('user/' + user.id, user); + } +} +```