Skip to content

Commit

Permalink
Merge pull request #37 from MetabobProject/reset-analyze-on-save-config
Browse files Browse the repository at this point in the history
feat: analyze on save config set to false by default
  • Loading branch information
AviGopal authored Mar 5, 2024
2 parents 580d9ab + e896ed1 commit 90f15e5
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 37 deletions.
34 changes: 34 additions & 0 deletions ext-src/helpers/CreateOrUpdateUserSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as vscode from 'vscode'
import { GetAPIConfig, GetRequestParamId } from '../config'
import { sessionService, CreateSessionRequest } from '../services'
import { Session } from '../state'
import debugChannel from '../debug'

export async function createOrUpdateUserSession(context: vscode.ExtensionContext): Promise<undefined> {
const sessionState = new Session(context)
Expand All @@ -15,6 +16,39 @@ export async function createOrUpdateUserSession(context: vscode.ExtensionContext
}

const sessionToken = sessionState.get()?.value

const one_minute = 60_000;
const thirty_minutes = one_minute * 30;

// Periodically checking the session
setInterval(() => {
sessionService.getUserSession(sessionToken || '')
.then((response) => {
if (response.isOk()) {
// @ts-ignore
const status = response.value?.httpConfig?.status;
if (status === 200) {
debugChannel.appendLine("Metabob: Successfully checked the session of the user \n");
} else if (status === 404) {
debugChannel.appendLine("Metabob: Session of User does not exist. Recreating it again.. \n");
sessionService.createUserSession(payload)
.then((response) => {
if (response.isOk()) {
if (response.value?.session) {
sessionState.set(response.value?.session)
}
}
})
}
}
})
.catch((error) => {
debugChannel.appendLine("Metabob: Error while activating sesstion \n" + JSON.stringify(error));
})

}, thirty_minutes);


if (sessionToken) {
payload['sessionToken'] = sessionToken
}
Expand Down
91 changes: 55 additions & 36 deletions ext-src/services/base.service.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,50 @@
import axios, { AxiosRequestConfig } from 'axios'
import { err, ok, Result } from 'rusty-result-ts'
import { GetAPIBaseURLConfig } from '../config'
import { ApiErrorBase } from './base.error'
import FormData from 'form-data'
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import { err, ok, Result } from 'rusty-result-ts';
import { GetAPIBaseURLConfig } from '../config';
import { ApiErrorBase } from './base.error';
import FormData from 'form-data';

const apiBase = GetAPIBaseURLConfig()
const apiBase = GetAPIBaseURLConfig();

export class ApiServiceBase {
protected urlBase = apiBase === undefined || apiBase === '' ? 'https://ide.metabob.com' : apiBase
protected urlBase = apiBase === undefined || apiBase === '' ? 'https://ide.metabob.com' : apiBase;

/**
* Creates a new service instance.
* @param path A base path for all requests this service will make. Defaults to `/api`.
*/
public constructor() {
this.urlBase = apiBase === undefined || apiBase === '' ? 'https://ide.metabob.com' : apiBase
this.urlBase = apiBase === undefined || apiBase === '' ? 'https://ide.metabob.com' : apiBase;
}

/**
* Returns a new instance of the base config for all requests this service makes.
* @protected
*/
protected getConfig(sessionToken?: string, formDataHeaders?: FormData.Headers): AxiosRequestConfig {
protected getConfig(
sessionToken?: string,
formDataHeaders?: FormData.Headers,
): AxiosRequestConfig {
const config: AxiosRequestConfig = {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
}
'Access-Control-Allow-Origin': '*',
},
};

if (sessionToken && config.headers) {
config.headers.Authorization = `Bearer ${sessionToken}`
config.headers.Authorization = `Bearer ${sessionToken}`;
}

if (formDataHeaders && config.headers) {
config.headers = {
...config.headers,
...formDataHeaders
}
...formDataHeaders,
};
}

return config
return config;
}

/**
Expand All @@ -50,10 +53,13 @@ export class ApiServiceBase {
* @param configOverrides A config object to merge onto the base config.
* @protected
*/
protected async get<T>(path = '', configOverrides: AxiosRequestConfig): Promise<Result<T | null, ApiErrorBase>> {
protected async get<T>(
path = '',
configOverrides: AxiosRequestConfig,
): Promise<Result<T | null, ApiErrorBase>> {
return await this.requestResultWrapper<T>(path, configOverrides, (fullPath, config) => {
return axios.get(fullPath, config)
})
return axios.get(fullPath, config);
});
}

/**
Expand All @@ -66,11 +72,11 @@ export class ApiServiceBase {
protected async post<T>(
path = '',
data: unknown = undefined,
configOverrides: AxiosRequestConfig
configOverrides: AxiosRequestConfig,
): Promise<Result<T | null, ApiErrorBase>> {
return await this.requestResultWrapper<T>(path, configOverrides, (fullPath, config) => {
return axios.post(fullPath, data, config)
})
return axios.post(fullPath, data, config);
});
}

/**
Expand All @@ -83,11 +89,11 @@ export class ApiServiceBase {
protected async put<T>(
path = '',
data: unknown = undefined,
configOverrides: AxiosRequestConfig
configOverrides: AxiosRequestConfig,
): Promise<Result<T | null, ApiErrorBase>> {
return await this.requestResultWrapper<T>(path, configOverrides, (fullPath, config) => {
return axios.put(fullPath, data, config)
})
return axios.put(fullPath, data, config);
});
}

/**
Expand All @@ -100,11 +106,11 @@ export class ApiServiceBase {
protected async patch<T>(
path = '',
data: unknown = undefined,
configOverrides: AxiosRequestConfig
configOverrides: AxiosRequestConfig,
): Promise<Result<T | null, ApiErrorBase>> {
return await this.requestResultWrapper<T>(path, configOverrides, (fullPath, config) => {
return axios.patch(fullPath, data, config)
})
return axios.patch(fullPath, data, config);
});
}

/**
Expand All @@ -113,25 +119,38 @@ export class ApiServiceBase {
* @param configOverrides A config object to merge onto the base config.
* @protected
*/
protected async delete<T>(path = '', configOverrides: AxiosRequestConfig): Promise<Result<T | null, ApiErrorBase>> {
protected async delete<T>(
path = '',
configOverrides: AxiosRequestConfig,
): Promise<Result<T | null, ApiErrorBase>> {
return await this.requestResultWrapper<T>(path, configOverrides, (fullPath, config) => {
return axios.delete(fullPath, config)
})
return axios.delete(fullPath, config);
});
}

private async requestResultWrapper<T>(
subPath: string,
config: AxiosRequestConfig,
request: (fullPath: string, config: AxiosRequestConfig | undefined) => Promise<{ data: unknown } | null>
request: (
fullPath: string,
config: AxiosRequestConfig | undefined,
) => Promise<AxiosResponse<T>>,
): Promise<Result<T | null, ApiErrorBase>> {
if (subPath.length > 0 && subPath[0] !== '/') {
subPath = `/${subPath}`
subPath = `/${subPath}`;
}
try {
const responseData: T | null = ((await request(`${this.urlBase}${subPath}`, config))?.data as T) ?? null
return ok(responseData)
const response = (await request(`${this.urlBase}${subPath}`, config)) ?? null;
const status = response?.status;
const responseData = response.data as T;
return ok({
...responseData,
httpConfig: {
status,
},
});
} catch (e: unknown) {
return err(new ApiErrorBase(e))
return err(new ApiErrorBase(e));
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
"metabob.analyzeDocumentOnSave": {
"description": "Analyze Document On Save",
"type": "boolean",
"default": true,
"default": false,
"category": "Metabob"
},
"metabob.backendSelection": {
Expand Down

0 comments on commit 90f15e5

Please sign in to comment.