LoginSignup
0
0

More than 1 year has passed since last update.

AWS Lambda (Python 3.8)&Wrike APIで取得したjsonをパースし任意のキーで取り出してみる。

Last updated at Posted at 2021-05-12

この記事の概要

前回の続き

せっかくなのでほしい要素を取り出してみる。

本記事の投稿者

前も書きましたがPython&Lambda初心者(両方とも初めて書いてみた)なのでセオリー的なPerseの仕方などはわからないので力技です。

大まかな手順

Wrikeで永続トークンを生成し、LabmdaでPythonの関数つくって実行し、かえって来たクエリの任意のキーを取り出す。

今回の目的

サンプルソースからpermalink要素を抜き出す。
今回はpermalinkをキーとしましたが、その他クエリに使えそうなキーはWrike公式APIリファレンスを参照してください
https://developers.wrike.com/api/v4/tasks/#query-tasks
→こちらのResponseです。

APIからのレスポンス(例)

sample.json
{
    "kind": "tasks",
    "data": [
        {
            "id": "TASKID",
            "accountId": "AccountID",
            "title": "タスクのタイトル",
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
省略
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            "permalink": "https://www.wrike.com/open.htm?id=00000000",
        }
    ]
}

ソース

lambda_fucntion_2.py
import json
import urllib.request
from pprint import pprint

permanent_token='Wrike API 永続トークン'
url = 'https://www.wrike.com/api/v4/tasks'
header ={ 'Authorization': 'bearer '+ permanent_token}

def lambda_handler(event, context):
    req=urllib.request.Request(url, headers=header)

    with urllib.request.urlopen(req) as res:
    # resは http.client.HTTPResponse
        json_data = json.loads(res.read().decode('utf8')) # レスポンスボディ
        headers = res.getheaders() # ヘッダー(dict)
        status = res.getcode() # ステータスコード

        # data要素のみを格納
        data = json_data['data']
        #パーマリンクのみ取得 ここを任意のキーにすることで項目を変えられます。
        for entry in data:
            print(entry['permalink'])

キー

今回はpermalinkをキーとしましたが、その他クエリに使えそうなキーはWrike公式APIリファレンスを参照してください
https://developers.wrike.com/api/v4/tasks/#query-tasks
→こちらのResponseです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0