-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(connectors): add endpoints for resources titles and parents (#1557)
* feat(connectors): add endpoint to return resources detail * plumbing * switch to post + add front binding
- Loading branch information
1 parent
fa56b07
commit 656ed33
Showing
12 changed files
with
455 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { Request, Response } from "express"; | ||
import { zip } from "fp-ts/lib/Array"; | ||
import { isLeft } from "fp-ts/lib/Either"; | ||
import * as t from "io-ts"; | ||
import * as reporter from "io-ts-reporters"; | ||
|
||
import { RETRIEVE_RESOURCE_PARENTS_BY_TYPE } from "@connectors/connectors"; | ||
import { Connector } from "@connectors/lib/models"; | ||
import logger from "@connectors/logger/logger"; | ||
import { apiError, withLogging } from "@connectors/logger/withlogging"; | ||
|
||
const GetResourcesParentsRequestBodySchema = t.type({ | ||
resourceInternalIds: t.array(t.string), | ||
}); | ||
|
||
export type GetResourcesParentsRequestBody = t.TypeOf< | ||
typeof GetResourcesParentsRequestBodySchema | ||
>; | ||
|
||
type GetResourcesParentsResponseBody = { | ||
resources: { | ||
internalId: string; | ||
parents: string[] | null; | ||
}[]; | ||
}; | ||
|
||
const _getResourcesParents = async ( | ||
req: Request< | ||
{ connector_id: string }, | ||
GetResourcesParentsResponseBody, | ||
GetResourcesParentsRequestBody | ||
>, | ||
res: Response<GetResourcesParentsResponseBody> | ||
) => { | ||
const connector = await Connector.findByPk(req.params.connector_id); | ||
if (!connector) { | ||
return apiError(req, res, { | ||
api_error: { | ||
type: "connector_not_found", | ||
message: "Connector not found", | ||
}, | ||
status_code: 404, | ||
}); | ||
} | ||
|
||
const bodyValidation = GetResourcesParentsRequestBodySchema.decode(req.body); | ||
if (isLeft(bodyValidation)) { | ||
const pathError = reporter.formatValidationErrors(bodyValidation.left); | ||
return apiError(req, res, { | ||
api_error: { | ||
type: "invalid_request_error", | ||
message: `Invalid request body: ${pathError}`, | ||
}, | ||
status_code: 400, | ||
}); | ||
} | ||
|
||
const { resourceInternalIds } = bodyValidation.right; | ||
|
||
const parentsGetter = RETRIEVE_RESOURCE_PARENTS_BY_TYPE[connector.type]; | ||
const parentsResults = await Promise.all( | ||
resourceInternalIds.map((resourceInternalId) => | ||
parentsGetter(connector.id, resourceInternalId) | ||
) | ||
); | ||
const resources: { internalId: string; parents: string[] }[] = []; | ||
|
||
for (const [internalId, parentsResult] of zip( | ||
resourceInternalIds, | ||
parentsResults | ||
)) { | ||
if (parentsResult.isErr()) { | ||
logger.error(parentsResult.error, "Failed to get resource parents"); | ||
return apiError(req, res, { | ||
api_error: { | ||
type: "internal_server_error", | ||
message: parentsResult.error.message, | ||
}, | ||
status_code: 500, | ||
}); | ||
} | ||
|
||
resources.push({ | ||
internalId, | ||
parents: parentsResult.value, | ||
}); | ||
} | ||
|
||
return res.status(200).json({ resources }); | ||
}; | ||
|
||
export const getResourcesParentsAPIHandler = withLogging(_getResourcesParents); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { Request, Response } from "express"; | ||
import { isLeft } from "fp-ts/lib/Either"; | ||
import * as t from "io-ts"; | ||
import * as reporter from "io-ts-reporters"; | ||
|
||
import { BATCH_RETRIEVE_RESOURCE_TITLE_BY_TYPE } from "@connectors/connectors"; | ||
import { Connector } from "@connectors/lib/models"; | ||
import { Result } from "@connectors/lib/result"; | ||
import { apiError, withLogging } from "@connectors/logger/withlogging"; | ||
|
||
const GetResourcesTitlesRequestBodySchema = t.type({ | ||
resourceInternalIds: t.array(t.string), | ||
}); | ||
type GetResourcesTitlesRequestBody = t.TypeOf< | ||
typeof GetResourcesTitlesRequestBodySchema | ||
>; | ||
|
||
type GetResourcesTitlesResponseBody = { | ||
resources: { | ||
internalId: string; | ||
title: string | null; | ||
}[]; | ||
}; | ||
|
||
const _getResourcesTitles = async ( | ||
req: Request< | ||
{ connector_id: string }, | ||
GetResourcesTitlesResponseBody, | ||
GetResourcesTitlesRequestBody | ||
>, | ||
res: Response<GetResourcesTitlesResponseBody> | ||
) => { | ||
const connector = await Connector.findByPk(req.params.connector_id); | ||
if (!connector) { | ||
return apiError(req, res, { | ||
api_error: { | ||
type: "connector_not_found", | ||
message: "Connector not found", | ||
}, | ||
status_code: 404, | ||
}); | ||
} | ||
|
||
const bodyValidation = GetResourcesTitlesRequestBodySchema.decode(req.body); | ||
if (isLeft(bodyValidation)) { | ||
const pathError = reporter.formatValidationErrors(bodyValidation.left); | ||
return apiError(req, res, { | ||
api_error: { | ||
type: "invalid_request_error", | ||
message: `Invalid request body: ${pathError}`, | ||
}, | ||
status_code: 400, | ||
}); | ||
} | ||
|
||
const { resourceInternalIds } = bodyValidation.right; | ||
|
||
const titlesRes: Result< | ||
Record<string, string | null>, | ||
Error | ||
> = await BATCH_RETRIEVE_RESOURCE_TITLE_BY_TYPE[connector.type]( | ||
connector.id, | ||
resourceInternalIds | ||
); | ||
|
||
if (titlesRes.isErr()) { | ||
return apiError(req, res, { | ||
api_error: { | ||
type: "internal_server_error", | ||
message: titlesRes.error.message, | ||
}, | ||
status_code: 500, | ||
}); | ||
} | ||
|
||
const titles = titlesRes.value; | ||
|
||
return res.status(200).json({ | ||
resources: resourceInternalIds.map((internalId) => ({ | ||
internalId, | ||
title: titles[internalId] || null, | ||
})), | ||
}); | ||
}; | ||
|
||
export const getResourcesTitlesAPIHandler = withLogging(_getResourcesTitles); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.