# REST Base Service

We have a base REST API service that we can leverage in the entire app stack.

It encapsulates the ceremony of dealing with backend calls, and provides us with a clean set of generic, async methods.

It's located in: lib-oga-webui-sharedkernel.

To use in a component or service, import it with this:

```typescript
import { RestBaseService } from 'lib-oga-webui-sharedkernel';
```

Add it to your constructor or a private injection:

```typescript
constructor(private _restsvc: RestBaseService)
{
  ...
}
```

Now, you can use its various calls.

Each call has these properties:

- Uses one of the four verbs: GET, POST, PUT, DELETE.
- Accepts the endpoint path fragment as string.  
    Composes the url from the app origin and the api\_BasePath property from environment.ts.
- Accepts an optional header instance, of type HttpHeaders.
- Returns a Promise.  
    Use await to call the method.
- Returns a generic web response object. See this for details: [Generic Web Reply](https://wiki.galaxydump.com/link/155)).  
    The concrete type must be specified when calling the method.

#### API URL

Each method composes the url based on the app's origin and the api\_BasePath property from environment.ts.

The url is composed of this form:

`${origin}${this.env.api_BasePath}/${urlpath}`

The origin will be the app's origin, unless overridden.

<p class="callout info">You can override the origin used by setting the public property, OriginOverride, of the REST service instance before making the call.</p>

The api\_BasePath is defined in your app's environment.ts file.

The urlpath is what your calling method passes in.  
It should include any query parameters, already urlencoded.

#### Example Usage

<p class="callout info">NOTE: The endpoint passed to the method does NOT need to include a leading '/'. This is appended for you.</p>

GET - Returning a void:

```typescript
let res = await this._restsvc.GetRequest<void>("VoidTests_V1/Get/AcceptVoid/ReturnVoid");
if(!res || res === null || !res.IsReturnGood())
{
  // Call failed.
}
// Call returned success.
```

GET - Returning a type:

```typescript
let res = await this._restsvc.GetRequest<sometype>("API_endpoint/Get/Somecall");
if(!res || res === null || !res.IsReturnGood())
{
  // Call returned an error or failed.
}
// Call returned success.
```

POST - Accepting a type and returning a type:

```typescript
let res = await this._restsvc.PostRequest<requesttype,responsetype>("API/ENDPOINT", requestdata);
if(!res || res === null || !res.IsReturnGoodDataExists())
{
  // The call failed, or returned no data.
}
// It succeeded, and we have data.

```

PUT - returning a type:

```typescript
let res = await this._restsvc.PutRequest<requesttype,responsetype>("API/ENDPOINT", requestdata);
if(!res || res === null || !res.IsReturnGoodDataExists())
{
  // The call failed, or returned no data.
}
// It succeeded, and we have data.

```

DELETE - returning a void:

```typescript
let res = await this._restsvc.DeleteRequest<responsetype>("API/ENDPOINT");
if(!res || res === null || !res.IsReturnGood())
{
  // The call failed, or returned no data.
}
// It succeeded, and we have data.
```