using EVE Python api for request and response - python

I am trying to use the EVE api with python 3. This is for the first time I am trying to use the API. So what I am trying to do and achieve is given below:
I have a list of name of people. for example:
Adam Gilchrist
Adam Barbar
Adam lobiof
Jaffer Wilson
Janet Wilson
Jane Cold
And many others. I am using the fuzzywuzzy python library. I have loaded these names into an array and using the library function for predicting the approx match of the string from the names using their email address names. For example:
adam.barbar#example.com is the exampe I have taken. Now, I have written the code for match the string adam.barbar with the name list that I have mentioned above.
I get the approx answers as expected.
Now where is the problem one might think. So here it is:
I want to fetch the response from the API as:
http://127.0.0.1/people/?email=adam.barbar#example.com
and get the response as the answer of fuzzywuzzy library as json to display on the screen.
I am not using any Database as such and all the names are available in the file format currently.
I have search and research about the usage of the EVE API, but could not find the usage as per my requirement. Where ever I searched for the solution I came across MongoDB or any other database. My requirement is database less.
So I want to know in the above mentioned condition, What need to be done if I want to use the Eve API

I am not using any Database as such and all the names are available in the file format currently.
As you are not using a database, you can use plain flask to solve your problem. It can easily return json for any request with the aptly named jsonify:
from flask import Flask, jsonify, make_response, request
def your_normal_code_here(email):
return something
app = Flask(__name__)
#app.route('/api/v1.0/people', methods=['GET'])
def people_api():
email = request.args.get('email')
if email is None:
make_response(jsonify({'error': 'Missing email parameter'}), 400)
return jsonify(your_normal_code_here(email))
For more robustness and completeness of your API, try using a lightweight framework like flask-restful.

Related

GET and POST Function App Function/Host Keys Using Python (Azure Government)

I am attempting to retrieve and add function/host keys for an Azure Government function app via Python. I am currently working with the information from this question and the corresponding API page. While these are not specific to Azure Government, I would think the process would be similar after updating the URLs to the Azure Government versions. However, I am receiving the error "No route registered for '/api/functions/admin/token'" when running the jwt part of the given code. Is this approach feasible for what I am trying to do?
I also found somewhere that I instead might want to try a GET request like this:
resp = requests.get("https://management.usgovcloudapi.net/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Web/sites/<function-app-name>/functions/admin/masterkey?api-version=20XX-XX-XX", headers={"Authorization": f"Bearer {something}"})
This gives me the error "{"error":{"code":"InvalidAuthenticationToken","message":"The access token is invalid."}}", though. If this is indeed the correct approach, then what format should the Bearer token take?
Bit late answering but it may be useful for someone else in the future, it took me a while to find out how to do this.
If you want to retrieve the keys of a specific function within a function app then you can use list_function_keys() function from the Python SDK
Working with the Az management API directly may be a bit annoying and since the Azure CLI is written in Python whatever operation you do with the CLI you can do it directly in a Python script.
Here's an example of how you can retrieve the keys
from azure.identity import DefaultAzureCredential
from azure.mgmt.web import WebSiteManagementClient
# Your subscription ID
SUB_ID = "00000000-0000-0000-0000-000000000000"
fn_name = "some_function" # Name of your function
app_name = "some_app" # Name of your site/function app
rg_name = "some_rg" # Resource group name to which the function belongs
web_client = WebSiteManagementClient(subscription_id=SUB_ID, credential=DefaultAzureCredential())
keys = web_client.web_apps.list_function_keys(rg_name, app_name, fn_name)
# Your keys will be accessible in the additional_properties param
print(keys.additional_properties)
Hope it helps! I'm new on Azure so if I'm doing something wrong, please don't hesitate to point out my mistake and share your correction

flask app searching logic for similar word

I am a flask beginner and recently I am doing my first project which is searching cocktails using flask and API.
So, here is the snippet of my flask code
#app.route('/search' ,methods=[ "POST"])
def search():
value = request.form['search']
cocktailAPI = requests.get(
f'https://www.thecocktaildb.com/api/json/v1/1/search.php?s={value}'
)
value = request.form['search']
print(value)
return f'ff'
My thought is like searching things on google, no matter what I am typing google can find the most relative information for me.
Is it possible to do that? for instance, if I type 'margarite' in which 'margarita' is correct, the flask will try to find the correct word in my API and give me a response.
Thank you!
What you're trying to achieve is called fuzzy search, and is a whole problem on its own.
This is not so hard though, considering that the list of existing cocktails is quite already known.
So here a way you could follow:
Build a list of existing cocktail names; you could for instance retrieve the whole list through Thecocktaildb API (though it requires a contribution), or create a list manually (as this is not likely to evolve much)
Once user enters a name, return the closest cocktail names from your list, using some distance metrics of your own (Wikipedia article provides some clues)
Request Thecocktaildb API for this cocktail.
Happy coding!

HTML Request from Denticon

I am attempting to query a cloud database using the requests package. This is from Denticon, a dentistry software that my office uses to schedule patients and store information on the cloud about them and their dental history. Eventually, I would like to process and analyze the results of the query in pandas but I am having issues getting query to work at all. I admit I am a novice at using this sort of API and querying in general, so show some grace if I am doing this absolutely wrong.
By their own documentation, the makers of Denticon say you must query using a html request. When attempting to use 'request.get()' I get an error message that this method in not supported for the request when using it on a url for a specific query.
Here is some short documentation from their own website that could help fill some gaps:
Denticon API
Denticon API Getting Started
Denticon Patient APIs
In my case I am trying to query a list of patients at a specific office/location. This is what I would think to try:
import requests
apiKey = {"API-AUTH-KEY":"AAC6DB7A-5A66-4EBC-B694-D6BCD99881CB",
"API-VENDOR-KEY":"BCF756D4-DCE6-4F2B-BAAE-7679D87037A7",
"PGID":"1"}
requests.get('https://dev-api.denticon.com/v2/api/Patient/AllPatients/', headers=apiKey)
(As a short disclaimer: these are practice api keys that Denticon provides. They do not allow you query any actual patient data, just to practice using the API)
At this point is where I get the "method not supported" response for request.get
The error means that you cannot call the API using requests.get. Try to do it using requests.post.
import requests
apiKey = {"API-AUTH-KEY":"AAC6DB7A-5A66-4EBC-B694-D6BCD99881CB",
"API-VENDOR-KEY":"BCF756D4-DCE6-4F2B-BAAE-7679D87037A7",
"PGID":"1"}
json = {"OID":"","PAGEINDEX":"1"}
requests.post('https://dev-api.denticon.com/v2/api/Patient/AllPatients/', headers=apiKey, json=json)

How to get a result from a collection on MongoDB using FastAPI?

I'm trying to create an API using FastAPI and MongoDB that has just a GET method and I have no clue for how to do this.
My application already populated the MongoDB with a huge data base using an txt file that is converted to csv and include all data on MONGODB collections and what I'm trying to do is: Give a zip code in the endpoint route(e.g. localhost:8000/cep/123456789) and my application will return all information from my MongoDB collections that is part of that zip code like street, city.
#app.get('/cep/{cep}')
def find_ceps():
mongo_uri = pymongo.MongoClient("mongodb://root:example#localhost:27017/")
db_name = mongo_uri["cep4free"]
col = db_name["cep4free"]
for i in col.find({}):
return i
cep = find_ceps()
return cep
I tried to do this, but it isn't work and I have no idea how to return this using a GET method.
I'm just new on Python and FastAPI as well and I'm coding this API for practice and learn. I would be glad if anyone could help.
Thanks!
First thing you need to do is separate out your db initialization from your actual route. FastAPI shows an example of how to do that, just replace the Couch initialization lines with the ones you need for Mongo.
Second, you (usually) will have a database folder with code you'll be using in your route. This is part of a layered architecture approach. From here you'd either define a schema, or just call a method from the defined class in the data layer you just created.
For example (of the more simplified approach) we would call cep_db.findAll() in the route. This method could live in a class named Cep in the file database/cep.py.
Finally, see this project https://github.com/markqiu/fastapi-mongodb-realworld-example-app for a working example.

Use django to expose python functions on the web

I have not worked with Django seriously and my only experience is the tutorials on their site.
I am trying to write my own application now, and what I want is to have some sort of API. My idea is that I will later be able to use it with a client written in any other language.
I have the simplest of all apps, a model that has a name and surname field.
So the idea is that I can now write an app lets say in c++ that will send two strings to my Django app so they can be saved in the database as name, surname respectively.
What I know until now is to create a form so a user can enter that information, or have the information in the url, and of curse adding them myself from the admin menu.
What I want though is some other better way, maybe creating a packet that contains that data. Later my client sends this data to my Django webpage and it will extract the info and save it as needed. But I do not know how to do this.
If my suggested method is a good idea, then I would like an example of how this is done. If not the I would like suggestions for possible things I could try out.
Typically, as stated by #DanielRoseman, you certainly want to:
Create a REST API to get data from another web site
Get data, typically in JSON or XML, that will contain all the required data (name and surname)
In the REST controller, Convert this data to the Model and save the Model to the database
Send an answer.
More information here: http://www.django-rest-framework.org/

Categories