What happens to Google Cloud Translation DetectLanguage when billing budget is reached? - google-translation-api

I am using Google.Cloud.Translation.V2 reference to run texts through "DetectLanguage".
This returns "Detection" object with language and score.
If I set Budget to $20 but I go over 1 million characters what gets returned from DetectLanguage? Does this throw exception or return empty "Detection" object ?

Related

Dynamic results from query builder

I am working on a search filter atm where people can specify things like (example) size, color, fabric and so on.
Obviously I have models for each i.e. size, color and fabric. But since this "search" should return every result i.e. size + color + fabric and not just one of the three I would need to make a new struct which contains all the (size, color and fabric) to be able to consume the result that Gorm would return.
Since we have a LOT of filters, this could get very messy. Does anyone know if there is a better way to do it? or what would be a best practice to do this?
type Result struct {
ID int
Name string
Age int
}
var result Result
db.Raw("SELECT id, name, age FROM users WHERE name = ?", 3).Scan(&result)
The above example illustrates how I think it should be done, but as you can expect with the amount of data I need to return this struct would become huge.
The results are quite large in terms of data. I mean what I would want in return in the final version for example (this is about sales):
What products were bought, the amount, color and size. Payment data i.e. price, tax, payment method. Customer information, et cetera.
So in total there is a lot of information to store.
All this information should be returned as a JSON format so we can call it through an API call.
Each call should give back 100 up to 15.000 results, to give you an idea of the side of the data.
Hope someone can explain a bit about a best practice method and -or how I should solve this problem as I am unsure on how to code this effectively.

How to get an event ID from the HitID returned by a Microsoft Graph search query?

I am trying to patch a calendar event with the Microsoft Graph API (from a Node express app).
I create a new event with client.api('/me/events').post(myEvent) and it works just fine (I see it appear in my calendar). The return value has an ID which is:
AAMkADc0Yjg2ODdmLTlkNDQtNGQ0Yi1iNjBmLTE1MDdmYzI4MGJkOABGAAAAAADt0ZJy6xMCRq23C8icFGeqBwAOM3XMH4d2SYMQ5psbvFytAAAAAAENAAAOM3XMH4d2SYMQ5psbvFytAAJ_B-B7AAA=
I then use client.api('/search/query').post(myQuery) to find the event based on some criteria, and this works fine. I receive an array of hits, with only one hit (which actually is the freshly created event, looking at the subject and body), and with a hitId equal to:
AAMkADc0Yjg2ODdmLTlkNDQtNGQ0Yi1iNjBmLTE1MDdmYzI4MGJkOABGAAAAAADt0ZJy6xMCRq23C8icFGeqBwAOM3XMH4d2SYMQ5psbvFytAAAAAAENAAAOM3XMH4d2SYMQ5psbvFytAAJ+B/B7AAA=
For some reason I don't understand why the 2 IDs are not fully identical: the _ is changed to +and -changed to /.
I now want to modify the event, and try to update it with
let newVal = hits[0].resource // hits is coming from the result returned by the search query
newVal.id = hits[0].hitId // needed because the 'resource' does not contain the id
client.api('/me/events/'+hitId).patch(newVal)
But I get an error: Resource not found for the segment 'B7AAA='.
Could you please tell me how to make the patch work (and explain why the ID from the search is not strictly like the one created). I have read several examples in the documentation (such as https://learn.microsoft.com/en-us/graph/search-concept-events) but I could not find a solution.
Many thanks!
So what is happening here is, PATCH /me/events/{hitId} is being resolved by Graph API such that the forward slash in the hitId denotes a path and Graph ends up using B7AAA= as a resource id hence the error Resource not found for the segment 'B7AAA='.
A work around that might work is to replace / in hitId(s) with %252F. You can do it like this.
client.api(`/me/events/${hitId.replace('/', '%252F')}`).patch(patch)
There is already this Issue on GitHub for documentation on how to handled these base64 encoded resource ids with /
As for the two IDs being non identical, Graph API will accept both of them and resolve to the same resource. I have no idea why they are different though.

Google geocode format issue. Receiving undefined offset error

I get this PHP Notice: Undefined offset: 0 on line 9. It is regarding the Google Geocode API in the code provided below.
An undefined offset error occurs when you try to reference an array value at position 0 but that position doesnt exist. I am assuming the Google geocode API has changed within the last year. As it must be returning a different result structure then the one originally programmed with. However, although I know this, I cannot find anything wrong with the code. Does anyone know if the format of my code is incorrect?
The code below pulls the country and town fields from an input form. The Google Geocode works out the lat and lng of the town, then feeds this to the timezone API to calculate the timezone. I have hidden my API key for security purposes. Any help is appreciated. Thank you.
<?php
$town = urldecode($_GET['town']);
$country = urldecode($_GET['country']);
if(!empty($_GET['state'])){
$state = urldecode($_GET['state']);
}
$location = json_decode(#file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?key=API_KEY_HERE&address='.urlencode($town.', '.(!empty($state) ? $state.', ' : '').$country)), true)['results'][0]['geometry']['location'];
$timezone = json_decode(#file_get_contents('https://maps.googleapis.com/maps/api/timezone/json?key=API_KEY_HERE&location='.$location['lat'].','.$location['lng'].'&timestamp='.date('U')), true);
$time = $timezone['rawOffset'] / 60 / 60;
echo $time;
?>
This part of your code is where the problem is:
)['results'][0]['geometry']['location']
You're assuming that there will always be a result at position 0 (the first item). In the case of an error the results array will be empty, and thus no result at position 0.
The API result structure hasn't changed. Google wouldn't do that, as it would break a LOT of applications. It's more likely that a particular set of inputs, such as an invalid address, are giving an error response.
Take a look at the documentation here. Pay attention to the "status" field, which contains a variety of status codes. You should modify your code to only try to get the item from the result if the status is "OK".
On the time zone side of things, you should check the status code there as well. (Here are the docs.) Then in most cases, you should take the timeZoneId rather than the rawOffset. You can use that ID with PHP's built-in time zone functionality (DateTime, DateTimeZone classes, etc.).

Google Calendar API stopped returning events

Since today I have problem with one of 2 similar calls to Google Calendar API v3:
calendar.events.list
Both calls are requesting list of assignemnts and use privateExtendedProperty to filter them, but key=Value pairs are different.
When I have privateExtendedProperty set to myStatus=READY it returns events I need,
but when I set privateExtendedProperty to myId=agxzfm1haWxmb29nYWVyNQsSDE9yZ2FuaXphdGlvbiIVYWN0aXZlZ2FtaW5nbWVkaWEuY29tDAsSBENhc2UYkcHA3wgM
it returns no events, though I'm sure there are several events with this myId...
It appears that the calendar API list is returning an empty list when querying by a privateExtendedProperty with 90 characters long.
Everything worked fine for years, but it suddenly broke for some of my users this morning...
This is my request:
GET googleapis.com/calendar/v3/calendars/****/events?privateExtendedProperty=myId%3Dagxzfm1haWxmb29nYWVyNQsSDE9yZ2FuaXphdGlvbiIVYWN0aXZlZ2FtaW5nbWVkaWEuY29tDAsSBENhc2UYwauk6QgM&key={YOUR_API_KEY}
This is the result I see in API explorer:
{
"kind": "calendar#events",
"etag": "\"*****\"",
"summary": "*****",
"description": "*****",
"updated": "2018-02-20T03:18:35.098Z",
"timeZone": "Asia/Tokyo",
"accessRole": "owner",
"defaultReminders": [
],
"nextSyncToken": "CJDv1KDEs9kCEJDv1KDEs9kCGAU=",
"items": [
]
}
Update 1:
My privateExtendedProperty looks like this:
{
"myStatus": "READY",
"myId": "agxzfm1haWxmb29nYWVyNQsSDE9yZ2FuaXphdGlvbiIVYWN0aXZlZ2FtaW5nbWVkaWEuY29tDAsSBENhc2UYkcHA3wgM",
"another": "Another value",
"another2": "Another value2"
}
When I query events with "myStatus=READY" - event is returned, but when I do the same with "myId=agxzfm1..." - it returns emtpy list.
Update 2:
For those who have the same issue. As per suggestion, I created a new calendar and then duplicated some test data and tried long value in privateExtendedProperty query and it returned event as expected. It means, if this is a bug - not all calendars are affected and moving things to a new calendar might be a way to go.
The document for Extended Properties says the limitations as follows.
The maximum size of a property's key is 44 characters, and properties with longer keys will be silently dropped.
The maximum size of a property's value is 1024 characters, and properties with longer values will be silently truncated.
An event can have up to 300 shared properties totaling up to 32kB in size (keys size + value size).
An event can have up to 300 private properties, totaling up to 32kB in size (keys size + value size), across all "copies" of the event.
From this, it seems that your case is out of the above limitations. In my environment, also I tried your situation. As the result, it was found that myId=agxzfm1haWxmb29nYWVyNQsSDE9yZ2FuaXphdGlvbiIVYWN0aXZlZ2FtaW5nbWVkaWEuY29tDAsSBENhc2UYkcHA3wgM can be used.
So can you confirm the following points again?
It seems that your result retrieved by API explorer doesn't included extendedProperties in the property of items. When you set extendedProperties, it can be seen. If you use fields, it might be not able to be seen.
Can you set extendedProperties with myId=agxzfm1haWxmb29nYWVyNQsSDE9yZ2FuaXphdGlvbiIVYWN0aXZlZ2FtaW5nbWVkaWEuY29tDAsSBENhc2UYkcHA3wgM again?
You can set it using calendar.events.patch.
When you retrieve events with myId=agxzfm1haWxmb29nYWVyNQsSDE9yZ2FuaXphdGlvbiIVYWN0aXZlZ2FtaW5nbWVkaWEuY29tDAsSBENhc2UYkcHA3wgM using calendar.events.list, if you use the URL encoded value of myId%3dagxzfm1haWxmb29nYWVyNQsSDE9yZ2FuaXphdGlvbiIVYWN0aXZlZ2FtaW5nbWVkaWEuY29tDAsSBENhc2UYkcHA3wgM, please try to use the value without URL encode.
If this was not useful for you and I misunderstand your question, I'm sorry.
Edit :
If you retrieve the event using calendar.events.get, can the extendedProperties you set be seen? If you cannot see it, please insert extendedProperties again.
The endpoint is GET https://www.googleapis.com/calendar/v3/calendars/### calendar Id ###/events/### event Id ###?fields=extendedProperties%2Fprivate
In my environment, the same endpoint of yours works fine. So as a test, can you try to create new calendar and run that query? Because I worried whether the recent update of calendar affects this situation.

Magento credit memo get offline or online refund status

I'm trying to add to the PDF creditmemo rather the refund was given offline or online.
How do I get this information from the creditmemo?
I've tried to use the method getTotalOfflineRefunded() but it comes back null everytime.
Is there an arg for getData() I can use to get this value?
The values you are searching for, are saved on the order not on the creditmemo
In the creditmemo model(Mage_Sales_Model_Order_Creditmemo) you will find something like this
$this->getOrder()->setTotalOfflineRefunded(
$this->getOrder()->getTotalOfflineRefunded()-$this->getGrandTota
);
$this->getOrder()->setBaseTotalOfflineRefunded(
$this->getOrder()->getBaseTotalOfflineRefunded()-$this->getBaseGrandTotal()
);
So to get the total offline refunded you should try this:
$order->load('{orderId}')->getBaseTotalOfflineRefunded();
In case the difference between getBaseTotalOfflineRefunded() and getTotalOfflineRefunded() is not clear:
Base always indicates the order amount in your "base" currency, while the other method gets the amount in the currency of the store where the order was placed in.

Resources