Eve framework: can't set attribute - put

I'm using Eve frameworks with it's hooks.
I'm also using on_insert hook for creating data. I've enabled PUT method, because it causes an error for POST.
Sending PUT requests looks like:
requests.put("http://127.0.0.1:7000/users", headers=headers, json={"name": "John", "age": 30})
It raises an error:
('message: ', u'{"_status": "ERR", "_error": {"message": "An exception occurred: can\'t set attribute", "code": 400}}')
('status: ', 400)

Related

Filter event objects by body content

I'm trying to query calendar events using the Graph API and I would like to filter them by category and body content.
The goal is to get any event object that is in category "Test" and contains the string "FooBar" in its body content.
I tried to query the Graph API with the following request:
https://graph.microsoft.com/v1.0/me/events?$filter=(categories/any(x:x eq 'Test') and contains(body/content, 'FooBar'))
The response is a 500 error message:
{
"error": {
"code": "ErrorInternalServerError",
"message": "An internal server error occurred. The operation failed.",
//...
}
}
The "categories" filter clause works fine on its own but as soon as I put the "body/content" clause back in I get the aforementioned error response.
The json object I get from the Graph API looks like this (stripped down for better readability)
{
"value": [
{
"categories": [
"Test"
],
"bodyPreview": "FooBar",
"body": {
"contentType": "html",
"content": "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body><div class=\"BodyFragment\"></div><div class=\"BodyFragment\"><font size=\"2\"><span style=\"font-size:11pt\"><div class=\"PlainText\">FooBar</div></span></font></div></body></html>"
}
}
]
}
Is there something wrong with my filter clause or might this be an issue with Graph API itself?
I first tried to filter by bodyPreview but the error response I got clearly said that this field cannot be filtered by so I guess body/content should be possible.
The filtering by body/content is not supported for events.
I've tried similar query for messages and it works fine, so your filter clause is correct.
GET https://graph.microsoft.com/v1.0/me/messages?$filter=(categories/any(x:x eq 'Test') and contains(body/content, 'FooBar'))

A post call to add a new openTypeExtension to event data fails with “Resource not found for the segment 'extensions'”

I am trying to add an extension to the event resource using Graph Explorer. I am using POST on https://graph.microsoft.com/v1.0/event/extensions with request body
{
"#odata.type": "microsoft.graph.openTypeExtension",
"extensionName": "com.wrike.WrikeIDs",
"id": "",
"permalink": ""
}
I get the error
"Resource not found for the segment 'event'"
and sometimes I get
"The OData request is not supported"
Content type is application/json
The error message might be misleading.
Try to adjust the path to
POST https://graph.microsoft.com/v1.0/me/events/{event_id}/extensions

Rails web-push throws an error 401 (unauthorized)

I'm getting a
{"code": 401, "errno": 109, "error": "Unauthorized", "more_info": "http://autopush.readthedocs.io/en/latest/http.html#error-codes", "message": "Request did not validate missing authorization header"} error in my rails app. But it was working just fine until yesterday...
This is the code for the payload_send
Webpush.payload_send(
endpoint: s[:endpoint],
p256dh: s[:p256dh],
auth: s[:auth],
vapid: {
subject: vapid_keys[:subject],
public_key: vapid_keys[:public_key],
private_key: vapid_keys[:private_key]
}
)
And all the required parameters are definitely completely filled in.
I'm using this webpush library for rails https://github.com/zaru/webpush

UnknownError when creating a MicrosoftGraph subscription

I am working on a project to set up a webhook with Microsft graph. I have everything set up to validate the endpoint I have created as per (https://developer.microsoft.com/en-us/graph/docs/concepts/webhooks), however, I am receiving an "Unknown Error" from Microsoft as follows:
"__SLOG0__", "{
\"error\": {
\"code\": \"UnknownError\",
\"message\": \"\",
\"innerError\": {
\"request-id\": \"d0037849-dc79-4244-bb15-cf72841c6653\",
\"date\": \"2018-10-22T20:00:43\"
}
}
}"
I create the subscription with the following values:
$body_vals = dict[
"changeType" => "created,updated",
"notificationUrl" => $notification_uri,
"resource" => "/me/mailfolders('inbox')/messages",
"expirationDateTime" =>
Office365APIUtils::getISO8601DateStamp($date->getTimestamp()),
"clientState" => "SecretClientState",
]
passed into my POST request to the endpoint. I know this has to do with my specific notification uri (which is a facebook endpoint) because if i switch the endpoint to https://google.com, for example, I get a more helpful response:
"__SLOG0__", "{
\"error\": {
\"code\": \"InvalidRequest\",
\"message\": \"Subscription validation request failed. Must respond with 200 OK to this request.\",
\"innerError\": {
\"request-id\": \"4e2ac4af-4d10-416d-83a1-4eb896a35418\",
\"date\": \"2018-10-22T19:52:46\"
}
}
}"
saying I have to validate at the endpoint. I already registered my app, if there is anyone with the Graph team or that has dealt with this before with any leads on these UnknownErrors? an example request-id is 7da743ce-6ffe-4d80-8611-a5be024c8b21
It looks like your code may not be performing the endpoint validation step. This article contains a complete walk-through of how to create a subscription. Have a look at the "Notification endpoint validation" section; your endpoint must be able to respond with 200 and include the validation token.

Rails: Pretty print JSON without overkill

I'm using the below code to display an unauthorized message in JSON:
def render_unauthorized
# Displays the Unauthorized message since the user did
# not pass proper authentication parameters.
self.headers['WWW-Authenticate'] = 'Token realm="API"'
render json: {
error: {
type: "unauthorized",
message: "This page cannot be accessed without a valid API key."
}
}, status: 401
end
Which outputs:
{"error":{"type":"unauthorized","message":"This page cannot be accessed without a valid API key."}}
So my question is this: Is there a way to pretty print this message (WITHOUT putting it in a separate view and using some 3rd party gem).
Edit: What is pretty print?
Properly spaced, and well .. pretty. Here's the output I'd like to see:
{
"error": {
"type": "unauthorized",
"message": "This page cannot be accessed without a valid API key."
}
}
Solution
Using #emaillenin's answer below worked. For the record, here's what the final code looks like (since he hadn't included the whole thing):
def render_unauthorized
# Displays the Unauthorized message since the user did
# not pass proper authentication parameters.
self.headers['WWW-Authenticate'] = 'Token realm="API"'
render json: JSON.pretty_generate({ # <-- Here it is :')
error: {
type: "unauthorized",
message: "This page cannot be accessed without a valid API key."
}
}), status: 401
end
Use this helper method built into JSON.
JSON.pretty_generate
I just tried and it works:
> str = '{"error":{"type":"unauthorized","message":"This page cannot be accessed without a valid API key."}}'
> JSON.pretty_generate(JSON.parse(str))
=> "{\n \"error\": {\n \"type\": \"unauthorized\",\n \"message\": \"This page cannot be accessed without a valid API key.\"\n }\n}"
If you (like I) find that the pretty_generate option built into Ruby's JSON library is not "pretty" enough, I recommend my own NeatJSON gem for your formatting.
To use it gem install neatjson and then use JSON.neat_generate instead of JSON.pretty_generate.
Like Ruby's pp it will keep objects and arrays on one line when they fit, but wrap to multiple as needed. For example:
{
"navigation.createroute.poi":[
{"text":"Lay in a course to the Hilton","params":{"poi":"Hilton"}},
{"text":"Take me to the airport","params":{"poi":"airport"}},
{"text":"Let's go to IHOP","params":{"poi":"IHOP"}},
{"text":"Show me how to get to The Med","params":{"poi":"The Med"}},
{"text":"Create a route to Arby's","params":{"poi":"Arby's"}},
{
"text":"Go to the Hilton by the Airport",
"params":{"poi":"Hilton","location":"Airport"}
},
{
"text":"Take me to the Fry's in Fresno",
"params":{"poi":"Fry's","location":"Fresno"}
}
],
"navigation.eta":[
{"text":"When will we get there?"},
{"text":"When will I arrive?"},
{"text":"What time will I get to the destination?"},
{"text":"What time will I reach the destination?"},
{"text":"What time will it be when I arrive?"}
]
}
It also supports a variety of formatting options to further customize your output. For example, how many spaces before/after colons? Before/after commas? Inside the brackets of arrays and objects? Do you want to sort the keys of your object? Do you want the colons to all be lined up?
Using the aligned option would allow your output to look like this:
{
"error": {
"type" : "unauthorized",
"message" : "This page cannot be accessed without a valid API key."
}
}

Resources