<Python, flask> REST APIをやってみた。。。

前から気になっていた、REST APIFlask-SQLAlchemy
サンプルコードを作ってみた。

http://127.0.0.1/sqlalc/id/name/looksの情報を、Json{"id": id, "name": name, "looks":looks}にして、通信+SQLデータベースに取り込む。
GET + /id/name/*/SQLからJsonゲット!
PUT + /-/name/looks/SQLにデータプット!
DELETE + /id/-/-/SQLからデータデリート!

実際に動かしてみた。

1... まずは適当な端末で、restapi.py を走らせる。

>py restapi.py
 * Running on http://127.0.0.1:8000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger pin code: xxx-xxx-xxx

2... で、別の端末で、まずは、データベースをチェックする。

>curl -i http://127.0.0.1:8000/sqlalc/
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 40
Server: Werkzeug/0.11.4 Python/3.5.3
Date: Sun, 03 Dec 2017 13:02:54 GMT

{
  "data": [],
  "r": "GET success"
}

3... ほんでもって、PUT with id = '1'。で、fail。idではPUTできないことにしてるから、、

>curl -i http://127.0.0.1:8000/sqlalc/1/Taro/Hage -X PUT
HTTP/1.0 403 FORBIDDEN
Content-Type: application/json
Content-Length: 49
Server: Werkzeug/0.11.4 Python/3.5.3
Date: Sun, 03 Dec 2017 13:03:14 GMT

{
  "id": "1",
  "r": "PUT fail, no id found"
}

4... と、いうことで、再度PUT with id = '-'。そうすっと、success

> curl -i http://127.0.0.1:8000/sqlalc/-/Taro/Hage -X PUT
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 76
Server: Werkzeug/0.11.4 Python/3.5.3
Date: Sun, 03 Dec 2017 13:03:32 GMT

{
  "id": 1,
  "looks": "Hage",
  "name": "Taro",
  "r": "PUT success"
}

5... PUT もういっちょう。

>curl -i http://127.0.0.1:8000/sqlalc/-/Jiro/MottoHage -X PUT
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 81
Server: Werkzeug/0.11.4 Python/3.5.3
Date: Sun, 03 Dec 2017 13:03:43 GMT

{
  "id": 2,
  "looks": "MottoHage",
  "name": "Jiro",
  "r": "PUT success"
}

6... データベースチェック。

>curl -i http://127.0.0.1:8000/sqlalc/
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 196
Server: Werkzeug/0.11.4 Python/3.5.3
Date: Sun, 03 Dec 2017 13:03:50 GMT

{
  "data": [
    {
      "id": 1,
      "looks": "Hage",
      "name": "Taro"
    },
    {
      "id": 2,
      "looks": "MottoHage",
      "name": "Jiro"
    }
  ],
  "r": "GET success"
}

7... GET id = '1。'

>curl -i http://127.0.0.1:8000/sqlalc/1/-/-
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 76
Server: Werkzeug/0.11.4 Python/3.5.3
Date: Sun, 03 Dec 2017 13:04:04 GMT

{
  "id": 1,
  "looks": "Hage",
  "name": "Taro",
  "r": "GET success"
}

8... か、 GET with name = 'Taro'

>curl -i http://127.0.0.1:8000/sqlalc/-/Taro/-
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 76
Server: Werkzeug/0.11.4 Python/3.5.3
Date: Sun, 03 Dec 2017 13:04:17 GMT

{
  "id": 1,
  "looks": "Hage",
  "name": "Taro",
  "r": "GET success"
}

9... あとは、削除。DELETE with id = '1'

>curl -i http://127.0.0.1:8000/sqlalc/1/-/- -X DELETE
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 28
Server: Werkzeug/0.11.4 Python/3.5.3
Date: Sun, 03 Dec 2017 13:04:29 GMT

{
  "r": "DELETE success"
}

10... データベースチェック。

>curl -i http://127.0.0.1:8000/sqlalc/
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 121
Server: Werkzeug/0.11.4 Python/3.5.3
Date: Sun, 03 Dec 2017 13:04:34 GMT

{
  "data": [
    {
      "id": 2,
      "looks": "MottoHage",
      "name": "Jiro"
    }
  ],
  "r": "GET success"
}

あと、Flask-RESTfulも試してみた。

けど、なんだか、使わない場合とコードの差があまりなかった。。。
これだと、あまり使わないかな、、、

参考にさせてもらったところ。
qiita.com

qiita.com

qiita.com

マニュアル。

Flask-SQLAlchemy — Flask-SQLAlchemy Documentation (2.3)

Flask-RESTful — Flask-RESTful 0.3.6 documentation