Merge pull request #206 from simple-login/normalize-api-response

Normalize api response
This commit is contained in:
Son Nguyen Kim 2020-05-23 19:33:13 +02:00 committed by GitHub
commit 1ded0c3e26
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 58 additions and 51 deletions

View file

@ -836,22 +836,7 @@ Input:
- (Optional) note: alias note - (Optional) note: alias note
Output: Output:
If success, 201 with the new alias, for example If success, 201 with the new alias info. Use the same format as in GET /api/aliases/:alias_id
```json
{
"alias": "www_groupon_com@my_domain.com",
"creation_date": "2020-02-04 16:23:02+00:00",
"creation_timestamp": 1580833382,
"email": "www_groupon_com@my_domain.com",
"id": 4,
"nb_block": 0,
"nb_forward": 0,
"nb_reply": 0,
"enabled": true,
"note": "This is a note"
}
```
#### POST /api/alias/random/new #### POST /api/alias/random/new
@ -865,22 +850,7 @@ Input:
- (Optional) note: alias note - (Optional) note: alias note
Output: Output:
If success, 201 with the new alias, for example If success, 201 with the new alias info. Use the same format as in GET /api/aliases/:alias_id
```json
{
"alias": "prefix.suffix@my_domain.com",
"creation_date": "2020-02-04 16:23:02+00:00",
"creation_timestamp": 1580833382,
"email": "www_groupon_com@my_domain.com",
"id": 4,
"nb_block": 0,
"nb_forward": 0,
"nb_reply": 0,
"enabled": true,
"note": "This is a note"
}
```
#### GET /api/v2/aliases #### GET /api/v2/aliases
@ -970,19 +940,43 @@ Input:
- `alias_id` in url - `alias_id` in url
Output: Output:
Alias info Alias info, use the same format as in /api/v2/aliases. For example:
```json ```json
{ {
"creation_date": "2020-02-04 16:23:02+00:00", "creation_date": "2020-04-06 17:57:14+00:00",
"creation_timestamp": 1580833382, "creation_timestamp": 1586195834,
"email": "e3@.alo@sl.local", "email": "prefix1.cat@sl.local",
"id": 4, "name": "A Name",
"nb_block": 0, "enabled": true,
"nb_forward": 0, "id": 3,
"nb_reply": 0, "mailbox": {
"enabled": true, "email": "a@b.c",
"note": "This is a note" "id": 1
},
"mailboxes": [
{
"email": "m1@cd.ef",
"id": 2
},
{
"email": "john@wick.com",
"id": 1
}
],
"latest_activity": {
"action": "forward",
"contact": {
"email": "c1@example.com",
"name": null,
"reverse_alias": "\"c1 at example.com\" <re1@SL>"
},
"timestamp": 1586195834
},
"nb_block": 0,
"nb_forward": 1,
"nb_reply": 0,
"note": null
} }
``` ```

View file

@ -5,7 +5,6 @@ from flask import Blueprint, request, jsonify, g
from flask_login import current_user from flask_login import current_user
from app.extensions import db from app.extensions import db
from app.log import LOG
from app.models import ApiKey from app.models import ApiKey
api_bp = Blueprint(name="api", import_name=__name__, url_prefix="/api") api_bp = Blueprint(name="api", import_name=__name__, url_prefix="/api")

View file

@ -207,7 +207,10 @@ def get_alias_info(alias: Alias) -> AliasInfo:
return alias_info return alias_info
def get_alias_info_v2(alias: Alias, mailbox) -> AliasInfo: def get_alias_info_v2(alias: Alias, mailbox=None) -> AliasInfo:
if not mailbox:
mailbox = alias.mailbox
q = ( q = (
db.session.query(Contact, EmailLog) db.session.query(Contact, EmailLog)
.filter(Contact.alias_id == alias.id) .filter(Contact.alias_id == alias.id)

View file

@ -10,10 +10,10 @@ from app.api.serializer import (
serialize_alias_info, serialize_alias_info,
serialize_contact, serialize_contact,
get_alias_infos_with_pagination, get_alias_infos_with_pagination,
get_alias_info,
get_alias_contacts, get_alias_contacts,
get_alias_infos_with_pagination_v2, get_alias_infos_with_pagination_v2,
serialize_alias_info_v2, serialize_alias_info_v2,
get_alias_info_v2,
) )
from app.config import EMAIL_DOMAIN from app.config import EMAIL_DOMAIN
from app.dashboard.views.alias_log import get_alias_log from app.dashboard.views.alias_log import get_alias_log
@ -327,7 +327,7 @@ def get_alias(alias_id):
if alias.user_id != user.id: if alias.user_id != user.id:
return jsonify(error="Forbidden"), 403 return jsonify(error="Forbidden"), 403
return jsonify(**serialize_alias_info(get_alias_info(alias))), 200 return jsonify(**serialize_alias_info_v2(get_alias_info_v2(alias))), 200
@api_bp.route("/aliases/<int:alias_id>/contacts") @api_bp.route("/aliases/<int:alias_id>/contacts")

View file

@ -1,6 +1,6 @@
from smtplib import SMTPRecipientsRefused from smtplib import SMTPRecipientsRefused
from flask import g, jsonify from flask import g
from flask import jsonify from flask import jsonify
from flask import request from flask import request
from flask_cors import cross_origin from flask_cors import cross_origin

View file

@ -4,7 +4,12 @@ from flask_cors import cross_origin
from itsdangerous import SignatureExpired from itsdangerous import SignatureExpired
from app.api.base import api_bp, require_api_auth from app.api.base import api_bp, require_api_auth
from app.api.serializer import serialize_alias_info, get_alias_info from app.api.serializer import (
serialize_alias_info,
get_alias_info,
serialize_alias_info_v2,
get_alias_info_v2,
)
from app.config import MAX_NB_EMAIL_FREE_PLAN from app.config import MAX_NB_EMAIL_FREE_PLAN
from app.dashboard.views.custom_alias import verify_prefix_suffix, signer from app.dashboard.views.custom_alias import verify_prefix_suffix, signer
from app.extensions import db from app.extensions import db
@ -170,4 +175,7 @@ def new_custom_alias_v2():
AliasUsedOn.create(alias_id=alias.id, hostname=hostname, user_id=alias.user_id) AliasUsedOn.create(alias_id=alias.id, hostname=hostname, user_id=alias.user_id)
db.session.commit() db.session.commit()
return jsonify(alias=full_alias, **serialize_alias_info(get_alias_info(alias))), 201 return (
jsonify(alias=full_alias, **serialize_alias_info_v2(get_alias_info_v2(alias))),
201,
)

View file

@ -3,7 +3,10 @@ from flask import jsonify, request
from flask_cors import cross_origin from flask_cors import cross_origin
from app.api.base import api_bp, require_api_auth from app.api.base import api_bp, require_api_auth
from app.api.serializer import serialize_alias_info, get_alias_info from app.api.serializer import (
get_alias_info_v2,
serialize_alias_info_v2,
)
from app.config import MAX_NB_EMAIL_FREE_PLAN from app.config import MAX_NB_EMAIL_FREE_PLAN
from app.extensions import db from app.extensions import db
from app.log import LOG from app.log import LOG
@ -57,6 +60,6 @@ def new_random_alias():
db.session.commit() db.session.commit()
return ( return (
jsonify(alias=alias.email, **serialize_alias_info(get_alias_info(alias))), jsonify(alias=alias.email, **serialize_alias_info_v2(get_alias_info_v2(alias))),
201, 201,
) )