Skip to content

Commit 3b9dcb9

Browse files
committed
Change bindings model to v1
1 parent 38e775d commit 3b9dcb9

File tree

6 files changed

+136
-111
lines changed

6 files changed

+136
-111
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import logging
2+
import azure.functions as func
3+
import json
4+
import pandas as pd
5+
from io import StringIO
6+
from sklearn.preprocessing import LabelEncoder
7+
8+
9+
def main(inbound: func.InputStream, outbound: func.Out[str]):
10+
try:
11+
logging.info("Triggered blob function with blob: %s", inbound.name)
12+
13+
# Read CSV content from the blob
14+
csv_content = inbound.read().decode("utf-8")
15+
logging.info("Read CSV content from blob successfully")
16+
17+
# Convert CSV content to a pandas DataFrame
18+
df = pd.read_csv(StringIO(csv_content))
19+
logging.info("Converted CSV content to DataFrame")
20+
21+
# Label encode 'Gender' and 'State' columns
22+
label_encoder = LabelEncoder()
23+
df['Gender'] = label_encoder.fit_transform(df['Gender'])
24+
df['State'] = label_encoder.fit_transform(df['State'])
25+
logging.info("Label encoded 'Gender' and 'State' columns")
26+
27+
# Calculate correlations
28+
gender_to_income_corr = df[['Gender', 'Income']].corr().iloc[0, 1]
29+
experience_to_income_corr = df[['Experience', 'Income']].corr().iloc[0, 1]
30+
state_to_income_corr = df[['State', 'Income']].corr().iloc[0, 1]
31+
logging.info("Calculated correlations")
32+
33+
# Create statistics dictionary
34+
statistics = {
35+
"gender_to_income_corr": gender_to_income_corr,
36+
"experience_to_income_corr": experience_to_income_corr,
37+
"state_to_income_corr": state_to_income_corr
38+
}
39+
logging.info("Created statistics dictionary: %s", statistics)
40+
41+
# Convert statistics to JSON format
42+
statistics_json = json.dumps(statistics, indent=2)
43+
logging.info("Converted statistics to JSON format")
44+
45+
# Upload statistics JSON file to storage account container blob
46+
outbound.set(statistics_json)
47+
logging.info("File 'statistics.json' was uploaded")
48+
49+
except Exception as e:
50+
logging.error("An error occurred: %s", str(e))
51+
return func.HttpResponse(f"Error: {str(e)}", status_code=500)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"bindings": [
3+
{
4+
"type": "blobTrigger",
5+
"direction": "in",
6+
"name": "inbound",
7+
"path": "hvalfangstcontainer/in/input.csv",
8+
"connection": "AzureWebJobsStorage"
9+
},
10+
{
11+
"type": "blob",
12+
"direction": "out",
13+
"name": "outbound",
14+
"path": "hvalfangstcontainer/out/statistics.json",
15+
"connection": "AzureWebJobsStorage"
16+
}
17+
]
18+
}

hvalfangst_function/function_app.py

Lines changed: 0 additions & 107 deletions
This file was deleted.

hvalfangst_function/host.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,5 @@
88
"excludedTypes": "Request"
99
}
1010
}
11-
},
12-
"extensionBundle": {
13-
"id": "Microsoft.Azure.Functions.ExtensionBundle",
14-
"version": "[4.*, 5.0.0)"
1511
}
1612
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import logging
2+
import azure.functions as func
3+
import jwt
4+
import os
5+
6+
7+
def main(req: func.HttpRequest, outbound: func.Out[str]) -> func.HttpResponse:
8+
try:
9+
logging.info("Received HTTP request to upload CSV")
10+
11+
# Validate JWT token
12+
auth_header = req.headers.get("Authorization")
13+
14+
if not auth_header:
15+
return func.HttpResponse("Missing auth header", status_code=401)
16+
17+
token = auth_header.split(" ")[1] # Extract Bearer token
18+
if not validate_jwt(token, audience=os.environ.get("FUNCTION_APP_CLIENT_ID")):
19+
return func.HttpResponse("Unauthorized", status_code=401)
20+
21+
logging.info("Received HTTP request to upload CSV")
22+
23+
# Parse raw bytes derived from request body to string
24+
string_body = req.get_body().decode("utf-8")
25+
logging.info("Parsed request body to string")
26+
27+
# Upload parsed string body, which conforms to CSV format
28+
outbound.set(string_body)
29+
logging.info("Successfully uploaded CSV content")
30+
return func.HttpResponse("Successfully uploaded CSV content", status_code=200)
31+
32+
except Exception as e:
33+
logging.error("An error occurred: %s", str(e))
34+
return func.HttpResponse(f"Error: {str(e)}", status_code=500)
35+
36+
37+
def validate_jwt(token: str, audience: str) -> bool:
38+
try:
39+
decoded = jwt.decode(token, audience=audience, options={"verify_signature": False})
40+
# Optionally check claims like roles or scopes
41+
return True
42+
except Exception as e:
43+
logging.error(f"JWT validation failed: {e}")
44+
return False
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"bindings": [
3+
{
4+
"authLevel": "anonymous",
5+
"type": "httpTrigger",
6+
"direction": "in",
7+
"name": "req",
8+
"route": "upload_csv"
9+
},
10+
{
11+
"type": "http",
12+
"direction": "out",
13+
"name": "$return"
14+
},
15+
{
16+
"type": "blob",
17+
"direction": "out",
18+
"name": "outbound",
19+
"path": "hvalfangstcontainer/in/input.csv",
20+
"connection": "AzureWebJobsStorage"
21+
}
22+
]
23+
}

0 commit comments

Comments
 (0)