Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
# lambda-rainFallTrendAPI
# lambda-rainFallTrendAPI

## Overview
現在の降水量の傾向を返します。
データは10分毎に更新されます。
データは30分前の傾向データになります。

## Request
Method : GET
Endpoint : /production/rain-fall-trend/{country}/{prefectures}/{river}
Parameter :
1. {country} : 国名を指定 ex)japan, taiwan
2. {prefectures} : 都道府県を指定 ex)tokyo, taipei
3. {river} : 河川名を指定 ex)arakawa, danshui

## Response

以下の Json を返します。
~~~
{
"timestamp": "2017-08-12T12:50:00",
"trend": "↑"
}
~~~

timestamp : 分析時点の日時(UTC)
trend : 降雨量の傾向(↑、↓、→)
15 changes: 15 additions & 0 deletions lambda.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "getRainFallTrend",
"description": "降雨量の傾向を取得する",
"region": "ap-northeast-1",
"runtime": "python3.6",
"handler": "lambda_function.lambda_handler",
"role": "arn:aws:iam::657885203613:role/lambda_athena_s3",
"ignore": [
"circle\\.yml$",
".git",
"/.*\\.pyc$"
],
"timeout": 300,
"memory": 128
}
49 changes: 49 additions & 0 deletions lambda_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import json
import urllib.parse
import boto3

client = boto3.client('s3')

bucket = 'test-uodu-s3'
target = 'trend'

def get_rainfall_trend(key):
response = client.get_object(Bucket=bucket, Key=key)
body = response['Body'].read().decode('utf-8')
return body

def set_response_body(status_code, body):
headers = {}
headers['Content-Type'] = 'application/json'

res_body = {}
res_body['statusCode'] = status_code
res_body['headers'] = headers
res_body['body'] = body

return res_body

def lambda_handler(event, context):

# クエリが渡されてない場合$
if (event['pathParameters'] is None):
return set_response_body(400, 'Bad Request No PathParameters')
else:
params = event['pathParameters']

# クエリパラメータが不正な場合のデフォルトを荒川に
if (set(params) >= {'country', 'prefectures', 'river'}):
country = params['country']
prefectures = params['prefectures']
river = params['river']
key = target + '/' + country + '/' + prefectures + '/' + river + '/trendRainFall.json'
else:
return set_response_body(400, 'Bad Request No Item')

try:
json_str = get_rainfall_trend(key)

return set_response_body(200, json_str)
except Exception as e:
print(e)
return set_response_body(500, 'File Not Found')
7 changes: 7 additions & 0 deletions test_200.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"pathParameters": {
"country": "taiwan",
"prefectures": "Taipei",
"river": "Danshui"
}
}
7 changes: 7 additions & 0 deletions test_400_bad_query.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"pathParameters": {
"country": "taiwan",
"prefectures": "Taipei",
"river": "D"
}
}
6 changes: 6 additions & 0 deletions test_400_no_item.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pathParameters": {
"country": "taiwan",
"prefectures": "Taipei"
}
}
3 changes: 3 additions & 0 deletions test_400_no_query.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"pathParameters": null
}