What is response headers format to output OpenAPI docs on FastAPI - fastapi

Let me know someone way to output header name
I wrote response the following code,
responses={
st.HTTP_200_OK: {
"model": main_response_data,
"description": "main response data",
"headers": [{"header_name":"response_key", "description": "Response key", "type": "string"}]
},
But after run, OpenAPI document output "0" at header name
headers
Name Description Type
------------------------------------------
0 Response key string

headers should be a dictionary with the HTTP-header as a key:
"headers": {"response_key": {"description": "Response key", "type": "string"}}
When you define it as a list instead, the index will be used as the key - i.e. why you're getting 0.

Related

Getting a 400 Error when doing a simple text post

Trying to migrate our calls away from the v2/ugcPosts endpoint to the new /rest/posts endpoint. I started off trying to create a text-only post as per the Text-Only Post Creation Sample Request
I copied and pasted the request body, changed the organization ID to my own, and included Authorization: Bearer {My Authorization Token}, X-Restli-Protocol-Version: 2.0.0, Content-Type: application/json, LinkedIn-Version:202207 as headers.
POST https://api.linkedin.com/rest/posts
{
"author": "urn:li:organization:***",
"commentary": "Sample text Post",
"visibility": "PUBLIC",
"distribution": {
"feedDistribution": "NONE",
"targetEntities": [],
"thirdPartyDistributionChannels": []
},
"lifecycleState": "PUBLISHED",
"isReshareDisabledByAuthor": false
}
However, when trying to post, I keep getting this 400 error.
{
"errorDetailType": "com.linkedin.common.error.BadRequest",
"code": "MISSING_REQUIRED_FIELD_FOR_DSC",
"message": "Field /adContext/dscAdAccount is required when the post is a Direct Sponsored Content, but missing in the request",
"errorDetails": {
"inputErrors": [
{
"description": "Field /adContext/dscAdAccount is required when the post is a Direct Sponsored Content, but missing in the request",
"input": {
"inputPath": {
"fieldPath": "/adContext/dscAdAccount"
}
},
"code": "MISSING_REQUIRED_FIELD_FOR_DSC"
}
]
},
"status": 400
}
I see that lifecycleState, distribution, visibility, commentary, author are all required, but adContext should be an optional field. I'm not sure what part of my request is indicating I'm trying to make a Direct Sponsored Content post - can someone take a look?
I've already tried
Removing the targetEntities and thirdPartyDistributionChannels parameters.
Removing isReshareDisabledByAuthor and the above.
Changing the feedDistribution to MAIN_FEED and NONE
Creating the post via the /v2/ugcPosts endpoint - meaning the authorization token and organization urn are correct

Find and delete Discord webhook message

I'm using a Discord webhook to send information which may later be invalidated, so I want to be able to delete it. To do this i use these endpoints:
First i make a post request to send a message:
POST /webhooks/{webhook.id}/{webhook.token}
And then i want to do a delete request to remove the message again:
DELETE /webhooks/{webhook.id}/{webhook.token}/messages/{message.id}
However I don't have the ID of the message i want to delete, since no response is given to the first POST request which is always an empty 204 response. Is it possible to get the message id?
Any help would be appreciated.
Per this reddit post:
If you need to reference a message ID of a webhook message you sent, you can add ?wait=true to the end of the URL which will give you the message data (including the ID) instead of a 204 (No Content) when you don't include the query parameter.
So if you send your normal POST request to your url like this: POST /webhooks/{webhook.id}/{webhook.token}, add ?wait=true to the end of that. Then you will get back data like this:
{
"id": "MESSAGEID",
"type": 0,
"content": "This is a test",
"channel_id": "CHANNELID",
"author": {
"bot": true,
"id": "AUTHORID",
"username": "USERNAME",
"avatar": "AVATARID",
"discriminator": "0000"
},
"attachments": [],
"embeds": [],
"mentions": [],
"mention_roles": [],
"pinned": false,
"mention_everyone": false,
"tts": false,
"timestamp": "2021-11-13T18:10:24.412000+00:00",
"edited_timestamp": null,
"flags": 0,
"components": [],
"webhook_id": "WEBHOOKID"
}

How to write parameter name with space in it? Custom Integromat apps

Api sends me a response with "Joined at" parameter, and I cant do anything with it because of the space " ", but that paramter is vital for one trigger in my Integromat app.
"trigger": {
"id": "{{item.id}}",
"date": "{{item.attributes.Joined at}}",
"type": "date",
"order": "desc"
}
Response example
You can use backticks to retrieve collection properties, as explained in the docs.
The expression will look like this: "date": "{{item.attributes.`Joined at`}}"

Unable to POST to Grafana using Python3 module requests

I'm trying to create a dashboard on Grafana using their backend API. I first test that my API token is set up by using GET and successfully get a return code of 200(shown below). I then try to use POST to create a simple dashboard but I keep getting a return code of 400. I'm pretty sure it has something to do with the payload I'm trying to send, but I have been unable to figure it out. Here is the link to the example page I'm using for their JSON format. http://docs.grafana.org/reference/http_api/
import requests
headers = {"Accept": "application/json","Content-Type": "application/json" ,"Authorization": "Bearer xxx"}
r = requests.get("http://www.localhost",headers=headers)
print(r.text)
print(r.status_code)
dashboard = {"id": None,
"title": "API_dashboard_test",
"tags": "[CL-5]",
"timezone": "browser",
"rows":"[{}]",
"schemaVersion": 6,
"version": 0
}
payload = {"dashboard": "%s" % dashboard}
url = "http://www.localhost/api/dashboards/db"
p = requests.post(url,headers=headers, data=payload)
print(p)
print(p.status_code)
print(p.text)
OUTPUT:
200
<Response [400]>
400
[{"classification":"DeserializationError","message":"invalid character 'd' looking for beginning of value"},{"fieldNames":["Dashboard"],"classification":"RequiredError","message":"Required"}]
The problem is that your object is not an actual json object.
You can use post method with json=YOUR_PYTHON_OBJECT
So to fix your code, change your dictionary to use just a regular python dictionary, use json=payload, rather than data=payload.
So refactoring your code, you will have:
import requests
headers = {"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": "Bearer xxx"
}
r = requests.get("http://www.localhost", headers=headers)
print(r.text)
print(r.status_code)
dashboard = {"id": None,
"title": "API_dashboard_test",
"tags": ["CL-5"],
"timezone": "browser",
"rows": [{}],
"schemaVersion": 6,
"version": 0
}
payload = {"dashboard": dashboard}
url = "http://www.localhost/api/dashboards/db"
p = requests.post(url, headers=headers, json=payload)
print(p)
print(p.status_code)
print(p.text)
Note the differences in dashboard, for example, "rows" was changed from "[{}]" to just [{}] so that it is a python object (list with empty dictionary), rather than a string.
The output is
200
<Response [200]>
200
{"slug":"api_dashboard_test","status":"success","version":0}

How can I get Mandrill to parse the Handlebars tags in my template?

I'm using Mandrill's send-template API method to send an email. It shouldn't matter, but just in case, I'm using PHP.
The email is delivered using the correct template, but none of the Handlebars variables are replaced with their values. Instead, they're merely removed.
Here's a full request as seen in the API Logs after reducing my code:
{
"template_name": "my-template-slug",
"template_content": [
{
"name": "price",
"content": "$25"
}
],
"message": {
"subject": "My Subject",
"from_email": "my#email.com",
"from_name": "My Name",
"to": [
{
"name": "Jimmy Crackcorn",
"type": "to",
"email": "jimmy#crackcorn.com"
}
]
},
"async": false,
"ip_pool": null,
"send_at": null,
"key": "my_api_key"
}
I also tried simplifying my template to just {{price}}, which just sends a blank email.
I've also verified that Sending Defaults > Merge Language is set to Handlebars.
In case it makes any difference, I created the template in MailChimp and sent it to Mandrill, then removed the escaping back-slashes from the variables.
Handlebars should be passed in the merge_vars or global_merge_vars parameters in your API request, not in template_content—which is for mc:edit regions for the MailChimp template language.

Resources