Skip to content

Commit

Permalink
provide 'isolated' behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
JanST123 committed Sep 15, 2023
1 parent 83303c8 commit d8d1736
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 40 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ onUserInput(formData) {

You can set global options, which apply on every object type when creating the client. The third parameter of the constructor can be used for options

#### global mode
By default, multiple client instances share token and settings. So if you create a new client instance, all the settings you apply there, will also be applied to all other instance. Even if you instanciate a new client instance with a different token, all the other instances will use the new token. This is due to historical reasonls.

**Example**

```js
Expand All @@ -146,6 +149,16 @@ client.Server.setDefaults({

You can also set the options for a single request to filter your objects. This will override global and per-object-type settings

#### isolated mode
The isolated mode behaves more common; each `Client` instance uses it's own settings. So you can have multiple instances with, for example, different tokens. Just set the 4th constructor property to `true`

**Example**

```js
const client = new gridscale.Client("[API-Token]", "[User-UUID]", {}, true);
```

### Filters
**Example**

```js
Expand All @@ -160,7 +173,7 @@ client.Server.list({

In this example the result will be the first 10 servers with more then 16GB of memory. Sorted by name and only returning the `name` and the `object_uuid`.

### Available filter operators:
#### Available filter operators:

Here you find an overview of the filter operators available when using the `filter` option.

Expand Down
89 changes: 50 additions & 39 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { api, ApiSettings, LogData } from './api';
import { api as globalApi, ApiSettings, LogData, APIClass } from './api';

import { Server } from './Objects/Server';
import { Storage } from './Objects/Storage';
Expand Down Expand Up @@ -66,7 +66,9 @@ class GridscaleClient {
public ServiceMarketplacePlanSettings: MarketplacePlanSettings;
public ServiceMarketplaceVersion: MarketplaceVersion;

public watchRequest: (_requestid: string) => ReturnType<typeof api.watchRequest>;
public watchRequest: (_requestid: string) => ReturnType<typeof globalApi.watchRequest>;

private myapi: APIClass;

/**
* Init Client with Default Values
Expand All @@ -75,51 +77,56 @@ class GridscaleClient {
* @param _token Security Token
* @param _userId UUID of User
* @param _options
* @param _isolated (if true, use isolated api which can be used alongside other instances. Default behavior is shared settings/tokens between the client instances)
*/
constructor(_token: string, _userId: string, _options: ApiSettings = {}) {
constructor(_token: string, _userId: string, _options: ApiSettings = {}, _isolated = false) {

if (_isolated) {
this.myapi = new APIClass();
}

// Store Security Tokens
api.storeToken(_token, _userId);
this.api.storeToken(_token, _userId);

// Store advanced Options
api.setOptions(_options);
this.api.setOptions(_options);

// Call Subtypes
this.Server = new Server(api);
this.Storage = new Storage(api);
this.Network = new Network(api);
this.IP = new IP(api);
this.ISOImage = new ISOImage(api);
this.SSHKey = new SSHKey(api);
this.Template = new Template(api);
this.Location = new Location(api);
this.ObjectStorage = new ObjectStorage(api);
this.Label = new Label(api);
this.Loadbalancer = new Loadbalancer(api);
this.Events = new Events(api);
this.Firewall = new Firewall(api);
this.PAAS = new PAAS(api);
this.PaasServiceTemplate = new PaasServiceTemplate(api);
this.PaasService = new PaasService(api);
this.PaasSecurityZone = new PaasSecurityZone(api);
this.Deleted = new Deleted(api);
this.MarketplaceApplication = new MarketplaceApplication(api);
this.ServiceMarketplaceApplication = new ServiceMarketplaceApplication(api);
this.ServiceMarketplaceApplicationInstance = new MarketplaceApplicationInstance(api);
this.ServiceMarketplacePlan = new MarketplacePlan(api);
this.ServiceMarketplacePlanSettings = new MarketplacePlanSettings(api);
this.ServiceMarketplaceVersion = new MarketplaceVersion(api);
this.Certificate = new Certificate(api);
this.BackupLocation = new BackupLocation(api);
this.watchRequest = api.watchRequest.bind(api);
this.Server = new Server(this.api);
this.Storage = new Storage(this.api);
this.Network = new Network(this.api);
this.IP = new IP(this.api);
this.ISOImage = new ISOImage(this.api);
this.SSHKey = new SSHKey(this.api);
this.Template = new Template(this.api);
this.Location = new Location(this.api);
this.ObjectStorage = new ObjectStorage(this.api);
this.Label = new Label(this.api);
this.Loadbalancer = new Loadbalancer(this.api);
this.Events = new Events(this.api);
this.Firewall = new Firewall(this.api);
this.PAAS = new PAAS(this.api);
this.PaasServiceTemplate = new PaasServiceTemplate(this.api);
this.PaasService = new PaasService(this.api);
this.PaasSecurityZone = new PaasSecurityZone(this.api);
this.Deleted = new Deleted(this.api);
this.MarketplaceApplication = new MarketplaceApplication(this.api);
this.ServiceMarketplaceApplication = new ServiceMarketplaceApplication(this.api);
this.ServiceMarketplaceApplicationInstance = new MarketplaceApplicationInstance(this.api);
this.ServiceMarketplacePlan = new MarketplacePlan(this.api);
this.ServiceMarketplacePlanSettings = new MarketplacePlanSettings(this.api);
this.ServiceMarketplaceVersion = new MarketplaceVersion(this.api);
this.Certificate = new Certificate(this.api);
this.BackupLocation = new BackupLocation(this.api);
this.watchRequest = this.api.watchRequest.bind(this.api);
}

/**
* Set the identifier of the client (used in X-Api-Client Header)
* @param _client
*/
public setApiClient(_client: string) {
api.storeClient(_client);
this.api.storeClient(_client);
}

/**
Expand All @@ -128,47 +135,47 @@ class GridscaleClient {
* @param _userId
*/
public setToken (_token: string, _userUUID: string) {
api.storeToken(_token, _userUUID);
this.api.storeToken(_token, _userUUID);
}

/**
* Set the HTTP endpoint of the API
* @param _endpoint
*/
public setEndpoint(_endpoint: string) {
api.setOptions({ endpoint: _endpoint });
this.api.setOptions({ endpoint: _endpoint });
}

/**
* Inject a custom fetch method, otherwise the API will decide if to use the browser's fetch method or a polyfill
* @param _fetch
*/
public setFetch(_fetch: Function) {
api.setOptions({ fetch: fetch });
this.api.setOptions({ fetch: fetch });
}

/**
* Add an additional logger callback, called whenever an error is happening
* @param _callback
*/
public addLogger ( _callback: (logData: LogData) => void ) {
api.addLogger( _callback );
this.api.addLogger(_callback);
}

/**
* Calls the Validate Token Endpoint of the API
* @returns HTTP Promise
*/
public validateToken() {
return api.get('/validate_token');
return this.api.get('/validate_token');
}

/**
* Get the paas service metrics API which is a special one as the service-uuid is required early in the URL
* @param _serviceUUID
*/
public PaasServiceMetrics(_serviceUUID: string) {
return new PaasServiceMetrics(api, _serviceUUID);
return new PaasServiceMetrics(this.api, _serviceUUID);
}

/**
Expand Down Expand Up @@ -197,6 +204,10 @@ class GridscaleClient {

return tmp;
}

get api() {
return this.myapi || globalApi;
}

}

Expand Down

0 comments on commit d8d1736

Please sign in to comment.