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

6-api-documentation #15

Merged
merged 5 commits into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 11 additions & 0 deletions apis/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from flask_restplus import Api
from .member import api as member

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

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
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'])
Copy link
Contributor

@dwi2 dwi2 Aug 6, 2019

Choose a reason for hiding this comment

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

This means after this patch, /photo is no longer working.
Can we also add an apis/photo.py to maintain compatibility?

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, I will add it.

Copy link
Member Author

@z6wdc z6wdc Aug 6, 2019

Choose a reason for hiding this comment

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

added by this commit 971363c

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