Skip to content

Commit

Permalink
Merge pull request #38 from dimitrov-d/master
Browse files Browse the repository at this point in the history
Implement RPC module
  • Loading branch information
dimitrov-d authored Nov 21, 2024
2 parents 677bcf7 + 711160d commit 7a5042b
Show file tree
Hide file tree
Showing 13 changed files with 270 additions and 32 deletions.
27 changes: 14 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions packages/sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ async function validatePolkadotWalletSignature() {
}

```

## Computing

The Computing module provides functionalities for managing computing contracts, including creating contracts, listing contracts, and interacting with specific contracts for operations like encryption and ownership transfer.
Expand Down Expand Up @@ -435,6 +436,42 @@ console.log(
);
```

## RPCs

The RPC module provides functionalities for managing RPC API keys and listing available endpoints. This module is essential for interacting with the Apillon platform's RPC services dynamically.

### Usage example

```typescript
import { Rpc } from '@apillon/sdk';
import { getConfig } from './helpers/helper';

// Initialize the RPC module
const rpc = new Rpc({
key: 'yourApiKey',
secret: 'yourApiSecret',
});

// Create a new API key
const apiKey = await rpc.createApiKey({
name: 'Test API Key',
description: 'Test Description',
});
console.log('API Key created:', apiKey.name);

// List all API keys
const { items } = await rpc.listApiKeys();
console.log('Total API Keys:', items.length);

// Get a specific API key by ID
const apiKey = await rpc.apiKey(apiKeyId).get();
console.log('API Key UUID:', apiKey.uuid);

// List all available endpoints
const endpoints = await rpc.listEndpoints();
console.log('Total Endpoints:', endpoints.length);
```

## Social

The Social module provides functionalities for managing social hubs and channels within the Apillon platform. This includes creating, listing, and interacting with hubs and channels. In the background it utilizes Grill.chat, a mobile-friendly, anonymous chat application powered by Subsocial.
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@apillon/sdk",
"description": "▶◀ Apillon SDK for NodeJS ▶◀",
"version": "3.5.0",
"version": "3.6.0",
"author": "Apillon",
"license": "MIT",
"main": "./dist/index.js",
Expand Down
6 changes: 6 additions & 0 deletions packages/sdk/src/docs-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export * from './types/storage';
export * from './types/identity';
export * from './types/computing';
export * from './types/social';
export * from './types/cloud-functions';
export * from './types/indexer';
export * from './types/rpc';

export * from './lib/apillon';
export * from './modules/storage/storage';
export * from './modules/storage/storage-bucket';
Expand All @@ -28,3 +32,5 @@ export * from './modules/cloud-functions/cloud-function';
export * from './modules/cloud-functions/cloud-function-job';
export * from './modules/indexing/indexing';
export * from './modules/indexing/indexer';
export * from './modules/rpc/rpc';
export * from './modules/rpc/rpc-api-key';
7 changes: 6 additions & 1 deletion packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ export * from './types/hosting';
export * from './types/storage';
export * from './types/identity';
export * from './types/social';
export * from './types/cloud-functions';
export * from './types/indexer';
export * from './types/rpc';

export * from './lib/apillon';
export * from './lib/common';

export * from './modules/storage/storage';
export * from './modules/hosting/hosting';
export * from './modules/nft/nft';
Expand All @@ -15,4 +20,4 @@ export * from './modules/social/social';
export * from './modules/project/project';
export * from './modules/cloud-functions/cloud-functions';
export * from './modules/indexing/indexing';
export * from './modules/indexing/indexer';
export * from './modules/rpc/rpc';
11 changes: 11 additions & 0 deletions packages/sdk/src/lib/apillon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ export interface ApillonConfig {
debug?: boolean;
}

export interface ICreateApillonModel {
/**
* Name of the model object
*/
name: string;
/**
* Description of the model object
*/
description: string;
}

export class ApillonModule {
protected config: ApillonConfig;

Expand Down
7 changes: 3 additions & 4 deletions packages/sdk/src/modules/cloud-functions/cloud-functions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ApillonModule } from '../../lib/apillon';
import { ApillonModule, ICreateApillonModel } from '../../lib/apillon';
import { ApillonApi } from '../../lib/apillon-api';
import { constructUrlWithQueryParams } from '../../lib/common';
import { IApillonList, IApillonPagination } from '../../types/apillon';
import { ICreateCloudFunction } from '../../types/cloud-functions';
import { CloudFunction } from './cloud-function';
import { CloudFunctionJob } from './cloud-function-job';

Expand Down Expand Up @@ -38,11 +37,11 @@ export class CloudFunctions extends ApillonModule {

/**
* Creates a new cloud function based on the provided data.
* @param {ICreateCloudFunction} data Data for creating the cloud function.
* @param {ICreateApillonModel} data Data for creating the cloud function.
* @returns {CloudFunction} Newly created cloud function.
*/
public async createCloudFunction(
data: ICreateCloudFunction,
data: ICreateApillonModel,
): Promise<CloudFunction> {
const cloudFunction = await ApillonApi.post<
CloudFunction & { functionUuid: string }
Expand Down
44 changes: 44 additions & 0 deletions packages/sdk/src/modules/rpc/rpc-api-key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { ApillonModel } from '../../lib/apillon';

export class RpcApiKey extends ApillonModel {
/**
* Unique identifier of the RPC API key.
*/
public id: number = null;

/**
* Name of the RPC API key.
*/
public name: string = null;

/**
* Description of the RPC API key.
*/
public description: string = null;

/**
* Unique identifier of the project.
*/
public projectUuid: string = null;

/**
* Unique identifier of the RPC API key, used for RPC calls.
*/
public uuid: string = null;

/**
* Array of favorite URLs for the RPC API key.
*/
public urls: string[] = [];

/**
* Constructor which should only be called via Rpc class.
* @param id Unique identifier of the RPC API key.
* @param data Data to populate RPC API key with.
*/
constructor(id: number, data?: Partial<RpcApiKey>) {
super(id.toString());
this.API_PREFIX = `/rpc/api-key/${id}`;
this.populate(data);
}
}
63 changes: 63 additions & 0 deletions packages/sdk/src/modules/rpc/rpc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { ApillonModule, ICreateApillonModel } from '../../lib/apillon';
import { ApillonApi } from '../../lib/apillon-api';
import { constructUrlWithQueryParams } from '../../lib/common';
import { IApillonList, IApillonPagination } from '../../types/apillon';
import { RpcEndpoint } from '../../types/rpc';
import { RpcApiKey } from './rpc-api-key';

export class Rpc extends ApillonModule {
/**
* API URL for RPC module.
*/
private API_PREFIX = '/rpc';

/**
* List all API keys for RPC.
* @param {IApillonPagination} params - The query parameters for filtering API keys.
* @returns The list of API keys.
*/
public async listApiKeys(
params?: IApillonPagination,
): Promise<IApillonList<RpcApiKey>> {
const url = constructUrlWithQueryParams(
`${this.API_PREFIX}/api-key`,
params,
);

const data = await ApillonApi.get<IApillonList<RpcApiKey>>(url);

return {
...data,
items: data.items.map((apiKey) => new RpcApiKey(apiKey.id, apiKey)),
};
}

/**
* Create a new API key for RPC module.
* @param {ApillonApiCreateRpcApiKeyDto} data - The data for creating an API key.
* @returns The created API key.
*/
public async createApiKey(data: ICreateApillonModel): Promise<RpcApiKey> {
const apiKey = await ApillonApi.post<any>(
`${this.API_PREFIX}/api-key`,
data,
);
return new RpcApiKey(apiKey.uuid, apiKey);
}

/**
* Get all available endpoints for the RPC service.
* @returns The list of endpoints.
*/
public async listEndpoints(): Promise<RpcEndpoint[]> {
return await ApillonApi.get<RpcEndpoint[]>(`${this.API_PREFIX}/endpoints`);
}

/**
* @param id Unique API key identifier.
* @returns An empty instance of RpcApiKey.
*/
public apiKey(id: number): RpcApiKey {
return new RpcApiKey(id, null);
}
}
66 changes: 66 additions & 0 deletions packages/sdk/src/tests/rpc.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Rpc } from '../modules/rpc/rpc';
import { RpcApiKey } from '../modules/rpc/rpc-api-key';
import { getConfig } from './helpers/helper';

describe('Rpc Module tests', () => {
let rpc: Rpc;
let apiKeyId: number;

beforeAll(() => {
rpc = new Rpc(getConfig());
});

test('Create new API key', async () => {
const apiKeyData = {
name: 'Test API Key',
description: 'Test Description',
};
const apiKey = await rpc.createApiKey(apiKeyData);

expect(apiKey).toBeInstanceOf(RpcApiKey);
expect(apiKey.name).toEqual(apiKeyData.name);
expect(apiKey.description).toEqual(apiKeyData.description);
expect(apiKey.uuid).toBeTruthy();
expect(apiKey.id).toBeTruthy();

apiKeyId = apiKey.id;
});

test('List API keys', async () => {
const { items } = await rpc.listApiKeys();

expect(items.length).toBeGreaterThanOrEqual(0);
items.forEach((apiKey) => {
expect(apiKey).toBeInstanceOf(RpcApiKey);
expect(apiKey.name).toBeTruthy();
expect(apiKey.uuid).toBeTruthy();
expect(apiKey.id).toBeTruthy();
});
});

test('Get specific API key', async () => {
const apiKey = await rpc.apiKey(apiKeyId).get();

expect(apiKey).toBeInstanceOf(RpcApiKey);
expect(apiKey.id).toEqual(apiKeyId);
});

test('List endpoints', async () => {
const endpoints = await rpc.listEndpoints();

expect(Array.isArray(endpoints)).toBeTruthy();
expect(endpoints.length).toBeGreaterThanOrEqual(0);

endpoints.forEach((endpoint) => {
expect(endpoint).toMatchObject({
id: expect.any(Number),
name: expect.any(String),
imageUrl: expect.any(String),
type: expect.any(String),
version: expect.any(String),
networkName: expect.any(String),
networkId: expect.any(Number),
});
});
});
});
11 changes: 0 additions & 11 deletions packages/sdk/src/types/cloud-functions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
export interface ICreateCloudFunction {
/**
* Name of the cloud function.
*/
name: string;
/**
* Description of the cloud function.
*/
description: string;
}

/**
* Interface for creating a cloud function job.
*/
Expand Down
Loading

0 comments on commit 7a5042b

Please sign in to comment.