DATE_FORMAT="%s" cannot post timestamp data into datetime field - eve

I would like to use timestamp for datetime field type, so I changed DATE_FORMAT for "%s":
settings.py:
DATE_FORMAT="%s"
...
'when': {
'type': 'datetime'
},
...
The format is now a valid timestamp when doing a GET on some data :
$ curl http://192.168.3.42:5001/stock
...
"when": "1551083317",
...
BUT I cannot insert new data, datetime are not accepted :
$ curl -d '{"when": "1555543177"}' -H 'Content-Type: application/json' http://192.168.3.42:5001/stock
{"_status": "ERR", "_issues": {"when": "must be of datetime type"}, "_error": {"code": 422, "message": "Insertion failure: 1 document(s) contain(s) error(s)"}}
I tried without double-quote :
curl -d '{"when": 1555543177}...
same result.
Different formats for DATE_FORMAT are OK except for "%s" (timestamp)
any idea ?
$ pip list
Package Version
Cerberus 1.2
Eve 0.8.1
Flask 1.0.2
pymongo 3.7.2

Related

Issues search error: Unexpected character (''' (code <N>)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')

I am trying to get this same type of query.
curl -u admin:admin -X POST localhost:50813/rest/api/2/search -H "Content-Type: application/json" -d '{"jql":"updated > -1d","fields":[""]}' -s | jq > jql-output.txt
But hitting error.
I ref this query from
https://confluence.atlassian.com/jirakb/how-to-programmatically-update-issues-from-a-jql-using-rest-api-in-jira-1031284474.html
Error which I am getting for now is to debug further.
curl -u admin:admin -X POST localhost:50813/rest/api/2/search -H "Content-Type: application/json" -d '{"jql":"updated > -1d","fields":[""]}'
Error:
{"errorMessages":["Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: org.apache.catalina.connector.CoyoteInputStream#c502a0b; line: 1, column: 2]"]}
Any idea? Been trying various options. No clear solution
Yes, it seems this is a documentation error. You can basically remove the fields from request (and it returns all fields as default) or you can set it from one of the following as stated in another Jira documentation:
By default, only navigable (*navigable) fields are returned in this
search resource. Note: the default is different in the get-issue
resource -- the default there all fields (*all).
*all - include all fields
*navigable - include just navigable fields
summary,comment - include just the summary and comments
-description - include navigable fields except the description (the default is *navigable for search)
*all,-comment - include everything except comments
So basically, the request is should be like that:
curl -u admin:admin -X POST localhost:50813/rest/api/2/search -H "Content-Type: application/json" -d '{"jql":"updated > -1d"}'

OpenAPI v3 / Swagger - calling a function and sending json dictionary to it

I'm trying to use OpenAPI/Swagger to run an endpoint capable of consuming a json dictionary, sending to a function and getting a response.
I'm using operationId to refer to the function that I want to call, but can't work out how to send the dictionary that's received by the endpoint.
controllers.get_options is called, but no parameters are sent to it with my current approach.
I think I'm missing something obvious, but it's not apparent!
I would call the endpoint like this:
curl -X 'POST' \
'http://localhost:8080/getoptions' \
-H 'accept: */*' \
-H 'Content-Type: application/json' \
-d '{
"product_area": "Main",
"product_type": "New"
}'
This is the openapi config file (./openapi.yaml)
EDIT: adding x-body-name: DiscussionResult below solved the issue as per Helen's comment
openapi: 3.0.0
info:
title: Test
version: '1.0'
paths:
/getoptions:
post:
description: Return product options from product type and area
operationId: controllers.get_options
requestBody:
required: true
content:
application/json:
x-body-name: DiscussionResult
schema:
$ref: '#/components/schemas/DiscussionResult'
responses:
200:
description: "success"
components:
schemas:
DiscussionResult:
type: object
discriminator:
propertyName: product_type
properties:
product_type:
type: string
example: "New"
product_area:
type: string
example: "Main"
I'm running it using connexion as shown here:
main.py
import connexion
import logging
def create_app():
logging.basicConfig(level=logging.DEBUG)
connex_app = connexion.FlaskApp(__name__, specification_dir="./openapi/")
connex_app.add_api("./openapi.yaml", resolver_error=501)
return connex_app
if __name__ == "__main__":
app = create_app()
app.run(host="0.0.0.0", port=8080)
requirements.txt
connexion[swagger-ui]
connexion>=2.2.0
python-jose[cryptography]
six>=1.9
Flask>=0.10.1
sqlathanor
and this is the function that I want to call
def get_options(DiscussionResult):
msg = "{} {}".format(DiscussionResult['product_area'], DiscussionResult['product_type'])
return jsonify(message=msg), 200
Connexion docs on Request Handling include the following note:
In the OpenAPI 3.x.x spec, the requestBody does not have a name. By default it will be passed in as ‘body’. You can optionally provide the x-body-name parameter in your requestBody schema to override the name of the parameter that will be passed to your handler function.
Looks like you need to add x-body-name: DiscussionResult to the DiscussionResult schema that is used in the requestBody:
components:
schemas:
DiscussionResult:
x-body-name: DiscussionResult # <---------
type: object
...
or
requestBody:
required: true
content:
application/json:
schema:
x-body-name: DiscussionResult # <---------
$ref: '#/components/schemas/DiscussionResult'
I'd very much recommend FastApi as I already said in the comments earlier. Here is a little bit of a working code.
main.py
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class DetailsModel(BaseModel):
product_area: str
product_type: str
#app.post("/get_details")
async def _(
input_json: DetailsModel
):
return {"returns": input_json.dict()}
Run uvicorn main:app --reload from root directory
Then check http://127.0.0.1:8000/docs
Then you can call:
curl -X 'POST' \
'http://127.0.0.1:8000/get_details' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"product_area": "Main",
"product_type": "New"
}'
Fastapi checks for any unprocessable entities with Pydantic which helps a lot with any requests that doesn't fit the model.
Check the official and very detailed documentation too https://fastapi.tiangolo.com/.

"swagger-codegen" and "openapi-generator" unintentionally convert a datetime without a time zone to a datetime with UTC time zone

I wrote a parameter that "type:" is "string" and "example:" is a text datetime without time zone like "2021-09-01 00:00:00".
but on the UI that was generated by swagger-codegen-cli-3.0.27.jar or openapi-generator-cli-5.2.1.jar,
the parameter format is ISO 8601 format with UTC timzone like "2021-09-01T00:00:00Z"
I don't want to convert datetime format.
I want to show without time zone style.
How do I write spec file?
test.yml
openapi: 3.0.3
info:
title: test
version: 1.0.0
paths:
/test/{dateTime}/:
get:
parameters:
- name: "dateTime"
in: "path"
required: true
schema:
type: string
example: "2021-09-01 00:00:00"
responses:
"200":
description: Success
content:
application/json:
schema:
type: object
properties:
dateTime2:
type: string
example: "2021-09-01 00:00:00"
then
java -jar swagger-codegen-cli-3.0.27.jar generate -i test.yaml -l python-flask -o test-flask
cd test-flask
python -m swagger_server
or
java -jar openapi-generator-cli-5.2.1.jar generate -i test.yaml -g python-flask -o test-flask
cd test-flask
python -m openapi_server
then open "http://localhost:8080/ui/#/default/test_date_time_get"

How to query predictionio for a particular app

I have created multiple apps in predictionio.
While inserting events in predictionio, there a parameter accessKey (associated to app) which is passed.
Whereas for queries.json, couldn't find the accessKey parameter.
Sample below:
curl -H "Content-Type: application/json" \
-d '{ "items": ["i1", "i3"], "num": 10, "categories" : ["c4", "c3"], "blackList": ["i21", "i26", "i40"] }' \
http://localhost:8000/queries.json
{"itemScores":[{"item":"i39","score":1.0773502691896257},{"item":"i44","score":1.0773502691896257},{"item":"i14","score":1.0773502691896257},{"item":"i45","score":0.7886751345948129},{"item":"i47","score":0.7618016810571367},{"item":"i6","score":0.7618016810571367},{"item":"i28","score":0.7618016810571367},{"item":"i9","score":0.7618016810571367},{"item":"i29","score":0.6220084679281463},{"item":"i30","score":0.5386751345948129}]}
Now, how to query data for a particular app?

watson machine learning api - token refresh 400 error

I've successfully generated a token with the GET /v3/identity/token API. I now want to be able to leverage the PUT API to keep the token active.
I am trying this curl command:
curl -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d '<token-value>' 'https://ibm-watson-ml.mybluemix.net/v3/identity/token' -v -i --basic --user <username>:<password>
I get a 400 error stating:
For request 'PUT /v3/identity/token' [Invalid Json: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value at [Source: akka.util.ByteIterator$ByteArrayIterator$$anon$1#18bd12ef; line: 1, column: 3]]
The token returned from the get request has the dash character in it, along with other non-alphnumeric values.
Does the token from the get request need to be parsed? what am I missing?
You need to set your content-type to application/json. But -d sends the Content-Type application/x-www-form-urlencoded, which maybe is not accepted on IBM side.
But, seems like your JSON (token) are in the incorrect format.
The token value needs to be the following format (JSON):
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"
}
And you need to follow the example of sent correctly the format:
curl -H 'Content-Type: application/json' -X PUT \
-d '{"token":"yourToken"}' \
https://ibm-watson-ml.mybluemix.net/v3/identity/token
See the official reference.

Resources