I am using App engine, and I'm trying to get the time zone from the request.
However when on local host it always seems to return 'ZZ' as the country code which is not a country in pytz library.
This code:
country = self.request.headers['X-Appengine-Country']
logging.info(country)
tz = pytz.country_timezones(country)
produces this error:
return self.data[key.upper()]
KeyError: 'ZZ'
many thanks for your help
'ZZ' is often used to denote 'Unknown or unspecified country'
There is also a numeric version of the two letter code, calculated as 1070+30a+b, where a and b are the two letters of the code converted by A=1, B=2, etc. So AA=1101, AB=1102, BA=1131, and ZZ=1876.
I suggest that you use the correct case for the Request Header names. For e.g. X-AppEngine-Country
However, in the local development environment - I do not think the Location features will be supported i.e. you will not get the correct values. These should work only on the deployment environment. The Location is most likely provided by a Google Service that is internal to the Google Network and not exposed in the Local Development Environment.
Try to deploy your code to the live environment and check the values.
Related
I am trying to integrate a payment API in my application and one of the required field is the device fingerprint. I have googled and all I got was the fingerprint scanner and touch Id support which aren't what I need. Any ideas?
Realistically speaking, perhaps the simplest thing you can do is not to obtain or calculate a unique device identifier, but to assign each specific installation of your app a unique code (which will therefore change if the app is uninstalled and reinstalled).
I'll try to give you an example.
In the init() method of the main class, you can add this code:
// Register the device (assigning it an unique deviceId)
// The deviceId is used also by: https://www.codenameone.com/javadoc/com/codename1/io/Util.html#getUUID--
registerDevice();
Your registerDevice() implementation could set the deviceId to identify this app installation. The best and most reliable solution, in this case, is to get a UUID from your server, e.g. like this code:
// Sets the device unique identifier
Rest.get(Server.getRestServerURL() + "/uniqueIdentifier").fetchAsString((Response<String> response) -> {
String deviceId = response.getResponseData();
Preferences.set("deviceId", deviceId);
Log.p("DeviceId: " + deviceId, Log.INFO);
});
The server-side code is quite simple. Staying in the Java world, and assuming we have a Spring Boot server, the code could be:
#GetMapping("/uniqueIdentifier")
#ResponseBody
public String uniqueIdentifier() {
return UUID.randomUUID().toString().replace("-", "");
}
This solution should be enough. Note that UUIDs must be unique, but they are not designed to be unpredictable. You can see a discussion about this here: https://stackoverflow.com/a/41156/1277576
Alternatively, if your code is to be client-side only, read this Javadoc carefully: https://www.codenameone.com/javadoc/com/codename1/io/Util.html#getUUID--
Util.getUUID() returns a pseudo-random Universally Unique Identifier in its canonical textual representation. This could be enough in most cases.
These are just basic suggestions. Obviously you will need to develop the code so that the deviceId is requested once.
Since sometime after 3pm EST on January 9th I am getting
TypeError: 'Attachment' object does not support indexing errors when trying to access the data portion of an email attachment:
attach = mail_message.attachments.pop()
encodedAttachment = attach[1]
The format of the emails I am processing has not changed in that time, and this code worked flawlessly up until then
The latest version (1.8.9) has introduced an Attachment class that is returned now instead of the (filename content) tuple that was returned previously. The class does implement __iter__, so unpacking works exactly the same:
filename, content = attachment
But it doesn't implement __getitem__, so accessing via index as you're doing will cause the error you're seeing. It's possible that creating an issue will get the code changed to be completely backwards-compatible, but the practical thing would be to change your code.
Is it possible to get info on what instance you're running on? I want to output just a simple identifier for which instance the code is currently running on for logging purposes.
Since there is no language tag, and seeing your profile history, I assume you are using GAE/J?
In that case, the instance ID information is embedded in one of the environment attributes that you could get via ApiProxy.getCurrentEnvironment() method. You could then extract the instance id from the resulting map using key BackendService.INSTANCE_ID_ENV_ATTRIBUTE.
Even though the key is stored in BackendService, this approach will also work for frontend instances. So in summary, the following code would fetch the instance ID for you:
String tInstanceId = ApiProxy.getCurrentEnvironment()
.getAttributes()
.get( BackendService.INSTANCE_ID_ENV_ATTRIBUTE )
.toString();
Please keep in mind that this approach is quite undocumented by Google, and might subject to change without warning in the future. But since your use case is only for logging, I think it would be sufficient for now.
With the advent of Modules, you can get the current instance id in a more elegant way:
ModulesServiceFactory.getModulesService().getCurrentInstanceId()
Even better, you should wrap the call in a try catch so that it will work correctly locally too.
Import this
import com.google.appengine.api.modules.ModulesException;
import com.google.appengine.api.modules.ModulesServiceFactory;
Then your method can run this
String instanceId = "unknown";
try{
instanceId = ModulesServiceFactory.getModulesService().getCurrentInstanceId();
} catch (ModulesException e){
instanceId = e.getMessage();
}
Without the try catch, you will get some nasty errors when running locally.
I have found this super useful for debugging when using endpoints mixed with pub-sub and other bits to try to determine why some things work differently and to determine if it is related to new instances.
Not sure about before, but today in 2021 the system environment variable GAE_INSTANCE appears to contain the instance id:
instanceId = System.getenv("GAE_INSTANCE")
I have a legacy Asterisk application in C which does authentication of users, routing and billing using MySQL. I have kept it with Asterisk 1.4.21 because none of the CDR data is returned in newer versions of Asterisk.
Apparently there have been some changes in 1.4.22 https://issues.asterisk.org/jira/browse/ASTERISK-13064 that have completely changed the way CDR-s are handled. Unfortunately no helpful information was given on how to properly migrate existing code.
They have changed the order of execution, the 'h' extension is called and the CDR data is reset.
My code:
ast_log(LOG_NOTICE,"Dialing string: '%s'\n", dialstr);
app = pbx_findapp("Dial");
if (app)
res = pbx_exec(chan, app, dialstr);
ast_log(LOG_NOTICE,"Return from pbx_exec '%i', Disposition: '%s'\n", res, ast_cdr_disp2str(chan->cdr->disposition));
Other parts of the code handle chan->cdr->billsec etc, but it always gives 0 values.
After a successful call I always get this log from CLI:
Return from pbx_exec '-1', Disposition: 'NO ANSWER' while the same code works fine on 1.4.21
One solution I heard is to use ast_reset() before Dial but I am not sure how to implement it.
Any help on how to adapt this application?
You can just get DIALSTATUS variable,that is enought for you application and will be supported in future releases.
pbx_builtin_getvar_helper(chan, "DIALSTATUS");
Is there a simple way to get the current serving application version in AppEngine?
os.environ['CURRENT_VERSION_ID']
String version = SystemProperty.version.get();
String applicationVersion = SystemProperty.applicationVersion.get();
This is the syntax:
public static final SystemProperty applicationVersion
The major version number for the currently running version of the application plus a timestamp at which it was deployed. Has the key, "com.google.appengine.application.version".
See here
PS. One puzzle still remains. What does timestamp next to version means and how to read it??
EDIT: Here is the key to the mystery.
Date UploadDate = new Date(Long.parseLong(
applicationVersion.substring(applicationVersion.lastIndexOf(".")+1))
/ (2 << 27) * 1000);
For Python (GAE SDK release: "1.4.2")
version_id = self.request.environ["CURRENT_VERSION_ID"].split('.')[1]
timestamp = long(version_id) / pow(2,28)
version = datetime.datetime.fromtimestamp(timestamp).strftime("%d/%m/%y %X")
See http://groups.google.com/group/google-appengine-python/browse_thread/thread/f86010e7cf3c71b4
from google.appengine.api import modules
modules.get_current_version_name()
Source: https://cloud.google.com/appengine/docs/python/modules/functions
For nodejs, I am not sure if this is documented.
process.env.GAE_VERSION
You can also access the process' environment variables:
GAE_VERSION
which is available when you deploy (gcloud app deploy) using the flag --version
For those who want an update, environment variables set for a GAE instance as of September 2020:
GAE_VERSION is the one that seems to answer the original question.
Google doc:
https://cloud.google.com/appengine/docs/standard/python3/runtime#environment_variables
The following environment variables are set by the runtime:
Environment variable Description
GAE_APPLICATION The ID of your App Engine application. This ID is prefixed with 'region code~' such as 'e~' for applications deployed in Europe.
GAE_DEPLOYMENT_ID The ID of the current deployment.
GAE_ENV The App Engine environment. Set to standard.
GAE_INSTANCE The ID of the instance on which your service is currently running.
GAE_MEMORY_MB The amount of memory available to the application process, in MB.
GAE_RUNTIME The runtime specified in your app.yaml file.
GAE_SERVICE The service name specified in your app.yaml file. If no service name is specified, it is set to default.
GAE_VERSION The current version label of your service.
GOOGLE_CLOUD_PROJECT The Cloud project ID associated with your application.
PORT The port that receives HTTP requests.
Based on my experiments today, there are two os.environ variables that you can use to get the current app version:
os.environ['GAE_VERSION']: the version name only
os.environ['CURRENT_VERSION_ID']: a unique version identifier composed of {version name}.{deployment id}, which is equivalent to os.environ['GAE_VERSION'] + '.' + os.environ['GAE_DEPLOYMENT_ID']
It appears that the so-called "deployment id" can be right-shifted 28 bits to get a timestamp in epoch seconds (as other answers already described).
For example: I deployed version "101" of my app at 2021-03-04T00:17:12Z and I'm seeing the following values:
os.environ['GAE_VERSION']: '101'
os.environ['CURRENT_VERSION_ID']: '101.433474146608888597'
os.environ['GAE_DEPLOYMENT_ID']: '433474146608888597'
You can use the following code to get the version name and timestamp from os.environ['CURRENT_VERSION_ID']:
>>> import os
>>> import datetime
>>> version_id = os.environ['CURRENT_VERSION_ID'] # example: '101.433474146608888597'
>>> name, ts = version_id.split('.')
>>> dt = datetime.datetime.utcfromtimestamp(int(ts) >> 28))
>>> dt.isoformat()
'2021-03-04T00:17:12'
Disclaimer: Most of this functionality is undocumented and the deployment ID format may be subject to change.