2018-10-23 17:24:38 +03:00
|
|
|
import base64
|
|
|
|
import io
|
2018-10-23 17:41:11 +03:00
|
|
|
import os
|
2018-10-23 15:52:25 +03:00
|
|
|
|
|
|
|
from flask import Flask, make_response, request
|
|
|
|
from flask.json import jsonify
|
|
|
|
|
2018-10-23 17:24:38 +03:00
|
|
|
import boto3
|
|
|
|
import httplib2
|
|
|
|
|
|
|
|
from apiclient.discovery import build
|
|
|
|
from oauth2client.service_account import ServiceAccountCredentials
|
2018-10-23 15:52:25 +03:00
|
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
2018-10-23 17:41:11 +03:00
|
|
|
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY')
|
2018-10-23 15:52:25 +03:00
|
|
|
|
|
|
|
|
2018-10-23 17:24:38 +03:00
|
|
|
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
|
2018-10-23 15:52:25 +03:00
|
|
|
|
|
|
|
|
|
|
|
def get_reviews_service():
|
|
|
|
"""Create new instance of google play API service."""
|
2018-10-23 17:24:38 +03:00
|
|
|
|
|
|
|
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', 'v2', http=http, cache_discovery=False)
|
2018-10-23 15:52:25 +03:00
|
|
|
return service.reviews()
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/reviews', methods=['GET'])
|
|
|
|
def get_reviews():
|
|
|
|
"""Get playstore reviews. Proxy playstore API requests."""
|
2018-10-23 17:24:38 +03:00
|
|
|
|
2018-10-23 15:52:25 +03:00
|
|
|
packageName = request.args.get('packageName', None)
|
2018-10-24 11:52:38 +03:00
|
|
|
nextPageToken = request.args.get('token', None)
|
2018-10-23 15:52:25 +03:00
|
|
|
|
|
|
|
if not packageName:
|
|
|
|
content = jsonify(msg='Missing `packageName` from request query')
|
|
|
|
return make_response(content, 400)
|
|
|
|
|
|
|
|
reviews_service = get_reviews_service()
|
2018-10-25 16:10:55 +03:00
|
|
|
app.logger.info('Playstore API: GET list')
|
2018-10-23 15:52:25 +03:00
|
|
|
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."""
|
|
|
|
|
|
|
|
return make_response(jsonify(msg='POST method not implemented'), 501)
|