Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use bytes or unicode to get args #164

Closed
wants to merge 6 commits into from

Conversation

notoriousno
Copy link
Contributor

@codecov-io
Copy link

codecov-io commented Jan 12, 2017

Codecov Report

Merging #164 into master will increase coverage by 0.15%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #164      +/-   ##
==========================================
+ Coverage   95.88%   96.03%   +0.15%     
==========================================
  Files          12       13       +1     
  Lines        1530     1589      +59     
  Branches       87       88       +1     
==========================================
+ Hits         1467     1526      +59     
  Misses         52       52              
  Partials       11       11
Impacted Files Coverage Δ
src/klein/resource.py 92.1% <100%> (+0.92%) ⬆️
src/klein/test/test_app.py 98.55% <100%> (ø) ⬆️
src/klein/test/test_request.py 100% <100%> (ø)
src/klein/app.py 98.6% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 22d3278...44aac2a. Read the comment docs.

Copy link
Member

@glyph glyph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all your work on Klein! I do really like where you're going with this - an os.path-style compatibility approach to bytes vs. text here does make sense. However, I think it needs to be a bit better thought out (especially the compatibility contract around KleinSite before it's ready. Please address the feedback here and re-submit.

Thanks again!


def getArg(self, key):
"""
Get a single arg value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot to document @param key, in particular @type key, which seems like rather the whole point of this change :).


class KleinHTTPRequest(server.Request):

def getArg(self, key):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't like the name getArg. "args" are an incredibly ill-defined mess in Twisted, mixing together query parameters ("GET args") and URL-encoded or multipart form-values in the body ("POST args" or "form args"). If we're going to try to do something better, it would be nice to clean this up a bit. Even if it is accessing the underlying .args, I feel like we should be disambiguating these namespaces.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a big fan of getters and setters either but it was the simple and easy to understand. I had a more elaborate solution using a dict-like object for the args variable but other devs weren't convinced with it and ultimatly it was scraped in favor of getArgs. As far as the Twisted mess, you provided a ticket #228 but work on this might have stalled. Any hope of reviving that?

Get a single arg value.

@raises KeyError: If key doesn't exist
@raises ValueError: If there is more than 1 value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot about UnicodeDecodeError, a natural consequence of ensure_utf8_bytes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch




class KleinSite(server.Site):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intended to be public? Do we need to use it for all Klein applications now? That seems like a pretty big change to quietly be foisting on all Klein applications. (That implies .app.resource() won't work in a normal server any more, or will randomly be missing some methods.)

If it is intended to be public, some docstrings seems like a bare minimum. Also, Site is an old-style class, so this should definitely have an , object at the end of its inheritance hierarchy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a rather ugly hack on my part and I'd like to move away from it. At the time I did not know of alternative ways to overload Request. In retrospect, I should've created an Request adapter or simply append the function to the request object in app.route(). Any thoughts on how to go about this?

class KleinSite(server.Site):
def buildProtocol(self, addr):
channel = http.HTTPFactory.buildProtocol(self, addr)
channel.requestFactory = KleinHTTPRequest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered using the requestFactory argument to Site.__init__ rather than having a custom subclass?

@wsanchez
Copy link
Member

wsanchez commented Jun 1, 2017

Note that I've taken a stab at a new IHTTPRequest interface which is intended to replace IRequest:

#181

The goal would be to obsolete the KleinHTTPRequest class. In the new API, you'd get request.url and then work with the URL API to get the query args.

@notoriousno: What are your thoughts on this?

@glyph
Copy link
Member

glyph commented Jun 21, 2017

Normally I'm a fan of small, incremental improvements.

Unfortunately, in this case, I think that we need a comprehensive re-design to make user education tractable. If we fix a sprawling core interface like IRequest in little dribbles like this, I fear that we'll just end up with 19 different ways to get .args, each one behaving slightly differently (although slightly better than the last!), which is actually a worse situation than one bad interface where everyone knows the pitfalls to work around.

So, given @wsanchez's work on #181, which I think has a better hope of becoming

“The New Request Interface For Twisted”

I think we would, sadly, be better off abandoning this work.

Nevertheless, thank you, @notoriousno for submitting it. This is definitely a problem area and one that it's tough to work on.

@glyph glyph closed this Jun 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants