Skip to content

Commit

Permalink
reformats code with black
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelli Scheuble committed May 30, 2020
1 parent e244fab commit 199c6bf
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 33 deletions.
11 changes: 8 additions & 3 deletions src/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@
allow_headers=["*"],
)


@app.get("/")
async def root():
"""
Verifies the API is deployed, and links to the docs
"""
return HTMLResponse(
"""
"""
<h1>Kondoboard API</h1>
<p>Go to <a href="/docs">/docs</a> for documentation.</p>
""")
"""
)


@app.get("/all")
async def search_all():
Expand All @@ -34,6 +37,7 @@ async def search_all():
all = get_all_jobs()
return all


@app.post("/search/")
async def search_custom(search: Search):
"""
Expand All @@ -47,6 +51,7 @@ async def search_custom(search: Search):
all = get_all_jobs()
return all


@app.post("/track/")
async def search_by_track(track: Track):
"""
Expand All @@ -57,4 +62,4 @@ async def search_by_track(track: Track):
We will be updating this later
"""
all = get_all_jobs()
return all
return all
4 changes: 3 additions & 1 deletion src/app/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from pydantic import BaseModel


class Search(BaseModel):
search: str
location: str = None


class Track(BaseModel):
track: str
location: str = None
location: str = None
48 changes: 24 additions & 24 deletions src/app/queries.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import requests
import requests
import json
import os

es_name = os.environ["ES_NAME"]
es_pass = os.environ["ES_PASS"]
es_endpoint = os.environ["ES_ENDPOINT"]


def connect(query):
"""
Defines elasticsearch connection
Expand All @@ -18,44 +19,43 @@ def connect(query):
Elasticsearch response in json format
"""
uri = f"https://{es_name}:{es_pass}@{es_endpoint}/jobs/_search"
headers ={"Content-Type": "application/json"}
headers = {"Content-Type": "application/json"}

response = requests.get(uri, headers=headers, data=query)
return response.json()


def reformat(response_query):
"""
Reformats elasticsearch query to remove extra information
"""

data = list()
for hit in response_query['hits']['hits']:
data.append({
'id': hit['_id'],
'source_url' : hit['_source']['post_url'],
'title': hit['_source']['title'],
'company': hit['_source']['company'],
'description': hit['_source']['description'],
'date_published': hit['_source']['publication_date'],
'location_city': hit['_source']['location_city'],
'location_state': hit['_source']['location_state'],
'geo_locat': hit['_source']['location_point']})

return {'jobs':data}
for hit in response_query["hits"]["hits"]:
data.append(
{
"id": hit["_id"],
"source_url": hit["_source"]["post_url"],
"title": hit["_source"]["title"],
"company": hit["_source"]["company"],
"description": hit["_source"]["description"],
"date_published": hit["_source"]["publication_date"],
"location_city": hit["_source"]["location_city"],
"location_state": hit["_source"]["location_state"],
"geo_locat": hit["_source"]["location_point"],
}
)

return {"jobs": data}


def get_all_jobs():
"""Simple Elasticsearch query that will return all jobs"""

# define query
query = json.dumps({
"query": {
"match_all": {}
}
})

query = json.dumps({"query": {"match_all": {}}})

response = connect(query)
reformatted = reformat(response)

return reformatted

return reformatted
2 changes: 1 addition & 1 deletion src/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
@pytest.fixture(scope="module")
def test_app():
client = TestClient(app)
yield client # testing happens here
yield client # testing happens here
12 changes: 10 additions & 2 deletions src/tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from starlette.testclient import TestClient


from app.main import app

client = TestClient(app)
Expand All @@ -10,14 +11,21 @@ def test_ping(test_app):
assert response.status_code == 404
# assert response.json() == {"ping": "pong!"}


def test_all(test_app):
response = test_app.get("/all")
assert response.status_code ==200
assert response.status_code == 200


# def test_track(test_app):
# response = test_app.post("/track/")
# assert response.status_code == 200


# def test_track(test_app):
# response = test_app.post("/track/{track}")
# assert response.status_code ==200

# def test_custom_search(test_app):
# response = test_app.post("/search/{search}/location/{location}")
# assert response.status_code ==200
# assert response.status_code ==200
4 changes: 2 additions & 2 deletions startup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# startup.py
# startup.py
# points gunicorn at our main.py within the src/app directory
from src.app.main import app
from src.app.main import app

0 comments on commit 199c6bf

Please sign in to comment.