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

[14.0] webservice: add webserviceConsumerMixin #22

Open
wants to merge 1 commit into
base: 14.0
Choose a base branch
from

Conversation

petrus-v
Copy link

@petrus-v petrus-v commented Jun 22, 2023

This is part of what has been discussed here: OCA/edi#776 (review)

In draft at the moment, I'd like to

  • Improve and discuss about design: I felt some abstraction is missing... I'll be back on it later
  • add test on the mixin

@OCA-git-bot
Copy link
Contributor

Hi @simahawk, @etobella,
some modules you are maintaining are being modified, check this out!

def _request(
self, method, url=None, url_params=None, consumer_record=None, **kwargs
):
if self.collection.save_response and isinstance(
Copy link
Contributor

Choose a reason for hiding this comment

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

You should wrap the whole existing code of this method w/ ctx manager to:

  1. check this only once
  2. store the reponse content even when request.raise_for_status at the end will brake the transaction

Also, why do you need this consumer_record?
The adapter should be agnostic and know nothing about such records.
I would wrap call instead and make it accept this param.
You can then define a method on the backend _must_save_response which checks the save_response flag and the consumer record if given.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for your review and feedback. I've refactor the code with this commit: 8862170 does it looks better ?

Copy link
Author

@petrus-v petrus-v Sep 5, 2023

Choose a reason for hiding this comment

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

hum... in fact while using with #oca/edi/787 I'm getting something weird as the main transction try to write on the same record as the new transcation to save the result, in the test I've done only the result paylaod was saved but the state of the edi.exchange.record was output_pending (I expected output_sent) 🤔

Copy link
Author

@petrus-v petrus-v Sep 5, 2023

Choose a reason for hiding this comment

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

logs confirmed that hypotesis

ERROR ** odoo.sql_db: bad query: UPDATE "edi_exchange_record" SET "edi_exchange_state"='output_sent',"exchanged_on"='2023-09-05 12:59:11',"write_uid"=62,"write_date"=(now() at time zone 'UTC') WH│
│ERROR: could not serialize access due to concurrent update

Not sure what's the best to do 🤔

Copy link
Author

Choose a reason for hiding this comment

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

I'm going to test this new version on real environement

def _compute_ws_response_content_filename(self):
for rec in self:
rec.ws_response_content_filename = (
"response_%s.json" % (str(rec.ws_response_status_code),)
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 add the formatted datetime string too

Copy link
Author

Choose a reason for hiding this comment

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

at the moment this is a computed field not stored 🤔 I could add a response_at datetime field 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

An alternative would be to relate the ir.attachment and read the write date from there...
No strong opinion on this but I think it would be nice to know when the response was recorded.

Copy link
Author

Choose a reason for hiding this comment

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

indeed it's a nice to have !

Copy link
Author

Choose a reason for hiding this comment

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

done by b5f8215

webservice/models/webservice_backend.py Outdated Show resolved Hide resolved
def _compute_ws_response_content_filename(self):
for rec in self:
rec.ws_response_content_filename = (
"response_%s.json" % (str(rec.ws_response_status_code),)
Copy link
Contributor

Choose a reason for hiding this comment

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

An alternative would be to relate the ir.attachment and read the write date from there...
No strong opinion on this but I think it would be nice to know when the response was recorded.

def call(self, method, *args, **kwargs):
return getattr(self._get_adapter(), method)(*args, **kwargs)
@contextmanager
def _consumer_record_env(self, record, new_cursor=True):
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 to understand why we need new_cursor flag. See below.

webservice/models/webservice_backend.py Show resolved Hide resolved
@petrus-v petrus-v force-pushed the 14.0-webservice-http_error_handler branch from b5f8215 to 8a29f55 Compare November 6, 2023 20:08
@petrus-v
Copy link
Author

petrus-v commented Nov 6, 2023

I've squashed commits and rebase the PR, also I've move the way to change the state on the consumer record adding method on the abstract method to make it easier the way to overwrite it as discussed OCA/edi#787 (comment)

Models inhering from this abstract model will get the webservice
response saved on it.
Copy link
Contributor

@simahawk simahawk left a comment

Choose a reason for hiding this comment

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

@petrus-v overall LG.

formatted_response_date = slugify(
rec.ws_response_date.strftime(DATETIME_FORMAT)
)
rec.ws_response_content_filename = "response_%s_%s.json" % (
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure assuming this is always json is ok. It can be pure txt or even html (unfortunately).
I'd leave it w/o ext for now. Not a blocker tho.

for rec in self:
if rec.ws_response_date and rec.ws_response_status_code:
formatted_response_date = slugify(
rec.ws_response_date.strftime(DATETIME_FORMAT)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
rec.ws_response_date.strftime(DATETIME_FORMAT)
fields.Datetime.to_string(rec.ws_response_date)

Comment on lines +106 to +108
content = request_error.response.content
status_code = request_error.response.status_code
raise request_error from request_error
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
content = request_error.response.content
status_code = request_error.response.status_code
raise request_error from request_error
content = request_error.response.content
status_code = request_error.response.status_code
raise

@gurneyalex gurneyalex added this to the 14.0 milestone Apr 26, 2024
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