Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

homework19.1 #27

wants to merge 1 commit into from

Conversation

vladspirin
Copy link
Owner

Cool task. I like to work with API.

@vladspirin vladspirin requested a review from antohaUa December 13, 2024 11:31
@vladspirin vladspirin self-assigned this Dec 13, 2024
Comment on lines +47 to +53
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
Copy link
Collaborator

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:
Copy link
Collaborator

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

Comment on lines +57 to +68
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 []
Copy link
Collaborator

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'
Copy link
Collaborator

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):
Copy link
Collaborator

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

Comment on lines +101 to +105
# End message
if failed_downloads:
_log.info(f'{failed_downloads} photo(s) could not be downloaded.')
else:
_log.info('All photos downloaded successfully!')
Copy link
Collaborator

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

@vladspirin vladspirin added the need to improve There is a possibility of improving the code label Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need to improve There is a possibility of improving the code
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

2 participants