Skip to content

Commit

Permalink
Merge pull request #13 from arati-tekdi/main
Browse files Browse the repository at this point in the history
Task #227823 Integration of sunbird API in middleware
  • Loading branch information
rushi-tekdi authored Sep 27, 2024
2 parents 0da4122 + f2c4026 commit 3f08584
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
27 changes: 17 additions & 10 deletions src/common/middleware/apiConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,11 +492,22 @@ export const apiList = {

//sunbird knowlg and inQuiry service
//public

'/api/question/v2/list': createRouteObject({ post: {} }, '/question/v5/list'),
'/action/questionset/v2/read/:identifier': createRouteObject(
{ get: {} },
'/questionset/v5/read/:identifier',
),
// added update one before any identifier
'/action/questionset/v2/hierarchy/update': createRouteObject(
{
patch: {
PRIVILEGE_CHECK: privilegeGroup.content.update,
ROLE_CHECK: rolesGroup.content_restricted,
},
},
'/questionset/v5/hierarchy/update',
),
'/action/questionset/v2/hierarchy/:identifier': createRouteObject(
{ get: {} },
'/questionset/v5/hierarchy/:identifier',
Expand All @@ -514,6 +525,10 @@ export const apiList = {
'/framework/v3/read/:identifier',
),
'/action/composite/v3/search': createRouteObject({ post: {} }, '/v3/search'),
'/action/object/category/definition/v1/read': createRouteObject(
{ post: {} },
'/object/category/definition/v4/read',
),
//secure
'/action/questionset/v2/create': createRouteObject(
{
Expand Down Expand Up @@ -560,15 +575,7 @@ export const apiList = {
},
'/questionset/v5/retire/:identifier',
),
'/action/questionset/v2/hierarchy/update': createRouteObject(
{
patch: {
PRIVILEGE_CHECK: privilegeGroup.content.update,
ROLE_CHECK: rolesGroup.content_restricted,
},
},
'/questionset/v5/hierarchy/update',
),

'/action/questionset/v2/reject/:identifier': createRouteObject(
{
post: {
Expand Down Expand Up @@ -614,7 +621,6 @@ export const apiList = {
},
}),
};
console.log('api list', JSON.stringify(apiList, null, 2));
export const urlPatterns = Object.keys(apiList);

//add public api
Expand All @@ -627,6 +633,7 @@ export const publicAPI = [
'/api/channel/v1/read/:identifier',
'/api/framework/v1/read/:identifier',
'/action/composite/v3/search',
'/action/object/category/definition/v1/read',
];

function convertToRegex(pattern) {
Expand Down
10 changes: 6 additions & 4 deletions src/common/middleware/middleware.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export class MiddlewareServices {
// check API is whitelisted
if (apiList[reqUrl]) {
if (!apiList[reqUrl][req.method.toLowerCase()]) {
console.log('not whitelist');
throw new HttpException(
'SHIKSHA_API_WHITELIST: URL not whitelisted',
HttpStatus.FORBIDDEN,
Expand All @@ -62,7 +61,6 @@ export class MiddlewareServices {
);
let checksToExecute = [];
// Iterate for checks defined for API and push to array
//console.log('req', req);
apiList[reqUrl][req.method.toLowerCase()].checksNeeded?.forEach(
(CHECK) => {
checksToExecute.push(
Expand Down Expand Up @@ -118,7 +116,8 @@ export class MiddlewareServices {
//replace forwardUrl if redirectUrl present
//check for dynamic url
const originalUrl = req.originalUrl;
let reqUrl = originalUrl.split('?')[0];
let temp = originalUrl.split('?');
let reqUrl = temp[0];
const withPattern = this.matchUrl(reqUrl);
reqUrl = withPattern || reqUrl;
if (apiList[reqUrl]?.redirectUrl) {
Expand All @@ -132,6 +131,9 @@ export class MiddlewareServices {
} else {
forwardUrl = apiList[reqUrl].redirectUrl;
}
if (temp[1]) {
forwardUrl = forwardUrl + '?' + temp[1];
}
}
const config = {
method: req.method,
Expand Down Expand Up @@ -164,6 +166,7 @@ export class MiddlewareServices {
'/api/channel': 'CONTENT_SERVICE',
'/api/framework': 'TAXONOMY_SERVICE',
'/action/composite': 'SEARCH_SERVICE',
'/action/object': 'TAXONOMY_SERVICE',
};

// Iterate over the mapping to find the correct service based on the URL prefix
Expand Down Expand Up @@ -324,7 +327,6 @@ export class MiddlewareServices {
try {
if (checksToExecute.length == 0) {
const response = await this.forwardRequest(req, res);
//console.log('response in middleware', response);
return res.json(response);
}
await Promise.allSettled(checksToExecute).then(
Expand Down
36 changes: 17 additions & 19 deletions src/middleware/gateway.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Injectable } from '@nestjs/common';
import axios from 'axios';
import { MiddlewareLogger } from 'src/common/loggers/logger.service';


@Injectable()
export class GatewayService {
constructor(private readonly middlewareLogger: MiddlewareLogger) {}
Expand All @@ -12,34 +11,33 @@ export class GatewayService {
url: string,
body: Object,
oheaders: any,
){
) {
let newheaders = {
tenantId: oheaders['tenantid'],
'content-type': 'application/json',
authorization: oheaders['authorization']
}
authorization: oheaders['authorization'],
};
try {
const response = await axios(
{
method,
url,
data :body,
headers: newheaders
});
return response.data
const response = await axios({
method,
url,
data: body,
headers: newheaders,
});
return response.data;
} catch (error) {
if (error.response) {
return error.response.data;
} else if (error.request) {
// No response was received
return {
result : {},
params : {
"err": "Internal server error",
"errmsg": "Internal server error",
"status": "failed"
}
}
result: {},
params: {
err: 'Internal server error',
errmsg: 'Internal server error',
status: 'failed',
},
};
} else {
// Error occurred in setting up the request
return error.message;
Expand Down

0 comments on commit 3f08584

Please sign in to comment.