forked from Cal-CS-61A-Staff/seating
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* some test routes * fetching photo from c1c api * feat: server photo cache * c1c client & mock * fix: remove unused imports
- Loading branch information
Showing
17 changed files
with
200 additions
and
250 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,3 +112,6 @@ gcp-service-account-credentials.json | |
|
||
# bootstrap | ||
setup*.sh | ||
|
||
# cache | ||
.cache/ |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -15,3 +15,4 @@ canvasapi==3.2.0 | |
gunicorn==21.2.0 | ||
psycopg2==2.9.9 | ||
sentry-sdk[flask] | ||
Flask-Caching==2.1.0 |
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,15 @@ | ||
from flask_caching import Cache | ||
from server import app | ||
|
||
cache_store = Cache(app, config={ | ||
'CACHE_TYPE': 'FileSystemCache', | ||
'CACHE_DIR': '.cache', | ||
'CACHE_DEFAULT_TIMEOUT': 3600} | ||
) | ||
|
||
|
||
def cache_key_photo(canvas_id: str): | ||
return f'student_photo_{canvas_id}' | ||
|
||
|
||
cache_life_photo = int(app.config.get('C1C_PHOTO_CACHE_LIFE')) |
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,43 @@ | ||
from server import app | ||
from server.services.c1c import fake_data | ||
|
||
|
||
def is_mock_c1c() -> bool: | ||
return app.config['MOCK_C1C'] and \ | ||
app.config['FLASK_ENV'].lower() != 'production' | ||
|
||
|
||
class C1C: | ||
def __init__(self, proxy_url, api_domain, username, password): | ||
self.proxy_dict = { | ||
'http': proxy_url, | ||
'https': proxy_url | ||
} if proxy_url else None | ||
self.api_domain = api_domain | ||
self.username = username | ||
self.password = password | ||
|
||
def _make_request(self, path, method='GET'): | ||
import requests | ||
url = f'{self.api_domain}{path}' | ||
if self.proxy_dict: | ||
return requests.request(method, url, proxies=self.proxy_dict, | ||
auth=(self.username, self.password)) | ||
else: | ||
return requests.request(method, url, auth=(self.username, self.password)) | ||
|
||
def get_student_photo(self, student_canvas_id): | ||
if is_mock_c1c(): | ||
return fake_data.get_fake_photo(student_canvas_id) | ||
try: | ||
r = self._make_request(f'/c1c-api/v1/photo/{student_canvas_id}') | ||
if r.status_code == 200: | ||
return r.content | ||
else: | ||
return None | ||
except: | ||
return None | ||
|
||
|
||
c1c_client = C1C(app.config['C1C_PROXY_URL'], app.config['C1C_API_DOMAIN'], | ||
app.config['C1C_API_USERNAME'], app.config['C1C_API_PASSWORD']) |
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,18 @@ | ||
import json | ||
|
||
FAKE_PHOTO_DICT = None | ||
|
||
if not FAKE_PHOTO_DICT: | ||
with open('server/services/c1c/fake_data/photos.json', 'rb') as f: | ||
FAKE_PHOTO_DICT = json.load(f) | ||
|
||
|
||
def get_fake_photo(student_canvas_id): | ||
print(FAKE_PHOTO_DICT) | ||
try: | ||
with open(f"server/services/c1c/fake_data/photos/{FAKE_PHOTO_DICT[student_canvas_id]}", | ||
'rb') as f: | ||
return f.read() | ||
except Exception as e: | ||
print(e) | ||
return None |
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,6 @@ | ||
{ | ||
"123456": "sample1.jpg", | ||
"234567": "sample1.jpg", | ||
"345678": "sample1.jpg", | ||
"456789": "sample1.jpg" | ||
} |
Loading
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.