4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Redmineに対してREST APIを実行してみる

Last updated at Posted at 2022-07-22

AWS Lambdaを使ってRestAPIを実行してみる

1. Redmineの管理者で RESTによるWebサービスを有効にするをチェックONにする

image.png

2. Redmineの画面で個人設定画面の右より、APIアクセスキーを発行する

redmine-api-key.png

3.AWS LambdaのマネジメントコンソールでLambdaのソースを書く


import request

エラーになります。lambda Layer使わないと無理だった。
もっと手軽にしたい(手軽さがない!)

検索すると、クラスメソッドさんの記事が見つかる
https://dev.classmethod.jp/articles/unused-requests-library/
「標準のurllib.requestsを使用する」

書きかえて、redmineのプロジェクト一覧を取得してみる

import json

import urllib.request

def get_simple(url: str):
    req = urllib.request.Request(url,
                                 method='GET')
    return urllib.request.urlopen(req)

def lambda_handler(event, context):

    headers = {
        'Accept': 'application/json',
    }
    data = '{}'
    try:
        res = get_simple(url='https://xxxx.xxxx.xxx/redmine/projects.json?&limit=5&key=<<API KEY>>')

    except urllib.error.HTTPError as e:
        print("---error")
        print(e.code)
        print(e.reason)
    else:
        print("---ok")
        print(res.status)
        print(res)
        print(res.read().decode('utf-8'))

    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

Function Logs (ログを整形して抜粋)
APIでlimit指定しないとコンソールログが見切れてしまう(途切れる)

{
	"projects": [
		{
			"id": 153,
			"name": "NNNNN",
			"identifier": "aaaa",
			"description": "",
			"status": 1,
			"is_public": false,
			"inherit_members": false,
			"custom_fields": [
				{
					"id": 22,
					"name": "Slack Channel",
					"value": ""
				},
				{
					"id": 30,
					"name": "Slack URL",
					"value": null
				}
			],
			"created_on": "2021-12-04T02:17:03Z",
			"updated_on": "2021-12-04T02:17:03Z"
		},
...
	"total_count": 200,
	"offset": 0,
	"limit": 5
}

かなり手軽に、REST APIにアクセスできました

4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?