-
Notifications
You must be signed in to change notification settings - Fork 27
Feature/task12 azure function usersId #49
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
Feature/task12 azure function usersId #49
Conversation
file is not needed
can be deleted
can be deleted
remove functions that are not associated with HttpTriggerAPIUsersId
remove functions that are not associated with HttpTriggerAPIUsersId
remove functions that are not associated with HttpTriggerAPIUsersId
removed .vscode files
removed .vscode files
removed .vscode files
removed .vscode files
remove files that should not be included
remove files that should not be included
remove files that should not be included
remove files that should not be included
remove files that should not be included
remove files that should not be included
…12-azure-function-usersId
import azure.functions as func | ||
import json | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would love to see some comments in this file. I think it would definitely be helpful in identifying what's going on at the different stages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will add comments!
connectionString = "Driver={};Server={};Database={};Uid={};Pwd={};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;".format( | ||
driver, db_server, db_name, db_username, db_password) | ||
|
||
id = req.params.get('id') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this id for? Whose id is it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lbrown51 this id is the id that gets passed as a parameter when this function gets called.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the userId.
Natalia suggested renaming this "id" to "userId" for clarification. The way I have it right now makes it confusing. Will update it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! I think that makes a lot of sense. Currently, it could be misinterpreted as a separate id of some kind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! good practice to keep names for the parameters same across the board. I used userId and taskId in my code
else: | ||
try: | ||
with pypyodbc.connect(connectionString) as conn: | ||
return execute_query(conn, id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it would make sense, instead of having "execute_query" to name the function something like "get_user_by_id"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For naming conventions, I would change it to your suggestion. Will update it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used a generic name here, so that it could be repurposed for different methods (get, update, post, delete)
connectionString = "Driver={};Server={};Database={};Uid={};Pwd={};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;".format( | ||
driver, db_server, db_name, db_username, db_password) | ||
|
||
id = req.params.get('id') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lbrown51 this id is the id that gets passed as a parameter when this function gets called.
I tested the functionality via the test tool on Azure, works as expected. Overall, it'd be great to see more comments to help those who continue to develop the API down the road. |
# https://stackoverflow.com/questions/16519385/output-pyodbc-cursor-results-as-python-dictionary/16523148#16523148 | ||
columns = [column[0] for column in cursor.description] | ||
data = dict(zip(columns, row)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some of the sources I looked into mention closing the connection after you open, such as cursor.close() and conn.close()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the comment did not get attached to the correct line. connection needs to be closed after the query executes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see inline comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything looks great. All my comments were addressed :)
Task: #12
EXPECTED RESULTS FOR API ENDPOINT USERS/{userId}
HTTP request for endpoint
users/userid:
Note when changing the id in the URL you will retrieve a different user.
This will show the users that are added to the DB and it is deployed to Azure Functions.
Test Function in Azure
Sign in to Azure.
Find nsc-function-team1 resource group
Select nsc-functionapp-team1
In the Navbar on th left select functions and click on HttpTriggerAPIUserId
Click on Code + Test on left nav bar
Click on function URL and test the function in the URL
It will return json {}
If it is not working on the first try wait for 30 seconds and try again

Test: users/1
Create and deploy Azure Functions from Visual Studio Code:
Prerequisites:
Create or use existing Azure Subscription
Install software to local workstation:
- Install the Azure Functions Core Tools in Visual Studio Code
- Install Python and Visual Studio Code with the Azure Function extension.
- Sign in to Azure.
- Verify your environment:
NOTE: Azure function only work with Python version 3.6, 3.7 and 3.8.
Reference: https://docs.microsoft.com/en-us/azure/developer/python/tutorial-vs-code-serverless-python-01
Reference: https://docs.microsoft.com/en-us/azure/developer/python/tutorial-vs-code-serverless-python-02
Note: When creating a HttpTriger in Visual Studio Code open the function folder in a separate window in Visual Studio Code and create your function in there. This will help to structure your Triggers and keep files in the same folder instead of in the root folder
3. Examine the Python code files in vscode:
Reference:
4. Debug the Azure Functions Python Code locally:
Reference:
5. Deploy Azure Function in Python
Reference: https://docs.microsoft.com/en-us/azure/developer/python/tutorial-vs-code-serverless-python-05
Purpose: This feature implemented the API endpoint for getting a specific user by userId.
TEST PULL REQUEST:
Requirements to set up Azure Function and Deploy them to Azure:
Install Azure Functions extension in vscode.
Open the functions folder separately in vscode.
Note: if you have the root repository folder open the azure.function extension will not work.
Install Python version 3.8 if you haven't yet
Note: Azure function does not work with Python 3.9 See documentation: https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-python
Set up environment in vscode/ .venv
Sign in to Azure
Deploy your functions once you completed all functionality and tested it locally.

To test it locally and Debug Functions:
Azure Function Setup:
local packages to install:**
Setup local.settings.json
-Access to Azure resources needed to access passwords
resource group: nsc-functions-team1
Verify that your local.settings.json file contains the following:
"FUNCTIONS_WORKER_RUNTIME": "addPasswordHere"
"ENV_DATABASE_USERNAME": "addPasswordHere"
"ENV_DATABASE_PASSWORD": "addPasswordHere"
"ENV_DATABASE_NAME": "addPasswordHere"
"ENV_DATABASE_SERVER": "addPasswordHere"
Troubleshoot ODBC Driver
Reference:
Need Apple Silicon / M1 builds mkleehammer/pyodbc#846
https://pypi.org/project/pyodbc/#files
https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/install-microsoft-odbc-driver-sql-server-macos?view=sql-server-ver15