-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.py
119 lines (89 loc) · 3.56 KB
/
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
import base64
import io
import os
from flask import Flask, make_response, request
from flask.json import jsonify
import boto3
import httplib2
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
app = Flask(__name__)
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY')
def get_credentials():
"""Fetch playstore credentials from SSM"""
# Fetch credentials from AWS SSM
ssm = boto3.client('ssm')
key_response = ssm.get_parameter(
Name='SumoPlaystoreReviewsKey', WithDecryption=True)
account_response = ssm.get_parameter(
Name='SumoPlaystoreReviewsAccount', WithDecryption=True)
# Parse SSM responses
account = account_response['Parameter']['Value']
key = io.BytesIO(base64.b64decode(key_response['Parameter']['Value']))
credentials = {
'key': key,
'account': account
}
return credentials
def get_reviews_service():
"""Create new instance of google play API service."""
scope = 'https://www.googleapis.com/auth/androidpublisher'
credentials = get_credentials()
service_credentials = ServiceAccountCredentials.from_p12_keyfile_buffer(
credentials['account'], credentials['key'], scopes=scope
)
http = httplib2.Http()
http = service_credentials.authorize(http)
service = build('androidpublisher', 'v3', http=http, cache_discovery=False)
return service.reviews()
@app.route('/reviews', methods=['GET'])
def get_reviews():
"""Get playstore reviews. Proxy playstore API requests."""
packageName = request.args.get('packageName', None)
nextPageToken = request.args.get('token', None)
if not packageName:
content = jsonify(msg='Missing `packageName` from request query')
return make_response(content, 400)
reviews_service = get_reviews_service()
app.logger.info('Playstore API: GET list')
query = reviews_service.list(packageName=packageName, token=nextPageToken)
return make_response(jsonify(query.execute()), 200)
@app.route('/reviews', methods=['POST'])
def post_reviews():
"""Post playstore reviews."""
# Flag to enable/disable playstore upload
UPLOADS_ENABLED = os.getenv('SUMO_PLAYSTORE_UPLOADS_ENABLED', False)
packageName = request.args.get('packageName', None)
reviewId = request.args.get('reviewId', None)
replyText = request.args.get('text', None)
if not packageName:
content = jsonify(msg='Missing `packageName` from request')
return make_response(content, 400)
if not reviewId:
content = jsonify(msg='Missing `review_id` from request')
return make_response(content, 400)
if not replyText:
content = jsonify(msg='Missing `text` from request')
return make_response(content, 400)
payload = {
'packageName': packageName,
'reviewId': reviewId,
'replyText': replyText
}
app.logger.info('Playstore API: POST')
app.logger.info('Playstore API - upload - call params: {}'.format(payload))
app.logger.info('Playstore API - uploads enabled: {}'.format(UPLOADS_ENABLED))
if UPLOADS_ENABLED:
reviews_service = get_reviews_service()
body = {
'replyText': replyText
}
query = reviews_service.reply(
packageName=packageName,
reviewId=reviewId,
body=body
)
response = query.execute()
app.logger.info('Playstore API - response: {}'.format(response))
return make_response(jsonify(response), 200)
return make_response(jsonify(msg='POST method not implemented'), 501)