Skip to content

WIP - feature/basic api for app#99

Open
Ombental wants to merge 1 commit intoPythonFreeCourse:developfrom
Ombental:feature/basic-api-for-app
Open

WIP - feature/basic api for app#99
Ombental wants to merge 1 commit intoPythonFreeCourse:developfrom
Ombental:feature/basic-api-for-app

Conversation

@Ombental
Copy link
Copy Markdown

basic implementation, no docs on front, no tests, no documentation yet

Hey, want some CRs for code

@Ombental Ombental changed the base branch from main to develop January 20, 2021 17:53
@yammesicka yammesicka closed this Jan 21, 2021
@yammesicka yammesicka deleted the branch PythonFreeCourse:develop January 21, 2021 02:36
@yammesicka yammesicka reopened this Jan 21, 2021
Comment thread app/routers/api.py

api_routes = [{'name': '/new_event',}, {'name': '/{date}'}]
api_key = session.query(Token).filter_by(owner_id=user.id).first()
session.close()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary? The session is being closed at the end of the get_db

type="text/javascript"
src="{{ url_for('static', path='/apiKeyGenerator.js') }}"
></script>
{% endblock %}
Copy link
Copy Markdown
Contributor

@IdanPelled IdanPelled Jan 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great work!
remember, tests and documentation are essential

Comment thread app/database/database.py

SQLALCHEMY_DATABASE_URL = os.getenv(
"DATABASE_CONNECTION_STRING", config.DEVELOPMENT_DATABASE_STRING)
"DATABASE_CONNECTION_STRING2", config.DEVELOPMENT_DATABASE_STRING)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mistake?

Comment thread app/database/database.py
"DATABASE_CONNECTION_STRING", config.DEVELOPMENT_DATABASE_STRING)
"DATABASE_CONNECTION_STRING2", config.DEVELOPMENT_DATABASE_STRING)

#pool_pre_ping=True for POSTGRES
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

Comment thread app/database/models.py
owner = relationship("User", back_populates="events")


class Token(Base):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it could be nice to add a short documentation for this table.

callAPIKeyRoute("/api/generate_key", { user: user_id, refresh: state }).then(
(data) => {
let keyText = document.getElementById("apiKeyHolder");
keyText.textContent = "API Key: " + data.key;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't you use f"string {param}" ?

Comment thread app/static/popover.js
@@ -1,9 +1,9 @@
// Enable bootstrap popovers
// // Enable bootstrap popovers
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revisit this file and remove unneeded code.

Comment thread app/database/database.py

SQLALCHEMY_DATABASE_URL = os.getenv(
"DATABASE_CONNECTION_STRING", config.DEVELOPMENT_DATABASE_STRING)
"DATABASE_CONNECTION_STRING2", config.DEVELOPMENT_DATABASE_STRING)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure you don't commit this change :)

Comment thread app/database/models.py

id = Column(String, primary_key=True, index=True)
owner_id = Column(Integer, ForeignKey("users.id"), nullable=False, unique=True)
owner = relationship("User", back_populates="token") No newline at end of file
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a good practice, we can add expiry_date. If you really into it, read about security in APIs: https://owasp.org/www-project-api-security/

Comment thread app/routers/api.py
return JSONResponse(jsonable_encoder({'key': token}))


@key_gen_router.post('/delete_key')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should use @key_gen_route.delete?

Comment thread app/routers/api.py
api_routes = [{'name': '/new_event',}, {'name': '/{date}'}]
api_key = session.query(Token).filter_by(owner_id=user.id).first()
session.close()
no_api_key = True
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can change these lines to key = getattr(api_key, 'id', None) and remove the no_api_key variable?

Comment thread app/routers/api.py
from typing import Optional


def check_api_key(key: str, session=Depends(get_db)):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should implement rate limit to this check

Comment thread app/routers/api.py
data = await request.json()
if data.get('refresh', False):
session.query(Token).filter_by(owner_id=data['user']).delete()
token = secrets.token_urlsafe(32)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job for using secrets!

Comment thread app/static/api_style.css
@@ -0,0 +1,8 @@
#apiKeyDelete {
font-size: 0.6rem;
margin-left: 20px;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer working with em/%

Comment thread app/routers/api.py
session.commit()
session.close()
return JSONResponse(jsonable_encoder({'success': True}))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too many blank lines :)

Comment thread app/routers/api.py
session.add(event)
session.commit()
d['id'] = event.id
session.close()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as @IdanPelled comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants