We need an i18n API that supports a lot of languages in its queries. What is the best way of use the "Accept-Languages" header key? I'm thinking on having a collection for each language and do a transparent query on the correspondant collection. Or do you think it's better to hardcode the language in the uri?
You can try this flask snippet: http://flask.pocoo.org/snippets/128/
Anyway using Accept-Language header is not good for this and better to use cookie, but you can make fallback to this header's information if cookie does not exists, to receive header content you can use flask.request.headers.get('Accept-Language') but keep in mind that this header might contain not single language, but for example something like this: da, en-gb;q=0.8, en;q=0.7
You can use before_request decorator to recognize language before request. Something like this:
#app.before_request
def before_request():
flask.request.lang = flask.request.cookies.get('lang')
if lang is None:
flask.request.lang = flask.request.headers.get('Accept-Language', 'en').split(' ')[0]
And than you can use flask.request.lang anywhere you need this.
Related
Because I am rewriting a legacy app, I cannot change what the clients either send or accept. I have to accept and return JSON, HTML, and an in-house XML-like serialization.
They do, fortunately set headers that describe what they are sending and what they accept.
So right now, what I do is have a decoder module and an encoder module with methods that are basically if/elif/else chains. When a route is ready to process/return something, I call the decoder/encoder module with the python object and the header field, which returns the formatted object as a string and the route processes the result or returns Response().
I am wondering if there is a more Quart native way of doing this.
I'm also trying to figure out how to make this work with Quart-Schema. I see from the docs that one can do app.json_encoder = <class> and I suppose I could sub in a different processor there, but it seems application global, there's no way to set it based on what the client sends. Optimally, it would be great if I could just pass the results of a dynamically chosen parser to Quart-Schema and let it do it's thing on python objects.
Thoughts and suggestions welcome. Thanks!
You can write your own decorator like the quart-schema #validation_headers(). Inside the decorator, check the header for the Content-Type, parse it, and pass the parsed object to the func(...).
is there an API in blackberry that can validate URL ?
i'm using stupid method startsWith(http://)
and read the Index of the string to make sure it contain ".com/" and other strings to ensure it look like a validate URL.
but i feel like it's a stupid and long way to use.
is there any API that can make it easier ?
I'm looking for away to make it within blackberry APIs not external packages
In the API the only classes are:
URLTextFilter: Useful only to add it to a text field (Call EditField.setFilter). It will discard invalid chars.
URI: It lets you to validate a URI by calling URI.create. Of course, not all URIs are URLs, but URLs should be URIs.
I find this behavior a bit strange, I'm using 'net\http' to do some restful communication to an internal API. For this I need to send a multipart/form-data request to our server. In my code I have this:
request["Content-Type"] = "multipart/form-data, boundary=AbCdE1"
The request created then looks like this:
"multipart/form-data, boundary=abcde1"
The problem is the body itself is using AbCdE1 for its boundaries and it fails. Obviously I could use just lowercase letters, but for a more reliably unique boundary, having capitals is helpful. I have seen comments that rails makes headers lowercase, is there a good reason why it does this without my intervention?
The correct delimiter for the Content-Type and the boundary is a semicolon... Have you tried to use a semicolon instead of the comma, to see if Rails still translate it to lowercase?
BTW check http://httparty.rubyforge.org/ for consuming RESTful webservices, the result is more elegant than writing net/http code.
I'd like to be able to change the extension of a url and recieve the model in a different format.
e.g. if
/products/list
returns a html page containing a list of products, then
/products/list.json
would return them in a json list.
Note: I like the simplicity of the ASP.NET MVC REST SDK, it only requires about 5 lines of code to hook it in, but the format is specified as a query string parameter i.e. /products/list?format=json which is not what I want, I could tweak this code if there are no other options, but I don't want to reinvent the wheel!
I wrote a blog post that shows one possible example. It's a tiny bit complicated, but might work for your needs.
http://haacked.com/archive/2009/01/06/handling-formats-based-on-url-extension.aspx
You should be able to just use routes in conjunction with the rest sdk
If you have the flexibility to drop Apache or something similar in front of your service, you can always use mod_rewrite to rewrite an external http://products/list.json into http://products/list?format=json which your framework can render more easily.
Instead of "list.json", you could choose "list/json" and use a route like
{controller}/{action}/{id}
Then ProductController.List would be called, with an ID parameter of "json". The .List() action then would decide whether or not to return an HTML view or JSON content.
I have to pass parameters between two rails apps. In one side (sender) I have an array of hashes. I have a code like the following to send the data:
http = Net::HTTP.new('localhost', '3030')
result = http.post('/processar_lotes', my_array_of_hashes)
Some questions
Is there any (kind of) serialize or something like this that I can pass to the other app?
At the other side, how can I de-serialize the information?
Is there a limit to the size of what I pass as a parameter?
To answer your questions:
There are many ways to 'serialize' the data. You can use your own custom format, or use a standard one. For example, you can try to use the Rails to_xml method, or the to_json method. You can also use Ruby's Marshal object.
Depending on your choice, this might be from_json, from_xml, Marshal.load, or your own custom reader.
Normally, this is unlimited for HTTP posts, but depending on your server configuration, it could be less.
Probably not the answer you're looking for, but I'd use XML. This would make your application much more flexible than using language-specific serialization.
It shouldn't be too hard to convert the array to XML and back.
EDIT: You might wanna check out ROXML and XML::Mapping.