-
Notifications
You must be signed in to change notification settings - Fork 8
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
APIEndpointAsset model #165
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
715fb8e
clean up asset attributes for display
ed05a0c
add key-value pair widget
306ed20
use new icons
1c50c84
add module level docstring
cd30bae
add body field to asset, fix issue with multiple rows added per click…
dfbaed8
use default key-value pair for body
f79bccd
use square brackets because mypy
5c4147d
update key-value widget and default key in api endpoint model's body
7b58b65
add test of apiendpointassetform
1086f3d
don't allow user to remove body field containing %query% value, and d…
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Generated by Django 4.2.3 on 2023-08-21 13:22 | ||
|
||
import django.db.models.deletion | ||
import fernet_fields.fields | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
('asset', '0002_alter_pineconeasset_api_key'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='APIEndpointAsset', | ||
fields=[ | ||
( | ||
'baseasset_ptr', | ||
models.OneToOneField( | ||
auto_created=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
parent_link=True, | ||
primary_key=True, | ||
serialize=False, | ||
to='asset.baseasset', | ||
), | ||
), | ||
('url', models.URLField(max_length=2048)), | ||
( | ||
'authentication_method', | ||
models.CharField( | ||
choices=[('Basic', 'Basic'), ('Bearer', 'Bearer')], | ||
default='Bearer', | ||
max_length=10, | ||
), | ||
), | ||
('api_key', fernet_fields.fields.EncryptedCharField(max_length=256)), | ||
('headers', models.JSONField(blank=True, null=True)), | ||
( | ||
'body', | ||
models.JSONField(blank=True, default={'data': '%query%'}, null=True), | ||
), | ||
], | ||
options={ | ||
'abstract': False, | ||
'base_manager_name': 'objects', | ||
}, | ||
bases=('asset.baseasset',), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
"""Logic for interfacing with an API Endpoint Asset.""" | ||
from logging import getLogger | ||
|
||
from asset.models import BaseAsset, PingResult, SearchResult | ||
from django.db import models | ||
from fernet_fields import EncryptedCharField | ||
|
||
logger = getLogger(__name__) | ||
|
||
|
||
class APIEndpointAsset(BaseAsset): | ||
"""Implementation of an API Endpoint asset.""" | ||
|
||
url = models.URLField(max_length=2048, blank=False, null=False) | ||
authentication_method = models.CharField( | ||
max_length=10, choices=[('Basic', 'Basic'), ('Bearer', 'Bearer')], default='Bearer' | ||
) | ||
api_key = EncryptedCharField(max_length=256, editable=True) | ||
headers = models.JSONField(blank=True, null=True) | ||
body = models.JSONField(blank=True, null=True, default={'data': '%query%'}) | ||
|
||
# Name of the file in the ./asset/static/ directory to use as a logo | ||
html_logo = 'asset/api-endpoint-logo.svg' | ||
html_name = 'API Endpoint' | ||
html_description = 'Generic API Endpoint Asset' | ||
|
||
HAS_PING = True | ||
|
||
@property | ||
def decrypted_api_key(self): | ||
"""Return the decrypted API key.""" | ||
if self.api_key is not None: | ||
try: | ||
decrypted_value = self.api_key | ||
masked_value = decrypted_value[:4] + '*' * (len(decrypted_value) - 4) | ||
return masked_value | ||
except UnicodeDecodeError: | ||
return 'Error: Decryption failed' | ||
return None | ||
|
||
def search(self, query: str, max_results: int) -> list[SearchResult]: | ||
"""Search the API Endpoint asset with the specified query.""" | ||
raise NotImplementedError('The search method is not implemented for this asset.') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So then how do we scan this asset? 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want to break this out into a separate PR to keep them both a reasonable size. |
||
|
||
def test_connection(self) -> PingResult: | ||
"""Ensure that the API Endpoint asset can be connected to.""" | ||
raise NotImplementedError('The test_connection method is not implemented for this asset.') | ||
|
||
def displayable_attributes(self): | ||
"""Display a subset of the model's attributes""" | ||
return [ | ||
{'label': 'URL', 'value': self.url}, | ||
{'label': 'Authentication Method', 'value': self.authentication_method}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Since this asset will support multiple auth types in the future, it's probably worth making this an opaque "secret" and adding an enum of the supported types (bearer is the only one to start). I'm guessing oauth is probably going to be needed sooner rather than later.
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.
spoke with @zimventures about this and I'm going to leave it as is. We don't expect that changing it later, with clearer direction, will take more effort than changing it now.