Skip to content

Commit 5d4ec30

Browse files
committed
base code
0 parents  commit 5d4ec30

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dmp_2/__pycache__/*
2+
3+
dmp_2/.env
4+
env/*

dmp_2/app.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from flask import Flask, jsonify
2+
from db import SupabaseInterface
3+
from collections import defaultdict
4+
5+
app = Flask(__name__)
6+
7+
8+
@app.route('/api/greeting', methods=['GET'])
9+
def greeting():
10+
response = {
11+
'message': 'Hello, welcome to my API!'
12+
}
13+
return jsonify(response)
14+
15+
@app.route('/api/get-data', methods=['GET'])
16+
def get_data():
17+
# Fetch data from Supabase
18+
try:
19+
import pdb;pdb.set_trace()
20+
response = SupabaseInterface().get_instance().client.table('dmp_pr_updates').select('*').execute()
21+
data = response.data
22+
return jsonify(data)
23+
except Exception as e:
24+
return jsonify({'error': str(e)}), 500
25+
26+
27+
def group_by_owner(data):
28+
grouped_data = defaultdict(list)
29+
for record in data:
30+
owner = record['owner']
31+
grouped_data[owner].append(record)
32+
return grouped_data
33+
34+
#DMP - CMS API's
35+
36+
@app.route('/api/issues', methods=['GET'])
37+
def get_issues():
38+
try:
39+
response = SupabaseInterface().get_instance().client.table('dmp_issue_updates').select('*').execute()
40+
data = response.data
41+
grouped_data = group_by_owner(data)
42+
return jsonify(grouped_data)
43+
except Exception as e:
44+
return jsonify({'error': str(e)}), 500
45+
46+
@app.route('/api/issues/<owner>', methods=['GET'])
47+
def get_issues_by_owner(owner):
48+
# Fetch data from Supabase
49+
try:
50+
import pdb;pdb.set_trace()
51+
response = SupabaseInterface().get_instance().client.table('dmp_issue_updates').select('*').eq('owner', owner).execute()
52+
53+
if not response.data:
54+
return jsonify({'error': "No data found"}), 500
55+
56+
data = response.data
57+
return jsonify(data)
58+
59+
except Exception as e:
60+
return jsonify({'error': str(e)}), 500
61+
62+
63+
@app.route('/api/issues/<owner>/<issue>', methods=['GET'])
64+
def get_issues_by_owner_id(owner,issue):
65+
# Fetch data from Supabase
66+
try:
67+
response = SupabaseInterface().get_instance().client.table('dmp_issue_updates').select('*').eq('owner', owner).eq('issue_number', issue).execute()
68+
69+
if not response.data:
70+
return jsonify({'error': "No data found"}), 500
71+
72+
data = response.data
73+
return jsonify(data)
74+
75+
except Exception as e:
76+
return jsonify({'error': str(e)}), 500
77+
78+
if __name__ == '__main__':
79+
app.run(debug=True)

dmp_2/db.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import os, sys
2+
from typing import Any
3+
from supabase import create_client, Client
4+
from supabase.lib.client_options import ClientOptions
5+
from abc import ABC, abstractmethod
6+
7+
client_options = ClientOptions(postgrest_client_timeout=None)
8+
9+
10+
11+
class SupabaseInterface():
12+
13+
_instance = None
14+
15+
def __init__(self):
16+
if not SupabaseInterface._instance:
17+
18+
# Load environment variables
19+
from dotenv import load_dotenv
20+
load_dotenv()
21+
22+
SUPABASE_URL = os.getenv('SUPABASE_URL')
23+
SUPABASE_KEY = os.getenv('SUPABASE_KEY')
24+
self.client: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
25+
SupabaseInterface._instance = self
26+
else:
27+
SupabaseInterface._instance = self._instance
28+
29+
30+
31+
@staticmethod
32+
def get_instance():
33+
# Static method to retrieve the singleton instance
34+
if not SupabaseInterface._instance:
35+
# If no instance exists, create a new one
36+
SupabaseInterface._instance = SupabaseInterface()
37+
return SupabaseInterface._instance
38+
39+
40+
def readAll(self, table):
41+
data = self.client.table(f"{table}").select("*").execute()
42+
return data.data
43+
44+
def add_data(self, data,table_name):
45+
data = self.client.table(table_name).insert(data).execute()
46+
return data.data
47+
48+
def add_data_filter(self, data, table_name):
49+
# Construct the filter based on the provided column names and values
50+
filter_data = {column: data[column] for column in ['dmp_id','issue_number','owner']}
51+
52+
# Check if the data already exists in the table based on the filter
53+
existing_data = self.client.table(table_name).select("*").eq('dmp_id',data['dmp_id']).execute()
54+
55+
# If the data already exists, return without creating a new record
56+
if existing_data.data:
57+
return "Data already exists"
58+
59+
# If the data doesn't exist, insert it into the table
60+
new_data = self.client.table(table_name).insert(data).execute()
61+
return new_data.data

dmp_2/requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Flask==3.0.3
2+
httpx==0.27.0
3+
python-dotenv==1.0.1
4+
supabase==2.4.5

0 commit comments

Comments
 (0)