Skip to content

Commit

Permalink
feat: add auth to translation request
Browse files Browse the repository at this point in the history
  • Loading branch information
nsprenkle committed Mar 19, 2024
1 parent 51aa00b commit 6df9588
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion xmodule/html_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from fs.errors import ResourceNotFound
from lxml import etree
from path import Path as path
from edx_rest_api_client.client import OAuthAPIClient
from web_fragments.fragment import Fragment
from xblock.core import XBlock
from xblock.fields import Boolean, List, Scope, String
Expand Down Expand Up @@ -118,9 +119,17 @@ def translated_view(self, _context, translate_lang):
shim_xmodule_js(fragment, 'HTMLModule')
return fragment

def _get_translations_client(self):
return OAuthAPIClient(
base_url=settings.TRANSLATIONS_SERVICE_EDX_OAUTH2_PROVIDER_URL,
client_id=settings.TRANSLATIONS_SERVICE_EDX_OAUTH2_KEY,
client_secret=settings.TRANSLATIONS_SERVICE_EDX_OAUTH2_SECRET,
)

def translate(self, content, language):
""" Request translated version of content from translations IDA"""

translation_client = self._get_translations_client()
url = f'{settings.AI_TRANSLATIONS_API_URL}/translate-xblock/'
headers = {
'content-type': 'application/json',
Expand All @@ -134,7 +143,7 @@ def translate(self, content, language):
"content_hash": sha256(content.encode('utf-8')).hexdigest(),
}

response = requests.post(
response = translation_client.post(
url,
data=json.dumps(payload),
headers=headers
Expand Down

0 comments on commit 6df9588

Please sign in to comment.