-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split creation of different trainspaces into different API endpoints
- Loading branch information
1 parent
57f1adc
commit 18df85b
Showing
3 changed files
with
68 additions
and
34 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
59 changes: 59 additions & 0 deletions
59
serverless/packages/functions/src/trainspace/create_tabular_trainspace.ts
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,59 @@ | ||
import { APIGatewayProxyHandlerV2 } from "aws-lambda"; | ||
import parseJwt from "@dlp-sst-app/core/parseJwt"; | ||
import TrainspaceData from './trainspace-data'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { DynamoDBClient, PutItemCommand } from '@aws-sdk/client-dynamodb'; | ||
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'; | ||
|
||
function validateRequestBody(eventBody: any, trainspace_id: string, uid: string) : TrainspaceData | null { | ||
return new TrainspaceData(trainspace_id, uid, "TABULAR", eventBody['default'], eventBody['name'], | ||
{ | ||
criterion: eventBody['criterion'], | ||
optimizer_name: eventBody['optimizer_name'], | ||
shuffle: eventBody['shuffle'], | ||
epochs: eventBody['epochs'], | ||
batch_size: eventBody['batch_size'], | ||
user_arch: eventBody['user_arch'] | ||
}, ""); | ||
} | ||
|
||
export const handler: APIGatewayProxyHandlerV2 = async (event) => { | ||
if (event) { | ||
const uid: string = parseJwt(event.headers.authorization ?? "")["user_id"]; | ||
const eventBody = JSON.parse(event.body? event.body : ""); | ||
|
||
const trainspaceId = uuidv4(); | ||
const trainspaceData = validateRequestBody(eventBody, trainspaceId, uid); | ||
|
||
if (trainspaceData == null) | ||
{ | ||
return { | ||
statusCode: 400, | ||
body: JSON.stringify({ message: "Invalid request body" }) | ||
} | ||
} | ||
|
||
|
||
const client = new DynamoDBClient({}); | ||
const docClient = DynamoDBDocumentClient.from(client); | ||
|
||
const command = new PutItemCommand(trainspaceData.convertToDynamoItemInput("trainspace")); | ||
|
||
const response = await docClient.send(command); | ||
if (response.$metadata.httpStatusCode != 200) { | ||
return { | ||
statusCode: 500, | ||
body: JSON.stringify({ message: "Internal server error."}) | ||
}; | ||
} | ||
|
||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ trainspaceId: trainspaceId, message: "Successfully created a new trainspace."}) | ||
}; | ||
} | ||
return { | ||
statusCode: 404, | ||
body: JSON.stringify({ message: "Not Found" }), | ||
}; | ||
}; |
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