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

Support parsing batch results #146

Merged
merged 7 commits into from
May 23, 2017
Merged

Support parsing batch results #146

merged 7 commits into from
May 23, 2017

Conversation

gabisurita
Copy link
Member

@gabisurita gabisurita commented May 21, 2017

Fixes/Updates #145

This may not be the best API for this feature, but it does support the use case for dumping records on kinto-wizard (Kinto/kinto-wizard#16).

A small example:

from kinto_http import Client

client = Client('http://localhost:8888/v1',
                auth=('gabi', 'ilovecats'),
                collection='status')

with client.batch() as batch:
    batch.update_record(id='r1', data={'busy': True})
    batch.get_record('r1')

set_r1, get_r1 = batch.results()
assert set_r1 == get_r1

Todos:

  • Add unit tests
  • Update CHANGELOG and README

Opinions?

Support parsing results from batch requests with a parse_results
method and a results attribute on a Batch context client after the
batch operation is sent.
@gabisurita gabisurita requested a review from leplatrem May 21, 2017 20:21
@gabisurita gabisurita changed the title [WIP] Support parsing batch results Support parsing batch results May 22, 2017
Copy link
Contributor

@leplatrem leplatrem left a comment

Choose a reason for hiding this comment

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

Mostly wording :)

Thanks !!!

CHANGELOG.rst Outdated
@@ -7,7 +7,7 @@ This document describes changes between each past release.
8.1.0 (unreleased)
==================

- Nothing changed yet.
- Allow reading from batch contexts using the ``parse_results`` method. (#146)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest to be more human friendly with something like:

Allow reading responses from batch requests with the ``results()`` method. (#146)

README.rst Outdated
batch.update_record(data={'id': idx})

A batch object shares the same methods as another client.
Reading data from batch operations is achived by using the ``parse_results`` method
Copy link
Contributor

Choose a reason for hiding this comment

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

when mentionning a method, it's nicer if we put the parenthesis ()

self.results.append((resp, headers))
return self.results

def parse_results(self):
Copy link
Contributor

@leplatrem leplatrem May 22, 2017

Choose a reason for hiding this comment

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

results() should be enough IMO. Parsing is not truely the action here


def parse_results(self):
# Get each batch block response
block_responses = [resp for resp, _ in self.results]
Copy link
Contributor

Choose a reason for hiding this comment

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

chunks would be easier to understand since we use this term previously

@@ -112,3 +112,25 @@ def test_batch_raises_exception_as_soon_as_subrequest_fails(self):
with self.assertRaises(KintoException):
batch.send()
assert self.client.session.request.call_count == 1

def test_results_attribute_is_available(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if we have both. Especially because this results attributes is chunked. I know we'd lose the headers information, but I would rather make it private. Especially if we don't make it official in docs.

@glasserc
Copy link
Contributor

I am not crazy about the API because it seems forwards-incompatible for doing things like sending a batch of updates as part of an offline-first sync() call. I get that simple things should be simple, but this makes complicated things a little harder. But we can always just break compatibility later..

@gabisurita gabisurita force-pushed the feat-parse-batch-results branch 2 times, most recently from 56584b1 to f46a1e6 Compare May 23, 2017 02:25
@gabisurita
Copy link
Member Author

gabisurita commented May 23, 2017

I am not crazy about the API because it seems forwards-incompatible for doing things like sending a batch of updates as part of an offline-first sync() call. I get that simple things should be simple, but this makes complicated things a little harder. But we can always just break compatibility later..

I agree this API is limited, but honestly I think we should support both a simple way to fetch only the response body and another to fetch the full response (mostly for internal stuff). I can't see a way to have an easy to use and fully forward-compatible call, do you have something in mind?

Copy link
Contributor

@leplatrem leplatrem left a comment

Choose a reason for hiding this comment

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

A few nits left, but then I'm ok to merge :)

README.rst Outdated
batch.get_record('r2')
batch.get_record('r3')

r1, r2, r3 = batch.parse_results()
Copy link
Contributor

Choose a reason for hiding this comment

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

leftover :)

README.rst Outdated
batch.update_record(data={'id': idx})

A batch object shares the same methods as another client.
Reading data from batch operations is achived by using the ``results()`` method
available ater a batch context is closed.
Copy link
Contributor

Choose a reason for hiding this comment

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

achieved

after

README.rst Outdated

r1, r2, r3 = batch.parse_results()

Besides the ``parse_results`` method, a batch object shares all the same methods as
Copy link
Contributor

Choose a reason for hiding this comment

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

leftover

@@ -97,6 +97,10 @@ def batch(self, **kwargs):
batch_session = BatchSession(self,
batch_max_requests=batch_max_requests)
batch_client = self.clone(session=batch_session, **kwargs)

# set a reference for reading results from the context
Copy link
Contributor

Choose a reason for hiding this comment

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

µnit: capitalize first word and add trailing .

@@ -43,7 +44,6 @@ def _build_requests(self):
return requests

def send(self):
result = []
Copy link
Contributor

Choose a reason for hiding this comment

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

don't we want to reset self._results here?

Side note: dont code in a hurry...
@leplatrem leplatrem merged commit 8fc639e into master May 23, 2017
@leplatrem leplatrem deleted the feat-parse-batch-results branch May 23, 2017 13:08
@glasserc
Copy link
Contributor

I can't see a way to have an easy to use and fully forward-compatible call, do you have something in mind?

What I would normally do is return a Response object which has a body attribute and/or a json property/method. But this is fine for now. Nice to see a PR from you again :)

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.

3 participants