Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KPMP-5387_use-measurement-protocol #17

Merged
merged 7 commits into from
Jul 31, 2024
Merged
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
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.*
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
FROM python:3.7.12-alpine3.14
WORKDIR /code
ENV FLASK_APP app.py
ENV FLASK_RUN_HOST 0.0.0.0
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers tzdata libffi-dev libc-dev
COPY app.py app.py
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
RUN pip3 install -U flask-cors
Expand Down
25 changes: 20 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from flask_cors import CORS
import mysql.connector
import logging
import requests
import json

app = Flask(__name__)
CORS(app)
Expand All @@ -15,6 +17,9 @@
minioSecretKey = os.environ.get('MINIO_SECRET_KEY')
s3Bucket = os.environ.get('BUCKET_NAME')
minioUrl = os.environ.get('MINIO_URL')
apiSecret = os.environ.get("API_SECRET")
ga4Id = os.environ.get("GA4_ID")
url = "https://www.google-analytics.com/mp/collect?measurement_id=" + ga4Id + "&api_secret=" + apiSecret
minioClient = Minio(minioUrl, access_key=minioAccessKey, secret_key=minioSecretKey, secure=False)
s3_client = boto3.client(
's3',
Expand All @@ -28,7 +33,7 @@

class MYSQLConnection:
def __init__(self):
logger.debug(
logger.info(
"Start: MYSQLConnection().__init__(), trying to load environment variables in docker"
)
self.host = None
Expand Down Expand Up @@ -95,14 +100,24 @@ def get_file_info_by_file_name(file_name):
(file_name,),
)


@app.route('/v1/file/download/<packageId>/<objectName>', methods=['GET'])
@app.route('/v1/file/download/<packageId>/<objectName>', methods=['POST', 'GET'])
def downloadFile(packageId, objectName):
result = get_file_info_by_file_name(objectName)
if result[0]["access"] == "open":
try:
objectNameFull = packageId + '/' + objectName
object = minioClient.get_object(s3Bucket, objectNameFull, request_headers=None)
payload = {
"client_id": "XXXXXXXXXX.YYYYYYYYYY",
"events": [
{
"name": "file_download",
"params": {
"file_name": objectName
}
}]
}
requests.post(url, json=payload, headers={"Content-Type": "application/json"})
return send_file(object, as_attachment=True, download_name=objectName)
except S3Error as err:
logger.error(err)
Expand All @@ -119,7 +134,7 @@ def downloadDerivedFileS3PS(packageId, objectName):
Params={'Bucket': s3Bucket, 'Key': objectNameFull},
ExpiresIn=3600)
except botocore.exceptions.ClientError as error:
logger.error(err)
logger.error(error)
except botocore.exceptions.ParamValidationError as error:
logger.error(err)
logger.error(error)

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ minio
boto3
flask-cors
mysql-connector-python
requests
Loading