Skip to content

Commit 0da4a8f

Browse files
authored
Merge pull request #71 from kjaymiller/black-api
runs black on v4/API
2 parents effb6bd + 8bdfe8c commit 0da4a8f

File tree

9 files changed

+238
-161
lines changed

9 files changed

+238
-161
lines changed

.vscode/extensions.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"recommendations": [
3+
"ms-azuretools.vscode-azurefunctions",
4+
"ms-python.python"
5+
]
6+
}

.vscode/launch.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Attach to Python Functions",
6+
"type": "python",
7+
"request": "attach",
8+
"port": 9091,
9+
"preLaunchTask": "func: host start"
10+
}
11+
]
12+
}

.vscode/settings.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"azureFunctions.projectSubpath": "search-website-functions-v4/api",
3+
"azureFunctions.deploySubpath": "search-website-functions-v4/api",
4+
"azureFunctions.scmDoBuildDuringDeployment": true,
5+
"azureFunctions.pythonVenv": ".venv",
6+
"azureFunctions.projectLanguage": "Python",
7+
"azureFunctions.projectRuntime": "~4",
8+
"debug.internalConsoleOptions": "neverOpen"
9+
}

.vscode/tasks.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"type": "func",
6+
"command": "host start",
7+
"problemMatcher": "$func-python-watch",
8+
"isBackground": true,
9+
"dependsOn": "pip install (functions)",
10+
"options": {
11+
"cwd": "${workspaceFolder}/search-website-functions-v4/api"
12+
}
13+
},
14+
{
15+
"label": "pip install (functions)",
16+
"type": "shell",
17+
"osx": {
18+
"command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
19+
},
20+
"windows": {
21+
"command": "${config:azureFunctions.pythonVenv}\\Scripts\\python -m pip install -r requirements.txt"
22+
},
23+
"linux": {
24+
"command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
25+
},
26+
"problemMatcher": [],
27+
"options": {
28+
"cwd": "${workspaceFolder}/search-website-functions-v4/api"
29+
}
30+
}
31+
]
32+
}

search-website-functions-v4/api/Lookup/__init__.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@
1212
key = environment_vars["search_api_key"]
1313

1414
# Your index name
15-
index_name = 'good-books'
15+
index_name = "good-books"
1616

1717
# Create Azure SDK client
1818
search_client = SearchClient(endpoint, index_name, AzureKeyCredential(key))
1919

20+
2021
def main(req: func.HttpRequest) -> func.HttpResponse:
2122

2223
# http://localhost:7071/api/Lookup?id=100
23-
docid = req.params.get('id')
24+
docid = req.params.get("id")
2425

2526
if docid:
2627
logging.info(f"/Lookup id = {docid}")
2728
returnedDocument = search_client.get_document(key=docid)
28-
29+
2930
full_response = {}
30-
full_response["document"]=returnedDocument
31-
32-
return func.HttpResponse(body=json.dumps(full_response), mimetype="application/json", status_code=200)
33-
else:
31+
full_response["document"] = returnedDocument
32+
3433
return func.HttpResponse(
35-
"No doc id param found.",
36-
status_code=200
34+
body=json.dumps(full_response), mimetype="application/json", status_code=200
3735
)
36+
else:
37+
return func.HttpResponse("No doc id param found.", status_code=200)

search-website-functions-v4/api/Search/__init__.py

Lines changed: 80 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
key = environment_vars["search_api_key"]
1313

1414
# Your index name
15-
index_name = 'good-books'
15+
index_name = "good-books"
1616

1717
# Create Azure SDK client
1818
search_client = SearchClient(endpoint, index_name, AzureKeyCredential(key))
@@ -22,12 +22,12 @@ def read_facets(facetsString):
2222
facets = facetsString.split(",")
2323
output = {}
2424
for x in facets:
25-
if(x.find('*') != -1):
26-
newVal = x.replace('*','')
27-
output[newVal]='array'
28-
else:
29-
output[x]='string'
30-
25+
if x.find("*") != -1:
26+
newVal = x.replace("*", "")
27+
output[newVal] = "array"
28+
else:
29+
output[x] = "string"
30+
3131
return output
3232

3333

@@ -36,107 +36,112 @@ def create_filter_expression(filter_list, facets):
3636
i = 0
3737
filter_expressions = []
3838
return_string = ""
39-
separator = ' and '
39+
separator = " and "
4040

41-
while (i < len(filter_list)) :
41+
while i < len(filter_list):
4242
field = filter_list[i]["field"]
4343
value = filter_list[i]["value"]
44-
45-
if (facets[field] == 'array'):
46-
print('array')
47-
filter_expressions.append(f'{field}/any(t: search.in(t, \'{value}\', \',\'))')
48-
else :
49-
print('value')
50-
filter_expressions.append(f'{field} eq \'{value}\'')
51-
44+
45+
if facets[field] == "array":
46+
print("array")
47+
filter_expressions.append(f"{field}/any(t: search.in(t, '{value}', ','))")
48+
else:
49+
print("value")
50+
filter_expressions.append(f"{field} eq '{value}'")
51+
5252
i += 1
53-
54-
53+
5554
return_string = separator.join(filter_expressions)
5655

5756
return return_string
5857

58+
5959
def new_shape(docs):
60-
60+
6161
old_api_shape = list(docs)
62-
63-
count=0
62+
63+
count = 0
6464
client_side_expected_shape = []
65-
65+
6666
for item in old_api_shape:
67-
67+
6868
new_document = {}
69-
new_document["score"]=item["@search.score"]
70-
new_document["highlights"]=item["@search.highlights"]
69+
new_document["score"] = item["@search.score"]
70+
new_document["highlights"] = item["@search.highlights"]
7171

7272
new_shape = {}
73-
new_shape["id"]=item["id"]
74-
new_shape["goodreads_book_id"]=item["goodreads_book_id"]
75-
new_shape["best_book_id"]=item["best_book_id"]
76-
new_shape["work_id"]=item["work_id"]
77-
new_shape["books_count"]=item["books_count"]
78-
new_shape["isbn"]=item["isbn"]
79-
new_shape["isbn13"]=item["isbn13"]
80-
new_shape["authors"]=item["authors"]
81-
new_shape["original_publication_year"]=item["original_publication_year"]
82-
new_shape["original_title"]=item["original_title"]
83-
new_shape["title"]=item["title"]
84-
new_shape["language_code"]=item["language_code"]
85-
new_shape["average_rating"]=item["average_rating"]
86-
new_shape["ratings_count"]=item["ratings_count"]
87-
new_shape["work_ratings_count"]=item["work_ratings_count"]
88-
new_shape["work_text_reviews_count"]=item["work_text_reviews_count"]
89-
new_shape["ratings_1"]=item["ratings_1"]
90-
new_shape["ratings_2"]=item["ratings_2"]
91-
new_shape["ratings_3"]=item["ratings_3"]
92-
new_shape["ratings_4"]=item["ratings_4"]
93-
new_shape["ratings_5"]=item["ratings_5"]
94-
new_shape["image_url"]=item["image_url"]
95-
new_shape["small_image_url"]=item["small_image_url"]
96-
97-
new_document["document"]=new_shape
98-
73+
new_shape["id"] = item["id"]
74+
new_shape["goodreads_book_id"] = item["goodreads_book_id"]
75+
new_shape["best_book_id"] = item["best_book_id"]
76+
new_shape["work_id"] = item["work_id"]
77+
new_shape["books_count"] = item["books_count"]
78+
new_shape["isbn"] = item["isbn"]
79+
new_shape["isbn13"] = item["isbn13"]
80+
new_shape["authors"] = item["authors"]
81+
new_shape["original_publication_year"] = item["original_publication_year"]
82+
new_shape["original_title"] = item["original_title"]
83+
new_shape["title"] = item["title"]
84+
new_shape["language_code"] = item["language_code"]
85+
new_shape["average_rating"] = item["average_rating"]
86+
new_shape["ratings_count"] = item["ratings_count"]
87+
new_shape["work_ratings_count"] = item["work_ratings_count"]
88+
new_shape["work_text_reviews_count"] = item["work_text_reviews_count"]
89+
new_shape["ratings_1"] = item["ratings_1"]
90+
new_shape["ratings_2"] = item["ratings_2"]
91+
new_shape["ratings_3"] = item["ratings_3"]
92+
new_shape["ratings_4"] = item["ratings_4"]
93+
new_shape["ratings_5"] = item["ratings_5"]
94+
new_shape["image_url"] = item["image_url"]
95+
new_shape["small_image_url"] = item["small_image_url"]
96+
97+
new_document["document"] = new_shape
98+
9999
client_side_expected_shape.append(new_document)
100-
100+
101101
return list(client_side_expected_shape)
102102

103+
103104
def main(req: func.HttpRequest) -> func.HttpResponse:
104105

105106
# variables sent in body
106107
req_body = req.get_json()
107-
q = req_body.get('q')
108-
top = req_body.get('top') or 8
109-
skip = req_body.get('skip') or 0
110-
filters = req_body.get('filters') or []
108+
q = req_body.get("q")
109+
top = req_body.get("top") or 8
110+
skip = req_body.get("skip") or 0
111+
filters = req_body.get("filters") or []
111112

112113
facets = environment_vars["search_facets"]
113114
facetKeys = read_facets(facets)
114-
115-
filter=""
116-
if(len(filters)):
115+
116+
filter = ""
117+
if len(filters):
117118
filter = create_filter_expression(filters, facetKeys)
118119

119120
if q:
120121
logging.info(f"/Search q = {q}")
121-
122-
search_results = search_client.search(search_text=q, top=top,skip=skip, facets=facetKeys, filter=filter, include_total_count=True)
123-
122+
123+
search_results = search_client.search(
124+
search_text=q,
125+
top=top,
126+
skip=skip,
127+
facets=facetKeys,
128+
filter=filter,
129+
include_total_count=True,
130+
)
131+
124132
returned_docs = new_shape(search_results)
125133
returned_count = search_results.get_count()
126134
returned_facets = search_results.get_facets()
127-
135+
128136
# format the React app expects
129137
full_response = {}
130-
131-
full_response["count"]=search_results.get_count()
132-
full_response["facets"]=search_results.get_facets()
133-
full_response["results"]=returned_docs
134-
135-
136-
return func.HttpResponse(body=json.dumps(full_response), mimetype="application/json",status_code=200)
137-
else:
138+
139+
full_response["count"] = search_results.get_count()
140+
full_response["facets"] = search_results.get_facets()
141+
full_response["results"] = returned_docs
142+
138143
return func.HttpResponse(
139-
"No query param found.",
140-
status_code=200
144+
body=json.dumps(full_response), mimetype="application/json", status_code=200
141145
)
142-
146+
else:
147+
return func.HttpResponse("No query param found.", status_code=200)

search-website-functions-v4/api/Suggest/__init__.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,42 @@
77

88
environment_vars = azure_config()
99

10-
#curl --header "Content-Type: application/json" \
10+
# curl --header "Content-Type: application/json" \
1111
# --request POST \
1212
# --data '{"q":"code","top":"5", "suggester":"sg"}' \
1313
# http://localhost:7071/api/Suggest
1414

1515
# Set Azure Search endpoint and key
1616
service_name = environment_vars["search_service_name"]
17-
endpoint = f'https://{service_name}.search.windows.net'
17+
endpoint = f"https://{service_name}.search.windows.net"
1818
key = environment_vars["search_api_key"]
1919

2020
# Your index name
21-
index_name = 'good-books'
21+
index_name = "good-books"
2222

2323
# Create Azure SDK client
2424
search_client = SearchClient(endpoint, index_name, AzureKeyCredential(key))
2525

26+
2627
def main(req: func.HttpRequest) -> func.HttpResponse:
2728

2829
# variables sent in body
2930
req_body = req.get_json()
30-
q = req_body.get('q')
31-
top = req_body.get('top')
32-
suggester = req_body.get('suggester')
31+
q = req_body.get("q")
32+
top = req_body.get("top")
33+
suggester = req_body.get("suggester")
3334

3435
if q:
3536
logging.info(f"/Suggest q = {q}")
3637
suggestions = search_client.suggest(search_text=q, suggester_name="sg", top=5)
37-
38+
3839
# format the React app expects
3940
full_response = {}
40-
full_response["suggestions"]=suggestions
41+
full_response["suggestions"] = suggestions
4142
print(suggestions)
42-
43-
return func.HttpResponse(body=json.dumps(full_response), mimetype="application/json",status_code=200)
44-
else:
43+
4544
return func.HttpResponse(
46-
"No query param found.",
47-
status_code=200
48-
)
45+
body=json.dumps(full_response), mimetype="application/json", status_code=200
46+
)
47+
else:
48+
return func.HttpResponse("No query param found.", status_code=200)
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import os
22

3+
34
def azure_config():
4-
5-
configs={}
6-
configs["search_facets"]= os.environ.get("SearchFacets","")
7-
configs["search_index_name"]=os.environ.get("SearchIndexName","")
8-
configs["search_service_name"]=os.environ.get("SearchServiceName","")
9-
configs["search_api_key"]=os.environ.get("SearchApiKey","")
10-
5+
6+
configs = {}
7+
configs["search_facets"] = os.environ.get("SearchFacets", "")
8+
configs["search_index_name"] = os.environ.get("SearchIndexName", "")
9+
configs["search_service_name"] = os.environ.get("SearchServiceName", "")
10+
configs["search_api_key"] = os.environ.get("SearchApiKey", "")
11+
1112
return configs

0 commit comments

Comments
 (0)