Skip to content

Commit

Permalink
feat: upgrade Azure Functions to v4 for graph-connector-app (#1287)
Browse files Browse the repository at this point in the history
* feat: upgrade Azure Functions to v4 for graph-connector-app

* feat: add local.settings.json
  • Loading branch information
formulahendry authored Jun 11, 2024
1 parent d1cea8b commit b153bc3
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 229 deletions.
25 changes: 0 additions & 25 deletions graph-connector-app/api/connection/function.json

This file was deleted.

25 changes: 0 additions & 25 deletions graph-connector-app/api/data/function.json

This file was deleted.

11 changes: 0 additions & 11 deletions graph-connector-app/api/extensions.csproj

This file was deleted.

6 changes: 5 additions & 1 deletion graph-connector-app/api/host.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
}
}
}
6 changes: 6 additions & 0 deletions graph-connector-app/api/local.settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "node"
}
}
7 changes: 4 additions & 3 deletions graph-connector-app/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "teamsfx-template-api",
"version": "1.0.0",
"engines": {
"node": "16 || 18"
"node": "18"
},
"scripts": {
"dev:teamsfx": "env-cmd --silent -f .localConfigs npm run dev",
Expand All @@ -14,7 +14,7 @@
"start": "npx func start"
},
"dependencies": {
"@azure/functions": "^1.2.2",
"@azure/functions": "^4.2.0",
"@microsoft/microsoft-graph-client": "^3.0.0",
"@microsoft/teamsfx": "^2.0.0",
"csv-parse": "^5.0.4",
Expand All @@ -24,5 +24,6 @@
"@types/node": "^18.7.18",
"env-cmd": "^10.1.0",
"typescript": "^4.4.4"
}
},
"main": "dist/src/functions/*.js"
}
4 changes: 0 additions & 4 deletions graph-connector-app/api/proxies.json

This file was deleted.

25 changes: 0 additions & 25 deletions graph-connector-app/api/schema/function.json

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,43 +1,34 @@
// Import polyfills for fetch required by msgraph-sdk-javascript.
import "isomorphic-fetch";
import { Context, HttpRequest } from "@azure/functions";
import { app, InvocationContext, HttpRequest, HttpResponseInit } from "@azure/functions";
import { Client } from "@microsoft/microsoft-graph-client";
import { TokenCredentialAuthenticationProvider } from "@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials";
import { AppCredential, AppCredentialAuthConfig } from "@microsoft/teamsfx";
import config from "../config";

interface Response {
status: number;
body: { [key: string]: any };
}

const authConfig: AppCredentialAuthConfig = {
authorityHost: config.authorityHost,
clientId: config.clientId,
tenantId: config.tenantId,
clientSecret: config.clientSecret,
};

type TeamsfxContext = { [key: string]: any };

/**
* @param {Context} context - The Azure Functions context object.
* @param {HttpRequest} req - The HTTP request.
* @param {teamsfxContext} TeamsfxContext - The context generated by teamsfx binding.
* @param {InvocationContext} context - The Azure Functions context object.
*/
export default async function run(
context: Context,
export async function connection(
req: HttpRequest,
teamsfxContext: TeamsfxContext
): Promise<Response> {
context: InvocationContext
): Promise<HttpResponseInit> {
context.log("HTTP trigger function processed a request.");

const connectionId = req.query.connectionId;
const connectionId = req.query.get("connectionId");

// Initialize response.
const res: Response = {
const res: HttpResponseInit = {
status: 200,
body: {
jsonBody: {
connectionAlreadyExists: false,
},
};
Expand All @@ -46,10 +37,10 @@ export default async function run(
try {
appCredential = new AppCredential(authConfig);
} catch (e) {
context.log.error(e);
context.error(e);
return {
status: 500,
body: {
jsonBody: {
error:
"Failed to construct TeamsFx with Application Identity. " +
"Ensure your function app is configured with the right Azure AD App registration.",
Expand All @@ -76,9 +67,9 @@ export default async function run(
});
} catch (e) {
if (e?.statusCode === 409) {
res.body.connectionAlreadyExists = true;
res.jsonBody.connectionAlreadyExists = true;
} else {
context.log.error(e);
context.error(e);
let error =
"Failed to create a connection for Graph connector: " + e.toString();
if (e?.statusCode === 401) {
Expand All @@ -87,7 +78,7 @@ export default async function run(
}
return {
status: e?.statusCode ?? 500,
body: {
jsonBody: {
error,
},
};
Expand All @@ -96,3 +87,9 @@ export default async function run(

return res;
}

app.http("connection", {
methods: ["POST"],
authLevel: "anonymous",
handler: connection,
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Import polyfills for fetch required by msgraph-sdk-javascript.
import "isomorphic-fetch";
import { Context, HttpRequest } from "@azure/functions";
import { app, InvocationContext, HttpRequest, HttpResponseInit } from "@azure/functions";
import { Client } from "@microsoft/microsoft-graph-client";
import { TokenCredentialAuthenticationProvider } from "@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials";
import { AppCredential, AppCredentialAuthConfig } from "@microsoft/teamsfx";
Expand All @@ -9,10 +9,6 @@ import * as path from "path";
import { parse } from "csv-parse/sync";
import config from "../config";

interface Response {
status: number;
body: { [key: string]: any };
}

const authConfig: AppCredentialAuthConfig = {
authorityHost: config.authorityHost,
Expand All @@ -21,36 +17,32 @@ const authConfig: AppCredentialAuthConfig = {
clientSecret: config.clientSecret,
};

type TeamsfxContext = { [key: string]: any };

/**
* @param {Context} context - The Azure Functions context object.
* @param {HttpRequest} req - The HTTP request.
* @param {teamsfxContext} TeamsfxContext - The context generated by teamsfx binding.
* @param {InvocationContext} context - The Azure Functions context object.
*/
export default async function run(
context: Context,
export async function data(
req: HttpRequest,
teamsfxContext: TeamsfxContext
): Promise<Response> {
context: InvocationContext
): Promise<HttpResponseInit> {
context.log("HTTP trigger function processed a request.");

const connectionId = req.query.connectionId;
const connectionId = req.query.get("connectionId");

// Initialize response.
const res: Response = {
const res: HttpResponseInit = {
status: 200,
body: {},
jsonBody: {},
};

let appCredential;
try {
appCredential = new AppCredential(authConfig);
} catch (e) {
context.log.error(e);
context.error(e);
return {
status: 500,
body: {
jsonBody: {
error:
"Failed to construct AppCredential with Application Identity. " +
"Ensure your function app is configured with the right Azure AD App registration.",
Expand All @@ -63,7 +55,6 @@ export default async function run(
const csvFileContent = (
await readFile(
path.join(
context.executionContext.functionDirectory,
"assets",
"ApplianceParts.csv"
)
Expand Down Expand Up @@ -110,14 +101,20 @@ export default async function run(
});
}
} catch (e) {
context.log.error(e);
context.error(e);
return {
status: e?.statusCode ?? 500,
body: {
jsonBody: {
error: "Failed to ingest items: " + e.toString(),
},
};
}

return res;
}

app.http("data", {
methods: ["POST"],
authLevel: "anonymous",
handler: data,
});
Loading

0 comments on commit b153bc3

Please sign in to comment.