-
Notifications
You must be signed in to change notification settings - Fork 0
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
homework19.1 #27
base: main
Are you sure you want to change the base?
homework19.1 #27
Conversation
def _prepare_params(self, params: dict) -> dict: | ||
"""Check if all required params are available.""" | ||
if not isinstance(params, dict): | ||
raise ValueError('Params must be a dictionary.') | ||
if 'api_key' not in params: | ||
raise ValueError("The 'api_key' parameter is required.") | ||
return params |
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.
This looks redundant for this task - lets remove
self.params = self._prepare_params(params) | ||
|
||
@staticmethod | ||
def _clean_url(url: str) -> str: |
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.
this looks redundant too - just use url from the beginnig without < and > signs
try: | ||
response = requests.get( | ||
self.base_url, | ||
params=self.params, | ||
timeout=10, | ||
) | ||
response.raise_for_status() | ||
data = response.json() | ||
return data.get('photos', []) | ||
except (HTTPError, ConnectionError, Timeout) as err: | ||
_log.error(f'Error fetching photos: {err}') | ||
return [] |
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.
we have some logic for get request execution. This logic we use twice - > here and in photo download. So better to create helper function aka get_request and use it in both places
failed_downloads += 1 | ||
continue | ||
|
||
filename = f'mars_photo{idx}.jpg' |
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.
This looks as naming template . we could use it as constant or class attribute and then fill with format() call
with requests.get(photo_url, stream=True, timeout=10) as resp: | ||
resp.raise_for_status() | ||
with open(filename, 'wb') as fh: | ||
for chunk in resp.iter_content(chunk_size=8192): |
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.
All hardcodes aka chunk_size, timeout etc better to move to init
# End message | ||
if failed_downloads: | ||
_log.info(f'{failed_downloads} photo(s) could not be downloaded.') | ||
else: | ||
_log.info('All photos downloaded successfully!') |
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.
These logic should be hidden to main block below. Failued downloads could you could get as function call return value
Cool task. I like to work with API.