-
Notifications
You must be signed in to change notification settings - Fork 379
✨(api) add API route to fetch document content #1213
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
Open
sylvinus
wants to merge
5
commits into
main
Choose a base branch
from
api_content
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0bd53ae
✨(api) add API route to fetch document content
sylvinus cdc2411
🚨(lint) fix lint
sylvinus ab05fa6
♻️(convert) reuse existing convert yprovider endpoint for content API
sylvinus 148890a
✅(convert) improve tests with stricter tests and less ipsum
sylvinus b7ffc76
🗑️(convert) cleanup old content route
sylvinus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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,80 @@ | ||
"""Y-Provider API services.""" | ||
|
||
import json | ||
from base64 import b64encode | ||
|
||
from django.conf import settings | ||
|
||
import requests | ||
|
||
|
||
class ConversionError(Exception): | ||
"""Base exception for conversion-related errors.""" | ||
|
||
|
||
class ValidationError(ConversionError): | ||
"""Raised when the input validation fails.""" | ||
|
||
|
||
class ServiceUnavailableError(ConversionError): | ||
"""Raised when the conversion service is unavailable.""" | ||
|
||
|
||
class YProviderAPI: | ||
"""Service class for Y-Provider API operations.""" | ||
|
||
@property | ||
def auth_header(self): | ||
"""Build microservice authentication header.""" | ||
# Note: Yprovider microservice accepts only raw token, which is not recommended | ||
return f"Bearer {settings.Y_PROVIDER_API_KEY}" | ||
|
||
def _request(self, url, data, content_type): | ||
"""Make a request to the Y-Provider API.""" | ||
response = requests.post( | ||
url, | ||
data=data, | ||
headers={ | ||
"Authorization": self.auth_header, | ||
"Content-Type": content_type, | ||
}, | ||
timeout=settings.CONVERSION_API_TIMEOUT, | ||
verify=settings.CONVERSION_API_SECURE, | ||
) | ||
response.raise_for_status() | ||
return response | ||
|
||
def convert(self, text): | ||
"""Convert a Markdown text into our internal format using an external microservice.""" | ||
|
||
if not text: | ||
raise ValidationError("Input text cannot be empty") | ||
|
||
try: | ||
response = self._request( | ||
f"{settings.Y_PROVIDER_API_BASE_URL}{settings.CONVERSION_API_ENDPOINT}/", | ||
text, | ||
"text/markdown", | ||
) | ||
return b64encode(response.content).decode("utf-8") | ||
except requests.RequestException as err: | ||
raise ServiceUnavailableError( | ||
"Failed to connect to backend service", | ||
) from err | ||
|
||
def content(self, base64_content, format_type): | ||
"""Convert base64 Yjs content to different formats using the y-provider service.""" | ||
|
||
if not base64_content: | ||
raise ValidationError("Input content cannot be empty") | ||
|
||
data = json.dumps({"content": base64_content, "format": format_type}) | ||
try: | ||
response = self._request( | ||
f"{settings.Y_PROVIDER_API_BASE_URL}content/", data, "application/json" | ||
) | ||
return response.json() | ||
except requests.RequestException as err: | ||
raise ServiceUnavailableError( | ||
"Failed to connect to backend service", | ||
) from err |
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.
Uh oh!
There was an error while loading. Please reload this page.