Merge pull request #10 from Team-Deep-Impact/sentry-backend

add the NASA Sentry api call to the backend
This commit is contained in:
Dan
2024-05-19 01:11:46 -04:00
committed by GitHub
21 changed files with 92 additions and 13 deletions

View File

@@ -0,0 +1,6 @@
SECRET_KEY=<"Your Django Secret Key">
SENTRY_API_KEY=<"Your NASA Sentry API Key">
OPENAI_API_KEY=<"Your OpenAI API Key">
HOST_CLIENT=<"Your FrontEnd URL">
ALLOWED_HOSTS=<"Your Server URL">
DEBUG=<"True for Deployment | False for Development">

2
backend/deep_impact_proj/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
.env
.venv

View File

@@ -1,8 +1,7 @@
from django.urls import path from django.urls import path
from .views import ( from .views import Sentry, OpenAI
Sentry,
OpenAI,
)
urlpatterns = [ urlpatterns = [
path('sentry/', Sentry.as_view(), name='sentry'), path('sentry/', Sentry.as_view(), name='sentry'),

View File

@@ -0,0 +1,23 @@
from django.core.exceptions import ValidationError
import os
import requests
from rest_framework.response import Response
from rest_framework.status import (
HTTP_200_OK,
HTTP_204_NO_CONTENT,
HTTP_201_CREATED,
HTTP_400_BAD_REQUEST
)
sentry_api_key = os.environ.get("SENTRY_API_KEY")
def get_sentry():
try:
response = requests.get(f'https://api.nasa.gov/neo/rest/v1/neo/browse?api_key={sentry_api_key}')
response.raise_for_status()
data = response.json()
return Response(data, status=HTTP_200_OK)
except ValidationError as e:
print(e)
return Response(e, status=HTTP_400_BAD_REQUEST)

View File

@@ -1,8 +1,26 @@
from django.shortcuts import render from django.shortcuts import render
from django.core.exceptions import ValidationError
import os
import requests
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.status import (
HTTP_200_OK,
HTTP_204_NO_CONTENT,
HTTP_201_CREATED,
HTTP_400_BAD_REQUEST
)
from .utils import get_sentry
sentry_api_key = os.environ.get("SENTRY_API_KEY")
# Create your views here. # Create your views here.
class Sentry: class Sentry(APIView):
pass def get(self, request):
return get_sentry()
class OpenAI:
class OpenAI(APIView):
pass pass

Binary file not shown.

View File

@@ -11,6 +11,11 @@ https://docs.djangoproject.com/en/5.0/ref/settings/
""" """
import os import os
from pathlib import Path from pathlib import Path
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Build paths inside the project like this: BASE_DIR / 'subdir'. # Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent BASE_DIR = Path(__file__).resolve().parent.parent
@@ -20,12 +25,12 @@ BASE_DIR = Path(__file__).resolve().parent.parent
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/ # See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret! # SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-3(n0mvl^xk3rjak+runl7#wbx1vr1y+y17dber5zl5ab3x6_hy' SECRET_KEY = os.environ.get("SECRET_KEY")
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True DEBUG = os.environ.get("DEBUG")
ALLOWED_HOSTS = [] ALLOWED_HOSTS = [os.environ.get("ALLOWED_HOSTS"), "localhost"]
# Application definition # Application definition
@@ -37,12 +42,15 @@ INSTALLED_APPS = [
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'corsheaders',
'api_app', 'api_app',
'rest_framework',
] ]
MIDDLEWARE = [ MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware', 'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware', 'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware', 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware',
@@ -52,6 +60,12 @@ MIDDLEWARE = [
ROOT_URLCONF = 'deep_impact_proj.urls' ROOT_URLCONF = 'deep_impact_proj.urls'
CORS_ALLOWED_ORIGINS = [os.environ.get("HOST_CLIENT")]
TEMPLATES = [ TEMPLATES = [
{ {
'BACKEND': 'django.template.backends.django.DjangoTemplates', 'BACKEND': 'django.template.backends.django.DjangoTemplates',

View File

@@ -0,0 +1,17 @@
asgiref==3.8.1
certifi==2024.2.2
charset-normalizer==3.3.2
click==8.1.7
Django==5.0.6
django-cors-headers==4.2.0
djangorestframework==3.15.1
gunicorn==22.0.0
h11==0.14.0
idna==3.7
packaging==24.0
python-dotenv==1.0.1
requests==2.31.0
sqlparse==0.5.0
urllib3==2.2.1
uvicorn==0.29.0
whitenoise==6.6.0

View File

@@ -3,7 +3,7 @@ import { Stage, Layer, Circle, Text, Line } from 'react-konva';
import { Slider, Button } from '@mui/material'; import { Slider, Button } from '@mui/material';
import axios from 'axios'; import axios from 'axios';
const API_KEY = 'LF7i77oqghRiq54HEFJh991WgjHcKsETP9D5ofsg';
const EARTH_RADIUS_KM = 6371; // Earth's radius in kilometers const EARTH_RADIUS_KM = 6371; // Earth's radius in kilometers
const EARTH_DISPLAY_SCALE = 0.01; // Display scale for Earth const EARTH_DISPLAY_SCALE = 0.01; // Display scale for Earth
const ASTEROID_DISPLAY_SCALE = 0.3; // Display scale for asteroids const ASTEROID_DISPLAY_SCALE = 0.3; // Display scale for asteroids
@@ -37,9 +37,9 @@ function Scenario() {
const [timeStep, setTimeStep] = useState(100); // State for time step const [timeStep, setTimeStep] = useState(100); // State for time step
useEffect(() => { useEffect(() => {
const fetchAsteroids = async () => { const fetchAsteroids = async (self, request) => {
try { try {
const response = await axios.get(`https://api.nasa.gov/neo/rest/v1/neo/browse?api_key=${API_KEY}`); const response = await axios.get(`http://localhost:8000/api/sentry/`);
asteroidsData.current = response.data.near_earth_objects; asteroidsData.current = response.data.near_earth_objects;
setAsteroidData(0); // Set initial asteroid setAsteroidData(0); // Set initial asteroid
} catch (error) { } catch (error) {