Skip to content

Commit

Permalink
6-api-documentation (#15)
Browse files Browse the repository at this point in the history
* api document, fixes #14

* add member api

* delete cat api

* fix typo

* add photo api
  • Loading branch information
z6wdc authored Aug 8, 2019
1 parent ea69cbf commit 0dd65d5
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 15 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@
- Install and launch [Cloud SQL Proxy](https://cloud.google.com/sql/docs/mysql/sql-proxy)

```sh
cloud_sql_proxy -dir=/tmp/cloudsql -instances=yori_instance_name=tcp:3306
./cloud_sql_proxy -dir=/tmp/cloudsql -instances=yori_instance_name=tcp:3306
```

- Launch server

```sh
env FLASK_APP=main.py DB_USER=username DB_PASS=password DB_NAME=database_name CLOUD_SQL_CONNECTION_NAME=yori_instance_name flask run
env FLASK_APP=main.py DB_USER=username DB_PASS=password CLOUD_SQL_CONNECTION_NAME=yori_instance_name flask run
```

or if you want to enable network access to local server (especially when you are developing yori-view)

```sh
env FLASK_APP=main.py DB_USER=username DB_PASS=password DB_NAME=database_name CLOUD_SQL_CONNECTION_NAME=yori_instance_name flask run --host=0.0.0.0
env FLASK_APP=main.py DB_USER=username DB_PASS=password CLOUD_SQL_CONNECTION_NAME=yori_instance_name flask run --host=0.0.0.0
```

If setting environments bothers you, write them in your dotfiles.
Expand Down
13 changes: 13 additions & 0 deletions apis/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from flask_restplus import Api
from .photo import api as photo
from .member import api as member

api = Api(
title='Yori API',
version='1.0',
description='API Documentation',
# All API metadatas
)

api.add_namespace(photo)
api.add_namespace(member)
31 changes: 31 additions & 0 deletions apis/member.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from flask import jsonify
from flask_restplus import Namespace, Resource, fields

api = Namespace('member', description='Members related operations')

member = api.model('member', {
'member_name_en': fields.String(required=True, description='英語'),
'member_name': fields.String(required=True, description='名前'),
'member_name_gana': fields.String(required=True, description='ひらがな'),
'member_gen': fields.Integer(required=True, description='期'),
'member_graduated': fields.Boolean(required=True, description='卒業'),
'group_id': fields.String(required=True, description='group id')
})

MEMBERS = [
{
'member_name_en': 'imaizumi_yui',
'member_name': '今泉佑唯',
'member_name_gana': 'いまいずみ_ゆい',
'member_gen': 1,
'member_graduated': True,
'group_id': 'keyakizaka'
}
]

@api.route('/')
class MemberList(Resource):
@api.doc('get_members')
@api.marshal_list_with(member)
def get(self):
return MEMBERS
31 changes: 31 additions & 0 deletions apis/photo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from flask import jsonify
from flask_restplus import Namespace, Resource, fields

api = Namespace('photo', description='Photos related operations')

photo = api.model('photo', {
'photo_group': fields.String(required=True, description='グループ'),
'photo_member': fields.String(required=True, description='名前'),
'photo_costume': fields.String(required=True, description='服'),
'photo_type': fields.String(required=True, description='距離'),
'photo_number': fields.Integer(required=True, description='数'),
'photo_folder': fields.String(required=True, description='フォルダ')
})

PHOTOS = [
{
"photo_group": "乃木坂46",
"photo_member": "樋口日奈",
"photo_costume": "2019 summer concert 1",
"photo_type": "ヨリ",
"photo_number": 1,
"photo_folder": "all"
}
]

@api.route('/')
class MemberList(Resource):
@api.doc('get_photos')
@api.marshal_list_with(photo)
def get(self):
return PHOTOS
2 changes: 1 addition & 1 deletion app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ env_variables:
CLOUD_SQL_CONNECTION_NAME: your-connection-name
DB_USER: USER
DB_PASS: PASSWORD
DB_NAME: DATABASE
DB_NAME: yori_database
13 changes: 2 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import datetime
import logging
import os
import json

from flask import Flask
from flask_cors import CORS
import sqlalchemy
import requests

from apis import api

def onGAE():
return os.getenv('GAE_ENV', '').startswith('standard')


app = Flask(__name__)
CORS(app)
api.init_app(app)
app.config['JSON_AS_ASCII'] = False

db_user = os.environ.get('DB_USER')
Expand Down Expand Up @@ -51,7 +52,6 @@ def onGAE():

db = sqlalchemy.create_engine(connection_string)


# TODO:
# We should create a package and move this into __init__.py of that package
def notify():
Expand All @@ -65,15 +65,6 @@ def notify():
logger.info(message)
requests.post(slack_webhook_url, json={'text': message})


# TODO:
# We should create a package and move this into that package
@app.route('/photo', methods=['GET'])
def index():
result = db.execute('select * from photos')
return json.dumps([dict(data) for data in result])


# TODO:
# We should create a package and move this into __init__.py of that package
if onGAE():
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Flask==1.0.2
flask-cors>=3.0.8
flask_restplus==0.12.1
SQLAlchemy>=1.3.0
PyMySQL==0.9.3
google-cloud-logging>=1.8.0
Expand Down

0 comments on commit 0dd65d5

Please sign in to comment.