Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make soracom-image-source that can handle upload path upon webhook request #6

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ A source webhook which uploads SorCam image to Soracom Harvest Files.
curl \
-XGET \
-H "x-api-key:${apikey}" \
"https://${hostname}/v1/source/soracam_image?device_id=${device_id}"
"https://${hostname}/v1/source/soracam_image?device_id=${device_id}&upload_directory=${directory}"
```

Query parameters
* device_id: Soracam ID
* upload_directory: Upload target directory under base path.

You will need SAM user with permissions below

Expand Down
11 changes: 8 additions & 3 deletions lambda/soracam-image-source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,17 @@ describe("Lambda handler", () => {

expect(result.statusCode).toBe(400);
expect(JSON.parse(result.body).message).toBe(
"device is required in querystring parameters"
"device_id and upload_directory are required in querystring parameters"
);
});

it("should successfully process the request", async () => {
const event = { queryStringParameters: { device_id: "testDevice" } };
const event = {
queryStringParameters: {
device_id: "testDevice",
upload_directory: "testDirectory",
},
};

// Mock the Secrets Manager response
secretsManagerMock.on(GetSecretValueCommand).resolves({
Expand Down Expand Up @@ -87,7 +92,7 @@ describe("Lambda handler", () => {

soracomHttpClientMock
.onPut(
/https:\/\/api.soracom.io\/v1\/files\/private\/test\/path\/testDevice-\d{14}.jpg/
/https:\/\/api.soracom.io\/v1\/files\/private\/test\/path\/testDirectory\/testDevice-\d{14}.jpg/
)
.reply(201);

Expand Down
13 changes: 9 additions & 4 deletions lambda/soracam-image-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ import { getSoracomClient, setGetSoracomClient } from "./lib/utils";
export { setGetSoracomClient };

export const handler = async (event: any = {}): Promise<any> => {
if (!event.queryStringParameters || !event.queryStringParameters.device_id) {
if (
!event.queryStringParameters ||
!event.queryStringParameters.device_id ||
!event.queryStringParameters.upload_directory
) {
return {
statusCode: 400,
body: JSON.stringify({
message: "device is required in querystring parameters",
message:
"device_id and upload_directory are required in querystring parameters",
}),
};
}

const harvestfilesPath = process.env.HARVEST_FILES_PATH!;

const deviceId = event.queryStringParameters.device_id;
const uploadDirectory = event.queryStringParameters.upload_directory;
const soracomClient = await getSoracomClient();

const time = Date.now() - 1000 * 10;
Expand All @@ -42,7 +47,7 @@ export const handler = async (event: any = {}): Promise<any> => {
}
await new Promise((resolve) => setTimeout(resolve, 1000));
}
let path = `${harvestfilesPath}/${deviceId}-${dayjs().format(
let path = `${harvestfilesPath}/${uploadDirectory}/${deviceId}-${dayjs().format(
"YYYYMMDDHHmmss"
)}.jpg`;

Expand Down