/
weekly_api.py
176 lines (128 loc) · 5.43 KB
/
weekly_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# -*- coding: utf-8 -*-
"""Bunker 1 Church Weekly API implemented using Google Cloud Endpoints.
Defined here are the ProtoRPC messages needed to define Schemas for methods
as well as those methods defined in an API.
"""
import urllib
import endpoints
from google.appengine.api import memcache
from protorpc import messages
from protorpc import message_types
from protorpc import remote
from models import Weekly, Token
from push import push_to, push_to_all
from datetime import datetime
package = 'Weekly'
class WeeklyMessage(messages.Message):
"""Weekly that stores a message."""
title = messages.StringField(1)
speaker = messages.StringField(2)
date = messages.StringField(3)
link = messages.StringField(4)
class WeeklyCollection(messages.Message):
"""Collection of Weeklies."""
items = messages.MessageField(WeeklyMessage, 1, repeated=True)
store = messages.StringField(2)
class RegistTokenResult(messages.Message):
result = messages.StringField(1)
def getWeeklyList():
store = 'mem'
data = memcache.get('weekly')
if not data:
store = 'db'
data = [{
'title' : weekly.title,
'speaker' : weekly.speaker,
'date' : weekly.date.strftime('%Y%m%d'),
'link' : weekly.link,
} for weekly in Weekly.query().fetch()]
return (data, store)
@endpoints.api(name='bunker1cc', version='v1')
class B1CWeeklyApi(remote.Service):
"""Bunker 1 Church Weekly API v1."""
@endpoints.method(message_types.VoidMessage, WeeklyCollection,
path='weekly', http_method='GET',
name='weekly.list')
def weeklies_list(self, unused_request):
data, store = getWeeklyList()
weeklies = WeeklyCollection(items = [WeeklyMessage(**weekly) for weekly in data ], store=store)
return weeklies
WHEN_RESOURCE = endpoints.ResourceContainer(
message_types.VoidMessage,
when=messages.StringField(1))
@endpoints.method(WHEN_RESOURCE, WeeklyMessage,
path='weekly/{when}', http_method='GET',
name='weekly.get')
def weeklies_get(self, request):
when = datetime.strptime(request.when,'%Y%m%d')
data = Weekly.query(Weekly.date==when).fetch(1)
if len(data) != 1 :
raise endpoints.NotFoundException('Weekly %s not found.' % (request.id,))
weekly = data[0]
return WeeklyMessage(
title = weekly['title'],
speaker = weekly['speaker'],
date = weekly['date'],
link = weekly['link'],
)
REG_RESOURCE = endpoints.ResourceContainer(
message_types.VoidMessage,
token=messages.StringField(2),
)
@endpoints.method(REG_RESOURCE, RegistTokenResult,
path='regist/{token}', http_method='POST',
name='weekly.regist')
def regist_token(self, request):
if Token.query(Token.token==request.token).fetch():
return RegistTokenResult(result="DUP")
token = Token(token=request.token, os='ios');
token.put();
push_to(token, u"환영합니다! 벙커원 교회 주보 알림목록에 등록되었습니다!", 0)
return RegistTokenResult(result="OK")
@endpoints.api(name='bunker1cc', version='v2')
class B1CWeeklyApiV2(remote.Service):
"""Bunker 1 Church Weekly API v2."""
@endpoints.method(message_types.VoidMessage, WeeklyCollection,
path='weekly', http_method='GET',
name='weekly.list')
def weeklies_list(self, unused_request):
data, store = getWeeklyList()
weeklies = WeeklyCollection(items = [WeeklyMessage(**weekly) for weekly in data ], store=store)
return weeklies
WHEN_RESOURCE = endpoints.ResourceContainer(
message_types.VoidMessage,
when=messages.StringField(1))
@endpoints.method(WHEN_RESOURCE, WeeklyMessage,
path='weekly/{when}', http_method='GET',
name='weekly.get')
def weeklies_get(self, request):
when = datetime.strptime(request.when,'%Y%m%d')
data = Weekly.query(Weekly.date==when).fetch(1)
if len(data) != 1 :
raise endpoints.NotFoundException('Weekly %s not found.' % (request.id,))
weekly = data[0]
return WeeklyMessage(
title = weekly['title'],
speaker = weekly['speaker'],
date = weekly['date'],
link = weekly['link'],
)
REG_RESOURCE = endpoints.ResourceContainer(
message_types.VoidMessage,
token=messages.StringField(2),
os=messages.StringField(3),
width=messages.StringField(4),
height=messages.StringField(5)
)
@endpoints.method(REG_RESOURCE, RegistTokenResult,
path='regist/{token}/{os}/{width}/{height}', http_method='POST',
name='weekly.regist')
def regist_token(self, request):
if Token.query(Token.token==request.token).fetch():
return RegistTokenResult(result="DUP")
token = urllib.unquote(request.token)
token = Token(token=request.token, os=request.os, width=request.width, height=request.height);
token.put();
push_to(token, u"환영합니다! 벙커원 교회 주보 알림목록에 등록되었습니다!", 0)
return RegistTokenResult(result="OK")
APPLICATION = endpoints.api_server([B1CWeeklyApi,B1CWeeklyApiV2])