Upload a file in Swagger and receive at Flask backend - python

I'm trying to upload a file using Swagger and Flask. I've the following configuration for swagger.
"/user/register/": {
"post": {
"tags": ["user"],
"summary": "Register a new user",
"description": "",
"operationId": "registerUser",
"consumes": ["application/json"],
"produces": ["application/json"],
"parameters": [{
"in": "body",
"name": "body",
"description": "User object that needs to be added.",
"required": true,
"schema": {
"$ref": "#/definitions/User"
}
},
{
"name": "file",
"in": "path",
"description": "file to upload",
"required": true,
"type": "file"
}]
}
},
I do get the option for uploading a file, but when I try to receive it at the backend,(using print request.files) it returns me nothing.
How can I receive the file (selected at swagger level) at the backend.??

iFile = request.files.getlist('file')[0]- This command reads the file uploaded via swagger UI.

For file, the type should be formData instead of path. See below for an example
{
"name": "file",
"in": "formData",
"description": "file to upload",
"required": false,
"type": "file"
}
ref: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/test/resources/2_0/petstore.json#L384
For consumes, it should be multipart/form-data

Related

Build a JSON object using for loop with python

I am new to python and trying to write a simple script that pushes notification to my slack channel about open tickets in my queue.
I am trying to submit the below JSON object to a slack webhook but not sure how to achieve the end result that looks like below.
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "There are *2* open tickets :in the queue\n *Ticket Details *"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*12345* - High CPU usage on prod app1 server"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*54321* - DB parameter modification - ProdDB03"
}
},
{
"type": "divider"
}
]
}
I am doing an API call to an URL that gives me the open ticket details in a JSON format. I am able to parse through it and store the Count (number of open tickets), Ticket ID and the Ticket Description in respective variables.
I would like to create a block for each ticket as below.
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*54321* - DB parameter modification - ProdDB03"
}
}
Just ideas on how to achieve this without actual code would be helpful too.
Thanks

Can i make use of binding expression in azure functions for Python?

I have in binding of httpTrigger and out binding of blob wherein i am creating a blob after processing the request. The blob name i need to evaluate on runtime and use that in my expression. Is there a way to achive that in python?
function.json
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"name": "outputblob",
"type": "blob",
"path": "testcntainer/attachments/{customFileName}",
"connection": "",
"direction": "out"
},
{
"name": "$return",
"type": "http",
"direction": "out"
}
]
}
No, it is no possible.
The value of blobname in binding is const.
The right way to acheive what you want is put the logic in the body of your function.
Have a look of this offcial doc:
https://learn.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python#upload-blobs-to-a-container
Let me know if you have any adoubts about above link, if that, I will update a simple code.:)

Problems creating new query DoubleClick Bid Manager - Python

dict = {
"kind": "doubleclickbidmanager#query",
"metadata": {
"dataRange": "LAST_30_DAYS",
"format": "CSV",
"title": "test API"
},
"params": {
"filters": [
{
"type": "FILTER_PARTNER",
"value": "Nestle (GCC&Levant)_PM MENA (2410734)"
}
],
"metrics": [
"METRIC_CLICKS",
"METRIC_UNIQUE_REACH_CLICK_REACH",
"METRIC_UNIQUE_REACH_IMPRESSION_REACH"
]
}
}
r = requests.post('https://www.googleapis.com/doubleclickbidmanager/v1.1/query',data = dict)
This is the code i am trying to use for creating Query for offline report on google bid manager.
It give me following error
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
I have tried different ways even tried using the request type call and put the authorization keys in the API call but it didn't work. Surely something is missing can anyone confirm?
You can follow these python exemples for login :https://github.com/googleads/googleads-bidmanager-examples/tree/master/python
But anyway, there is always something wrong after the login, i post another question below : HttpEroor 500 Backend Error and HttpError 403 using DoubleClick Bid Manager API in python

How to POST json file/data to api url via python

Sorry if this question simple for you guyz but I still have no solution for it. I need your help here.
I have json file with the data that need to be post to DB via api. The post request return response 200 (ok) but the data is actually not store to the DB.
Please find the code that i use to send the json data to api url
url = 'http://192.168.1.1:7279/ctrl/api/topodisco'
headers = {'Accept' : 'application/json', 'Content-Type' :
'application/json'}
r = requests.post(url, data=open('elements.json', 'rb'), headers=headers)
print(r)
This is the content of elements.json file
[
{
"type": "rtr-x",
"name": "we-01",
"ip": "1.1.1.1",
"location": "jbx"
},
{
"type": "swr-x",
"name": "sw-03",
"ip": "172.16.3.18",
"location": "jbx"
},
{
"type": "rtr-x",
"name": "we-03",
"ip": "1.1.1.10",
"location": "jbx"
}
]
This is sample of api body that shall be followed
{
"service": "topodisco",
"action": "create_node",
"table": "user3",
"node": [
{
"type": "SAMPLE_TYPE",
"name": "SAMPLE Name",
"ip": "192.1.1.1",
"location": "sample location"
},
{
"type": "SAMPLE_TYPE",
"name": "SAMPLE Name",
"ip": "192.1.1.2",
"location": "sample location"
}
]
}
Looking into that, I don't know how to insert service,action,table and node onto the script. Is there a way that I can add those to the code above without modified/edit the json file. The json file is generated from a csv file. Please help me.
Please advise me further. Thank you for your attention and support

Location widget in messenger platform displays a search bar in mobile version but not in Desktop Version

By using bot functionality provided by facebook messenger platform,
I want users to be able to provide location by using search.
It's working as expected in mobile app of the messenger as it's showing search option. But in the desktop version of the messenger, search option is not showing in location widget.
I wanted to ask that is it expected behavior or I'm missing out something.
Also in browser console, it's showing some error:
ErrorUtils caught an error:
"navigator.permissions.query(...).then(...).done is not a function".
Subsequent errors won't be logged;
see https://fburl.com/debugjs.ja # qtaMy7UNoCv.js:47.
Here's what I've tried so far:
def send_location_with_quick_reply(self, recipient_id, message, quick_replies):
url = self.get_message_url()
payload = {
"recipient":{
"id":recipient_id
},
"message":{
"text": message,
"quick_replies":[{
"content_type": "location"
}]
}
}
# _do_post will hit the send_message API of `Messenger`.
return self._do_post(url, payload)
And here's the response I'm getting after the user chooses the location:
{
"object": "page",
"entry": [{
"id": "128922990987384",
"time": 1505890084176,
"messaging": [{
"sender": {
"id": "1456347437763847"
},
"recipient": {
"id": "128922990987384"
},
"timestamp": 1505890084065,
"message": {
"mid": "mid.$cAAAvskrTvY9kz1Bs4Vengsjpb9L_",
"seq": 2366,
"attachments": [{
"title": "User's Location",
"url": "https:\\/\\/l.facebook.com\\/l.php?u=https\\u00253A\\u00252F\\u00252Fwww.bing.com\\u00252Fmaps\\u00252Fdefault.aspx\\u00253Fv\\u00253D2\\u002526pc\\u00253DFACEBK\\u002526mid\\u00253D8100\\u002526where1\\u00253D12.9703749\\u0025252C\\u00252B77.6361206\\u002526FORM\\u00253DFBKPL1\\u002526mkt\\u00253Den-US&h=ATNsjbke0tPFGIFpCq4MA5l1W6wmiwp0cTfUZNXSSbMDHxygEM4GrVlZmtaebsN4elliFhMSJNmIFwQgn-p_fxnF2hW0VdKTj2z_0hsWnH4dlLZGdQ&s=1&enc=AZN9DwrutbtXSfRAdxQf4bzFSMSO3zujAb0LBOgUt9mz16ZnDn7CSZDBLmnISjfAMbLG6b6H6hn9a3KCb6wOo7dn",
"type": "location",
"payload": {
"coordinates": {
"lat": 12.9703749,
"long": 77.6361206
}
}
}]
}
}]
}]
}
I am using python and drf to integrate with messenger platform.
Yes, this is expected behavior currently.

Categories

Resources