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

Pass dapr api token #598

Merged
merged 5 commits into from Sep 25, 2023
Merged

Pass dapr api token #598

merged 5 commits into from Sep 25, 2023

Conversation

DeepanshuA
Copy link
Contributor

@DeepanshuA DeepanshuA commented Sep 13, 2023

Description

microsoft/durabletask-python#16 is merged. So, updating this PR.

This change enables user to pass an API token to WorkflowRuntime and DaprWorkflowClient.
If user doesn't pass explicitly and API Token and environment variable is set for DAPR_API_TOKEN, it will be picked.

Issue reference

We strive to have all PR being opened based on an issue, where the problem or feature have been discussed prior to implementation.

Please reference the issue this PR will close: #594

Checklist

Please make sure you've completed the relevant tasks for this PR, out of the following list:

  • Code compiles correctly
  • Created/updated tests
  • Extended the documentation

Signed-off-by: Deepanshu Agarwal <deepanshu.agarwal1984@gmail.com>
@DeepanshuA DeepanshuA requested review from a team as code owners September 13, 2023 14:57
@RyanLettieri
Copy link
Contributor

Great changes! The only question I have is if/how these changes were tested and verified and if it is possible to create a test (or modify an existing one) in order to ensure that this new behavior doesn't break in the future.

address = getAddress(host, port)
self.__obj = client.TaskHubGrpcClient(host_address=address)
metadata = []
if apiToken is not None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[not blocking] Are you sure you even need the API token as parameter? I'd rather refer people to using the settings package to set the token:

from dapr.conf import settings
settings.DAPR_API_TOKEN='mytokenhere'

And as you know that is only necessary if the token itself isn't already set as an environment variable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can keep the parameter if you'd like to have it for consistency with other SDKs - but if other SDKs do not use this, then I recommend also removing it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed api_token option. It will pick only if corresponding env. setting is there.
python DaprClient also doing the same thing - so to maintain consistency.

@@ -17,6 +17,7 @@

from dapr.conf import settings

DAPR_API_TOKEN_HEADER = 'dapr-api-token'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please import this instead so we can change everything in one place if it ever must change.

from dapr.ext.workflow.util import DAPR_API_TOKEN_HEADER

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, used from original place only now.

Copy link
Member

@berndverst berndverst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some feedback - 2 small questions, but you do need to use snake case instead of camelcase :)

@@ -37,9 +38,14 @@ class DaprWorkflowClient:
This client is intended to be used by workflow application, not by general purpose
application.
"""
def __init__(self, host: Optional[str] = None, port: Optional[str] = None):
def __init__(self, host: Optional[str] = None, port: Optional[str] = None, apiToken: Optional[str] = None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def __init__(self, host: Optional[str] = None, port: Optional[str] = None, apiToken: Optional[str] = None):
def __init__(self, host: Optional[str] = None, port: Optional[str] = None, api_token: Optional[str] = None):

Python requires snake case instead of camelcase per style guide

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the option.

@@ -31,10 +32,15 @@ class WorkflowRuntime:
"""WorkflowRuntime is the entry point for registering workflows and activities.
"""

def __init__(self, host: Optional[str] = None, port: Optional[str] = None):
def __init__(self, host: Optional[str] = None, port: Optional[str] = None, apiToken: Optional[str] = None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this snake case. api_token

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the option.

def __init__(self, host: Optional[str] = None, port: Optional[str] = None):
def __init__(self, host: Optional[str] = None, port: Optional[str] = None, apiToken: Optional[str] = None):
metadata = []
if apiToken is not None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same feedback [not blocking] - do you really need this in the method signature?

In Python we really want folks to use our settings package to centrally set these things (if they are not already provided via environment variables)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, removed the option

@berndverst
Copy link
Member

Please run the linter and typecheck locally before updating this PR and fix issues.

tox -e flake8
tox -e type

Current issues for lint:

./ext/dapr-ext-workflow/dapr/ext/workflow/dapr_workflow_client.py:41:101: E501 line too long (111 > 100 characters)
./ext/dapr-ext-workflow/dapr/ext/workflow/util.py:22:1: E302 expected 2 blank lines, found 1
./ext/dapr-ext-workflow/dapr/ext/workflow/workflow_runtime.py:16:1: F401 'typing.Any' imported but unused
./ext/dapr-ext-workflow/dapr/ext/workflow/workflow_runtime.py:16:1: F401 'typing.Dict' imported but unused
./ext/dapr-ext-workflow/dapr/ext/workflow/workflow_runtime.py:35:101: E501 line too long (111 > 100 characters)
./ext/dapr-ext-workflow/dapr/ext/workflow/workflow_runtime.py:44:5: E301 expected 1 blank line, found 0

@berndverst
Copy link
Member

Great changes! The only question I have is if/how these changes were tested and verified and if it is possible to create a test (or modify an existing one) in order to ensure that this new behavior doesn't break in the future.

@RyanLettieri I do not think we should modify the examples - however, if you'd like a unit test / mock could be added here.

To me this is not a blocking issue because this code is very straightforward and in line with what we have already in the SDK.

Copy link
Member

@berndverst berndverst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The workflow example also currently appears broken

		== APP == Traceback (most recent call last):
		== APP ==   File "/home/runner/work/python-sdk/python-sdk/examples/demo_workflow/app.py", line 113, in <module>
		== APP ==     main()
		== APP ==   File "/home/runner/work/python-sdk/python-sdk/examples/demo_workflow/app.py", line 49, in main
		== APP ==     workflowRuntime = WorkflowRuntime(host, port)
		== APP ==   File "/home/runner/work/python-sdk/python-sdk/ext/dapr-ext-workflow/dapr/ext/workflow/workflow_runtime.py", line 43, in __init__
		== APP ==     self.__worker = worker.TaskHubGrpcWorker(host_address=address, metadata=metadata)
		== APP == TypeError: __init__() got an unexpected keyword argument 'metadata'

This is likely caused by the fact you made metadata a list now instead of a tuple. See comment above.

@berndverst
Copy link
Member

@DeepanshuA was there no update to durable-task-python required to make this work? Otherwise there should be a change to the requirements.txt / setup.cfg.

@berndverst
Copy link
Member

@DeepanshuA @RyanLettieri this PR cannot work right now. This needs to be done:

@DeepanshuA
Copy link
Contributor Author

@DeepanshuA @RyanLettieri this PR cannot work right now. This needs to be done:

Yeah @berndverst.
I forgot to update about this in the description of this PR.

@DeepanshuA DeepanshuA changed the title Pass dapr api token [DEPENDENCY_AWAITED] Pass dapr api token Sep 19, 2023
Signed-off-by: Deepanshu Agarwal <deepanshu.agarwal1984@gmail.com>
@berndverst
Copy link
Member

@DeepanshuA now that microsoft/durabletask-python#16 is merged, please cut a release and complete this PR so that this will not delay the Python SDK release. Thanks!

Signed-off-by: Deepanshu Agarwal <deepanshu.agarwal1984@gmail.com>
@codecov
Copy link

codecov bot commented Sep 21, 2023

Codecov Report

Patch coverage: 83.33% and project coverage change: -0.02% ⚠️

Comparison is base (f9eedff) 86.42% compared to head (7fbaec7) 86.40%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #598      +/-   ##
==========================================
- Coverage   86.42%   86.40%   -0.02%     
==========================================
  Files          74       74              
  Lines        3595     3605      +10     
==========================================
+ Hits         3107     3115       +8     
- Misses        488      490       +2     
Files Changed Coverage Δ
dapr/clients/grpc/_helpers.py 89.85% <ø> (ø)
...workflow/dapr/ext/workflow/dapr_workflow_client.py 97.43% <83.33%> (-2.57%) ⬇️
...ext-workflow/dapr/ext/workflow/workflow_runtime.py 70.27% <83.33%> (+1.52%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@DeepanshuA DeepanshuA changed the title [DEPENDENCY_AWAITED] Pass dapr api token Pass dapr api token Sep 21, 2023
@berndverst berndverst merged commit 010a5b3 into dapr:master Sep 25, 2023
6 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for DAPR_API_TOKEN in the Workflow SDK
3 participants