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.
- Loading branch information
Showing
8 changed files
with
76 additions
and
25 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
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.
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