Přeskočit na hlavní obsah

API requests

API Requests

In order to communicate with the API, you should use the AsolApiDataService from the @asol-platform/services package. It is a wrapper around the Angular HttpClient and provides methods for CRUD operations. Most importantly it handles the authorization header and the impersonation token for you.

The response is always expected to be in JSON format.

Table of contents

Impersonation

Impersonation is a feature that allows you to perform actions on behalf of another user. This is useful besides other uses for testing or debugging purposes. To use impersonation, you need to provide an AsolImpersonationOptions object when making a request. This object contains the following properties:

  • tid: The tenant ID of the user you want to impersonate.
  • orgs_codes: An array of organization codes that the impersonated user belongs to.

Getting data

Get a list of items

To get a list of items, use the get method. It takes the following parameters:

  • uri: The URI of the API endpoint.
  • searchParams: Optional search parameters to be included in the request.
  • headers: Optional custom headers to be included in the request.
  • impersonationOptions: Optional impersonation options.

The result is an observable of AsolCollectionResultModel<TRead>, where TRead is the type of the items in the collection.

The AsolCollectionResultModel contains the following properties:

  • items: An array of items returned by the API.
  • totalCount: The total number of items available.

Internally, the get method uses the HttpClient to make a GET request to the specified URI with the provided search parameters and headers.

Get a single item

To get a single item, use the getOne method. It takes the following parameters:

  • uri: The URI of the API endpoint.
  • searchParams: Optional search parameters to be included in the request.
  • headers: Optional custom headers to be included in the request.
  • impersonationOptions: Optional impersonation options.

The result is an observable of TRead, where TRead is the type of the item returned by the API.

Internally, the getOne method uses the HttpClient to make a GET request to the specified URI with the provided search parameters and headers.

Modifying data

Create an item / Posting data

To create an item, use the create method. This method should also be used for requests that need to post data to the backend, like sending data grid's data request. It takes the following parameters:

  • uri: The URI of the API endpoint.
  • data: The data to be sent in the request body.
  • parameters: Optional query parameters to be included in the request.
  • headers: Optional custom headers to be included in the request.
  • impersonationOptions: Optional impersonation options.

The result is an observable of TReturn, where TReturn is the type of the item returned by the API.

Internally, the create method uses the HttpClient to make a POST request to the specified URI with the provided data, query parameters, and headers.

Update an item

To update an item, use the update method. It takes the following parameters:

  • uri: The URI of the API endpoint.
  • data: The data to be sent in the request body.
  • parameters: Optional query parameters to be included in the request.
  • headers: Optional custom headers to be included in the request.
  • impersonationOptions: Optional impersonation options.

The result is an observable of TRead, where TRead is the type of the item returned by the API.

Internally, the update method uses the HttpClient to make a PUT request to the specified URI with the provided data, query parameters, and headers.

Patch an item

To patch an item, use the patch method. It takes the same parameters as the update method and behaves similarly, but it uses the PATCH HTTP method instead of PUT and serves to send partial updates to the item instead of replacing the entire object.

Delete an item

To delete an item, use the delete method. It takes the following parameters:

  • uri: The URI of the API endpoint.
  • id: Optional ID of the item to be deleted. If provided, it will be appended to the URI.
  • params: Optional query parameters to be included in the request.
  • headers: Optional custom headers to be included in the request.
  • impersonationOptions: Optional impersonation options.

The result is an observable of TDelete, where TDelete is the type of the item returned by the API.

Internally, the delete method uses the HttpClient to make a DELETE request to the specified URI with the provided ID, query parameters, and headers.

Default headers

The default headers used in the requests are:

return new HttpHeaders()
.set('Accept', 'application/json-patch+json')
.set('Content-Type', 'application/json-patch+json');