I am quite surprised I couldn't find anything on this in my Google searching.
I'm using TwistedWeb to make a simple JSON HTTP API. I'd like to customize the 404 page so it returns something in JSON rather than the default HTML. How might I do this?
There is no API in Twisted Web like something.set404(someResource). A NOT FOUND response is generated as the default when resource traversal reaches a point where the next child does not exist - as indicated by the next IResource.getChildWithDefault call. Depending on how your application is structured, this means you may want to have your own base class implementing IResource which creates your custom NOT FOUND resource for all of its subclasses (or, better, make a wrapper since composition is better than inheritance).
If you read the implementation of twisted.web.resource.Resource.getChild you'll see where the default NOT FOUND behavior comes from and maybe get an idea of how to create your own similar behavior with different content.
Related
I'm trying to use the etsy API and I was finally able to get it running from the source. I gave it my key, and it returned the following when printed out.
<etsy._v2.EtsyV2 object at 0xb7284ccc>
However I have no idea what to do with it. The github-repo doesn't have much documentation, and the command that is suppose to follow doesn't work. I read the Etsy API and didn't find the mentioned command getFrontFeaturedListings like the github listed.
I've had this issue before with an HTTP response object and I was told to use response.content to check out more info on the object. It didn't work for this object so I'm wondering if there was a simple way to test any generic object, or at least see what this object contains?
When in doubt you can always use the dir built-in method on an arbitrary python object. This will show you methods and fields attached to the object. https://docs.python.org/3/library/functions.html#dir
Anyway, sorry to hear about the poor documentation of the library. Last time I used Etsy's API I just created a little class that used requests. It wasn't much work since Etsy lays out all of the URIs + documentation nicely on their developer site. https://www.etsy.com/developers/documentation/reference/favoritelisting
We are building an exensive api-link with the Exact online odata API. Problem we are having is that many objects cant be updated or deleted. For instance BankEntryLines, GeneralJournalEntryLines.
We have now worked around this by creating new EntryLines upon each update or delete, but this creates much unclarity in some cases.
Can the API be changed, or can I get extra authorization to be able to update or delete these objects, just like is possible in the GUI?
As the Exact Online REST API doesn't support modifying on quite some objects, there is no way to achieve what you want using the REST API. If the Exact Online XML API doesn't support updating either, there is only one solution left.
That solution is forbidden by Exact, and it could risk you lose you application developer status. You can make those changes using HTTP POSTS on the web site itself. If you can extract the calls that are made through the screens, you can mimic their behavior and by replaying that, you can modify what you need.
If you want to make a coupling to Exact Online and you are starting with developing, I want to suggest you to take a look at Invantive Data Hub, which allows updating Exact Online using SQL syntax. (To give full disclosure: I work for that company)
Is there any basic example that shows how to get POST parameters from the request in mod python custom handler. I have no trouble in GET requests, where I get my arguments from request.args, BUT if method is POST, request.args is None.
Thanks.
request.args stores query string parameters, as mentioned in the documentation.
If you want to get POST variables, you can always read the body of the request (request.read()) and parse it (urldecode in your case).
But keep in mind that, as mentioned on the official mod_python homepage:
Current State of Mod_Python
Currently mod_python is not under active development. This does not mean that it is "dead" as some people have claimed. It smiply means that the code and the project are mature enough when very little is required to maintain it.
Which means you may be better off using something more modern, like mod_wsgi.
I am writing an app in which users will be able to store information that they can specify a REST interface for. IE, store a list of products at /<username>/rest/products. Since the URLs are obviously not known before hand, I was trying to think of the best way to implement dynamic URL creation in Flask. The first way I thought of would be to write a catch-all rule, and route the URL from there. But then I am basically duplicating URL routing capabilities when Flask already has them built-in. So, I was wondering if it would be a bad idea to use .add_url_rule() (docs here, scroll down a bit) to attach them directly to the app. Is there a specific reason this shouldn't be done?
Every time you execute add_url_rule() the internal routing remaps the URL map. This is neither threadsafe nor fast. I right now don't understand why you need user specific URL rules to be honest. It kinda sounds like you actually want user specific applications mounted?
Maybe this is helpful: http://flask.pocoo.org/docs/patterns/appdispatch/
I have had similar requirement for my application where each endpoint /<SOMEID>/rest/other for given SOMEID should be bounded to a different function. One way to achieve this is keeping a lookup dictionary where values are the function that handle the specific SOMEID. For example take a look at this snippet:
func_look_up_dict = {...}
#app.route('<SOMEID>/rest/other', methods=['GET'])
def multiple_func_router_endpoint(SOMEID):
if SOMEID in func_look_up_dict.keys():
return jsonify({'result' = func_look_up_dict[SOMEID]()}), 200
else:
return jsonify({'result'='unknown', 'reason'='invalid id in url'}), 404
so for this care you don't really need to "dynamically" add url rules, but rather use a url rule with parameter and handle the various cases withing a single function. Another thing to consider is to really think about the use case of such URL endpoint. If <username> is a parameter that needs to be passed in, why not to use a url rule such as /rest/product/<username> or pass it in as an argument in the GET request?
Hope that helps.
I'm almost afraid to post this question, there has to be an obvious answer I've overlooked, but here I go:
Context: I am creating a blog for educational purposes (want to learn python and web.py). I've decided that my blog have posts, so I've created a Post class. I've also decided that posts can be created, read, updated, or deleted (so CRUD). So in my Post class, I've created methods that respond to POST, GET, PUT, and DELETE HTTP methods). So far so good.
The current problem I'm having is a conceptual one, I know that sending a PUT HTTP message (with an edited Post) to, e.g., /post/52 should update post with id 52 with the body contents of the HTTP message.
What I do not know is how to conceptually correctly serve the (HTML) edit page.
Will doing it like this: /post/52/edit violate the idea of URI, as 'edit' is not a resource, but an action?
On the other side though, could it be considered a resource since all that URI will respond to is a GET method, that will only return an HTML page?
So my ultimate question is this: How do I serve an HTML page intended for user editing in a RESTful manner?
Another RESTful approach is to use the query string for modifiers: /post/52?edit=1
Also, don't get too hung up on the purity of the REST model. If your app doesn't fit neatly into the model, break the rules.
There is no such thing as a RESTful URI. It is false concept as URIs should be completely opaque to the client.
If it helps you to properly implement the HTTP uniform interface by avoiding verbs in your URIs then that's great, but don't feel constrained by what your URI looks like. It is very limiting to think of resource modeling as type of data modelling. A RESTful system usually needs to do way more than just CRUD operations, so you need to be creative about what resources you make available in your system.
If you create a URL and dereferencing it returns a 200 status code, then that URL refers to a resource. If you create another URL and it also returns a 200, then that is a difference resource.
That means:
http://example.org/customer/10.xml
http://example.org/customer/10.json
http://example.org/customer/10?format=xml
http://example.org/customer/10?format=json
are 4 different resources, and
http://example.org/customers
http://example.org/customers?closed=true
http://example.org/customers?page=2&pagelength=20
are also different resources.
Therefore to answer your question, if you do
GET /post/52/edit
and it returns a 200 status code and a representation, then it must be a resource.
Instead of calling it /post/52/edit, what if you called it /post/52/editor?
Now it is a resource. Dilemma averted.
I don't think /object/id/action is part of the REST specification.
Is your editor going to be a generic thing for all objects ? Then maybe your URL should look like
/editor/object/id
The action is an HTTP Verb ( GET,PUT,DELETE,POST ) and is supposed to be a part of the HTTP request and not part of the URL. For a better summary, check out this Wikipedia article on RESTful_web_services.