Browse Source

[IMP] emcc: catch 403, 404, 500 return code

pull/115/head
robin.keunen 5 years ago
parent
commit
feda238515
  1. 2
      easy_my_coop_connector/demo/demo.xml
  2. 21
      easy_my_coop_connector/models/emc_backend.py
  3. 4
      easy_my_coop_connector/models/subscription_request.py
  4. 4
      easy_my_coop_connector/tests/test_subscription_request.py

2
easy_my_coop_connector/demo/demo.xml

@ -19,6 +19,6 @@
<record id="emc_binding_product_template_B_demo" model="emc.binding.product.template"> <record id="emc_binding_product_template_B_demo" model="emc.binding.product.template">
<field name="backend_id" ref="emc_backend_demo"/> <field name="backend_id" ref="emc_backend_demo"/>
<field name="internal_id" ref="easy_my_coop.product_template_share_type_2_demo"/> <field name="internal_id" ref="easy_my_coop.product_template_share_type_2_demo"/>
<field name="external_id">38</field>
<field name="external_id">31</field>
</record> </record>
</odoo> </odoo>

21
easy_my_coop_connector/models/emc_backend.py

@ -6,9 +6,15 @@ import json
import logging import logging
import requests import requests
from werkzeug.exceptions import NotFound
from werkzeug.exceptions import (
InternalServerError,
NotFound,
)
from odoo import _, api, fields, models from odoo import _, api, fields, models
from odoo.exceptions import (
AccessDenied,
)
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
@ -40,10 +46,17 @@ class EMCBackend(models.Model):
if response.status_code == 200: if response.status_code == 200:
content = response.content.decode("utf-8") content = response.content.decode("utf-8")
return json.loads(content) return json.loads(content)
else:
# todo handle different codes (at least 404, 403, 500)
elif response.status_code == 403:
raise AccessDenied(_("You are not allowed to access this resource"))
elif response.status_code == 404:
raise NotFound( raise NotFound(
_("request returned status code %s" % response.status_code)
_("Resource not found %s on server" % response.status_code)
)
else: # 500 et al.
content = response.content.decode("utf-8")
raise InternalServerError(
_("request returned status code %s with message %s" % (
response.status_code, content))
) )
@api.multi @api.multi

4
easy_my_coop_connector/models/subscription_request.py

@ -54,13 +54,13 @@ class SubscriptionRequest(models.Model):
adapter = SubscriptionRequestAdapter(backend) adapter = SubscriptionRequestAdapter(backend)
sr_data = adapter.read(external_id) sr_data = adapter.read(external_id)
# todo 404
request_values = adapter.to_write_values(sr_data) request_values = adapter.to_write_values(sr_data)
sr_binding = SRBinding.search_binding(backend, external_id) sr_binding = SRBinding.search_binding(backend, external_id)
if sr_binding: # update request if sr_binding: # update request
srequest = sr_binding.internal_id.write(request_values)
srequest = sr_binding.internal_id
srequest.write(request_values)
else: else:
srequest = self.env["subscription.request"].create(request_values) srequest = self.env["subscription.request"].create(request_values)
SRBinding.create( SRBinding.create(

4
easy_my_coop_connector/tests/test_subscription_request.py

@ -31,7 +31,7 @@ SEARCH_RESULT = {
"lang": "en_US", "lang": "en_US",
"ordered_parts": 3, "ordered_parts": 3,
"name": "Manuel Dublues", "name": "Manuel Dublues",
"share_product": {"name": "Part B - Worker", "id": 38},
"share_product": {"name": "Part B - Worker", "id": 31},
"state": "draft", "state": "draft",
} }
], ],
@ -49,7 +49,7 @@ GET_RESULT = {
}, },
"lang": "en_US", "lang": "en_US",
"ordered_parts": 3, "ordered_parts": 3,
"share_product": {"name": "Part B - Worker", "id": 38},
"share_product": {"name": "Part B - Worker", "id": 31},
"state": "draft", "state": "draft",
} }

Loading…
Cancel
Save