Skip to content

Commit

Permalink
NAS-132607 / 25.04 / Adapting to new API (#11143)
Browse files Browse the repository at this point in the history
  • Loading branch information
undsoft authored Dec 6, 2024
1 parent efb4398 commit 3408719
Show file tree
Hide file tree
Showing 149 changed files with 1,122 additions and 631 deletions.
4 changes: 2 additions & 2 deletions proxy.config.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
"target": "http://_REMOTE_",
"secure": false,
"changeOrigin": true,
"pathRewrite": {"^/_upload" : "http://_REMOTE_:6000/_upload"},
"pathRewrite": {"^/_upload" : "http://_REMOTE_/_upload"},
"loglevel": "debug"
},
"/_download": {
"target": "http://_REMOTE_",
"secure": false,
"changeOrigin": true,
"pathRewrite": {"^/_download" : "http://_REMOTE_:6000/_download"},
"pathRewrite": {"^/_download" : "http://_REMOTE_/_download"},
"loglevel": "debug"
}
}
4 changes: 4 additions & 0 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { WINDOW } from 'app/helpers/window.helper';
import { AuthService } from 'app/services/auth/auth.service';
import { DetectBrowserService } from 'app/services/detect-browser.service';
import { LayoutService } from 'app/services/layout.service';
import { PingService } from 'app/services/websocket/ping.service';

@UntilDestroy()
@Component({
Expand All @@ -26,6 +27,7 @@ export class AppComponent implements OnInit {
private authService: AuthService,
private detectBrowser: DetectBrowserService,
private layoutService: LayoutService,
private pingService: PingService,
@Inject(WINDOW) private window: Window,
) {
this.authService.isAuthenticated$.pipe(untilDestroyed(this)).subscribe((isAuthenticated) => {
Expand Down Expand Up @@ -61,6 +63,8 @@ export class AppComponent implements OnInit {
}
console.error(err);
};

this.pingService.setupPing();
}

ngOnInit(): void {
Expand Down
14 changes: 8 additions & 6 deletions src/app/core/testing/classes/mock-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import {
ApiJobParams,
ApiJobResponse,
} from 'app/interfaces/api/api-job-directory.interface';
import { ApiEvent } from 'app/interfaces/api-message.interface';
import { ApiEventTyped } from 'app/interfaces/api-message.interface';
import { Job } from 'app/interfaces/job.interface';
import { ApiService } from 'app/services/websocket/api.service';
import { SubscriptionManagerService } from 'app/services/websocket/subscription-manager.service';
import { WebSocketHandlerService } from 'app/services/websocket/websocket-handler.service';

/**
Expand All @@ -41,14 +42,15 @@ const anyArgument = when((_: ApiJobParams<ApiJobMethod>) => true);
*/
@Injectable()
export class MockApiService extends ApiService {
private subscribeStream$ = new Subject<ApiEvent>();
private subscribeStream$ = new Subject<ApiEventTyped>();
private jobIdCounter = 1;

constructor(
protected override wsHandler: WebSocketHandlerService,
protected override translate: TranslateService,
wsHandler: WebSocketHandlerService,
subscriptionManager: SubscriptionManagerService,
translate: TranslateService,
) {
super(wsHandler, translate);
super(wsHandler, subscriptionManager, translate);

this.call = jest.fn();
this.job = jest.fn();
Expand Down Expand Up @@ -127,7 +129,7 @@ export class MockApiService extends ApiService {
this.jobIdCounter += 1;
}

emitSubscribeEvent(event: ApiEvent): void {
emitSubscribeEvent(event: ApiEventTyped): void {
this.subscribeStream$.next(event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { MockEnclosureGenerator } from 'app/core/testing/mock-enclosure/mock-enc
import { ApiCallMethod, ApiCallParams, ApiCallResponse } from 'app/interfaces/api/api-call-directory.interface';
import { SystemInfo } from 'app/interfaces/system-info.interface';
import { ApiService } from 'app/services/websocket/api.service';
import { SubscriptionManagerService } from 'app/services/websocket/subscription-manager.service';
import { WebSocketHandlerService } from 'app/services/websocket/websocket-handler.service';

@Injectable({
Expand All @@ -19,9 +20,10 @@ export class MockEnclosureApiService extends ApiService {

constructor(
wsManager: WebSocketHandlerService,
subscriptionManager: SubscriptionManagerService,
translate: TranslateService,
) {
super(wsManager, translate);
super(wsManager, subscriptionManager, translate);

console.warn('MockEnclosureApiService is in effect. Some calls will be mocked');
}
Expand Down
9 changes: 7 additions & 2 deletions src/app/core/testing/utils/mock-api.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ApiCallMethod } from 'app/interfaces/api/api-call-directory.interface';
import { ApiJobDirectory, ApiJobMethod } from 'app/interfaces/api/api-job-directory.interface';
import { Job } from 'app/interfaces/job.interface';
import { ApiService } from 'app/services/websocket/api.service';
import { SubscriptionManagerService } from 'app/services/websocket/subscription-manager.service';
import { WebSocketHandlerService } from 'app/services/websocket/websocket-handler.service';

/**
Expand Down Expand Up @@ -47,8 +48,12 @@ export function mockApi(
return [
{
provide: ApiService,
useFactory: (wsHandler: WebSocketHandlerService, translate: TranslateService) => {
const mockApiService = new MockApiService(wsHandler, translate);
useFactory: (
wsHandler: WebSocketHandlerService,
translate: TranslateService,
) => {
const subscriptionManager = {} as SubscriptionManagerService;
const mockApiService = new MockApiService(wsHandler, subscriptionManager, translate);
(mockResponses || []).forEach((mockResponse) => {
if (mockResponse.type === MockApiResponseType.Call) {
mockApiService.mockCall(mockResponse.method, mockResponse.response);
Expand Down
7 changes: 0 additions & 7 deletions src/app/enums/api-error-name.enum.ts

This file was deleted.

21 changes: 0 additions & 21 deletions src/app/enums/api-message-type.enum.ts

This file was deleted.

36 changes: 36 additions & 0 deletions src/app/enums/api.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export enum ApiErrorName {
NotAuthenticated = 'ENOTAUTHENTICATED',
NoAccess = 'EACCES',
NoMemory = 'ENOMEM',
AlreadyExists = 'EEXIST',
Again = 'EAGAIN',
Validation = 'EINVAL',
}

export const apiErrorNames = new Map<ApiErrorName, string>([
[ApiErrorName.NotAuthenticated, 'Not Authenticated'],
[ApiErrorName.NoAccess, 'Access Error'],
[ApiErrorName.NoMemory, 'No Memory'],
[ApiErrorName.AlreadyExists, 'Already Exists'],
[ApiErrorName.Again, 'Try Again'],
[ApiErrorName.Validation, 'Validation Error'],
]);

export enum JsonRpcErrorCode {
InvalidRequest = -32600,
MethodNotFound = -32601,
InvalidParams = -32602,
InternalError = -32603,
TooManyConcurrentCalls = -32000,
CallError = -32001,
}

export enum CollectionChangeType {
Changed = 'changed',
Added = 'added',
Removed = 'removed',
}

export enum ShellMessageType {
Connected = 'connected',
}
2 changes: 1 addition & 1 deletion src/app/enums/response-error-type.enum.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export enum ResponseErrorType {
export enum JobExceptionType {
Validation = 'VALIDATION',
}
52 changes: 52 additions & 0 deletions src/app/helpers/api.helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { isObject } from 'lodash-es';
import { ApiError } from 'app/interfaces/api-error.interface';
import {
ErrorResponse,
RequestMessage,
IncomingMessage,
CollectionUpdateMessage, SuccessfulResponse,
} from 'app/interfaces/api-message.interface';
import { Job } from 'app/interfaces/job.interface';

export function isApiError(error: unknown): error is ApiError {
if (error === null) return false;
Expand All @@ -9,3 +17,47 @@ export function isApiError(error: unknown): error is ApiError {
&& 'reason' in error
&& 'trace' in error;
}

export function isFailedJob(obj: unknown): obj is Job {
if (obj === null) return false;

return typeof obj === 'object'
&& ('state' in obj
&& 'error' in obj
&& 'exception' in obj
&& 'exc_info' in obj);
}

export function isIncomingMessage(something: unknown): something is IncomingMessage {
return isObject(something) && 'jsonrpc' in something;
}

export function isCollectionUpdateMessage(something: unknown): something is CollectionUpdateMessage {
return isIncomingMessage(something) && 'method' in something && something.method === 'collection_update';
}

export function isSuccessfulResponse(something: unknown): something is SuccessfulResponse {
return isIncomingMessage(something)
&& 'result' in something;
}

export function isErrorResponse(something: unknown): something is ErrorResponse {
return isIncomingMessage(something)
&& 'error' in something
&& Boolean(something.error);
}

export function extractApiError(someError: unknown): ApiError | undefined {
if (isErrorResponse(someError)) {
return someError.error.data;
}

return undefined;
}

export function makeRequestMessage(message: Pick<RequestMessage, 'id' | 'method' | 'params'>): RequestMessage {
return {
jsonrpc: '2.0',
...message,
};
}
8 changes: 4 additions & 4 deletions src/app/helpers/operators/apply-api-event.operator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { of } from 'rxjs';
import { IncomingApiMessageType } from 'app/enums/api-message-type.enum';
import { CollectionChangeType } from 'app/enums/api.enum';
import { ApiEventTyped } from 'app/interfaces/api-message.interface';
import { Pool } from 'app/interfaces/pool.interface';
import { applyApiEvent } from './apply-api-event.operator';
Expand All @@ -8,7 +8,7 @@ describe('applyApiEvent', () => {
it('adds an item when an Added event is received', () => {
const items = [{ id: 1 } as Pool];
const event = {
msg: IncomingApiMessageType.Added,
msg: CollectionChangeType.Added,
fields: { id: 2 } as Pool,
} as ApiEventTyped<'pool.query'>;

Expand All @@ -20,7 +20,7 @@ describe('applyApiEvent', () => {
it('updates an item when a Changed event is received', () => {
const items = [{ id: 1, name: 'pool1' } as Pool];
const event = {
msg: IncomingApiMessageType.Added,
msg: CollectionChangeType.Added,
fields: { id: 1, name: 'pool2' } as Pool,
} as ApiEventTyped<'pool.query'>;

Expand All @@ -32,7 +32,7 @@ describe('applyApiEvent', () => {
it('removes an item when a Removed event is received', () => {
const items = [{ id: 1, name: 'pool1' }, { id: 2, name: 'pool2' }] as Pool[];
const event = {
msg: IncomingApiMessageType.Removed,
msg: CollectionChangeType.Removed,
id: 1,
} as ApiEventTyped<'pool.query'>;

Expand Down
8 changes: 4 additions & 4 deletions src/app/helpers/operators/apply-api-event.operator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OperatorFunction, map } from 'rxjs';
import { IncomingApiMessageType } from 'app/enums/api-message-type.enum';
import { CollectionChangeType } from 'app/enums/api.enum';
import { ApiCallAndSubscribeMethod, ApiCallAndSubscribeResponse } from 'app/interfaces/api/api-call-and-subscribe-directory.interface';
import { ApiCallResponse } from 'app/interfaces/api/api-call-directory.interface';
import { ApiEventTyped } from 'app/interfaces/api-message.interface';
Expand All @@ -9,11 +9,11 @@ export function applyApiEvent<
>(): OperatorFunction<[ApiCallResponse<M>, ApiEventTyped<M>], ApiCallAndSubscribeResponse<M>[]> {
return map(([items, event]) => {
switch (event?.msg) {
case IncomingApiMessageType.Added:
case CollectionChangeType.Added:
return [...items, event.fields];
case IncomingApiMessageType.Changed:
case CollectionChangeType.Changed:
return items.map((item) => (item.id === event.id ? event.fields : item));
case IncomingApiMessageType.Removed:
case CollectionChangeType.Removed:
return items.filter((item) => item.id !== event.id);
default:
break;
Expand Down
4 changes: 1 addition & 3 deletions src/app/interfaces/api-error.interface.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { ApiErrorName } from 'app/enums/api-error-name.enum';
import { ResponseErrorType } from 'app/enums/response-error-type.enum';
import { ApiErrorName } from 'app/enums/api.enum';

export interface ApiError {
errname: ApiErrorName;
error: number;
extra: unknown;
reason: string;
trace: ApiErrorTrace;
type: ResponseErrorType | null;
message?: string | null;
}

Expand Down
Loading

0 comments on commit 3408719

Please sign in to comment.