-
Notifications
You must be signed in to change notification settings - Fork 0
[5192][ADD] base_api_connection #59
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
267ac50
[3875][ADD] base_api_connection (#132)
AungKoKoLin1997 ec08e2e
[IMP] base_api_connection: pre-commit stuff
AungKoKoLin1997 9f9b1e0
[ADD] base_api_connection
AungKoKoLin1997 ba750d8
[IMP] base_api_connection: add token_type
AungKoKoLin1997 9cd4d6d
fix pre-commit
AungKoKoLin1997 ca21b02
upd for http error detail
nobuQuartile d1c1931
add logger about http error
nobuQuartile 5084f36
add conditional branch about endpoint and params
nobuQuartile e9aa383
upd from Yoshi-san's comment 1
nobuQuartile File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| =================== | ||
| Base API Connection | ||
| =================== | ||
|
|
||
| .. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
| !! This file is generated by oca-gen-addon-readme !! | ||
| !! changes will be overwritten. !! | ||
| !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
|
|
||
| .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png | ||
| :target: https://odoo-community.org/page/development-status | ||
| :alt: Beta | ||
| .. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png | ||
| :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html | ||
| :alt: License: LGPL-3 | ||
| .. |badge3| image:: https://img.shields.io/badge/github-qrtl%2Faxls--oca-lightgray.png?logo=github | ||
| :target: https://github.com/qrtl/axls-oca/tree/16.0/base_api_connection | ||
| :alt: qrtl/axls-oca | ||
|
|
||
| |badge1| |badge2| |badge3| | ||
|
|
||
| This module facilitates API connections between Odoo and other web services. It is not usable on its own; rather, it is a low-level module intended to serve as a base for others. An example of such a module is 'project_task_capture'. | ||
|
|
||
| **Table of contents** | ||
|
|
||
| .. contents:: | ||
| :local: | ||
|
|
||
| Configuration | ||
| ============= | ||
|
|
||
| 1. Go to Settings > API Connection > API Configuration. | ||
| 2. Create an API configuration record with 'base_url', 'header_api_key_string', 'code', and 'x_api_key'. | ||
|
|
||
| Bug Tracker | ||
| =========== | ||
|
|
||
| Bugs are tracked on `GitHub Issues <https://github.com/qrtl/axls-oca/issues>`_. | ||
| In case of trouble, please check there if your issue has already been reported. | ||
| If you spotted it first, help us smashing it by providing a detailed and welcomed | ||
| `feedback <https://github.com/qrtl/axls-oca/issues/new?body=module:%20base_api_connection%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_. | ||
|
|
||
| Do not contact contributors directly about support or help with technical issues. | ||
|
|
||
| Credits | ||
| ======= | ||
|
|
||
| Authors | ||
| ~~~~~~~ | ||
|
|
||
| * Quartile Limited | ||
|
|
||
| Maintainers | ||
| ~~~~~~~~~~~ | ||
|
|
||
| This module is part of the `qrtl/axls-oca <https://github.com/qrtl/axls-oca/tree/16.0/base_api_connection>`_ project on GitHub. | ||
|
|
||
| You are welcome to contribute. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from . import models |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Copyright 2023 Quartile (https://www.quartile.co) | ||
| # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). | ||
| { | ||
| "name": "Base API Connection", | ||
| "version": "15.0.1.0.0", | ||
| "category": "API", | ||
| "website": "https://www.quartile.co", | ||
| "author": "Quartile, Odoo Community Association (OCA)", | ||
| "license": "LGPL-3", | ||
| "depends": ["base"], | ||
| "data": [ | ||
| "security/ir.model.access.csv", | ||
| "views/api_config_views.xml", | ||
| ], | ||
| "installable": True, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| from . import api_config | ||
| from . import api_call_mixin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # Copyright 2023 Quartile (https://www.quartile.co) | ||
| # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). | ||
|
|
||
| import logging | ||
|
|
||
| import requests | ||
|
|
||
| from odoo import _, models | ||
| from odoo.exceptions import UserError | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class APICallMixin(models.AbstractModel): | ||
| _name = "api.call.mixin" | ||
| _description = "API Call Mixin" | ||
|
|
||
| def get_api_key(self, config): | ||
| if config.token_type: | ||
| return f"{config.token_type} {config.api_key}" | ||
| return config.api_key | ||
|
|
||
| def make_api_call( | ||
| self, | ||
| code, | ||
| external_system="generic", | ||
| endpoint=None, | ||
| custom_headers=None, | ||
| params=None, | ||
| json=None, | ||
| http_method="get", | ||
| ): | ||
| config = self.env["api.config"].search( | ||
| [("external_system", "=", external_system), ("code", "=", code)], limit=1 | ||
| ) | ||
| if not config: | ||
| raise UserError(_("API configuration not found.")) | ||
| url = f"{config.base_url.rstrip('/')}" | ||
| if endpoint: | ||
| url = f"{url}/{endpoint}" | ||
| headers = {"Content-Type": "application/json"} | ||
| if custom_headers: | ||
| headers.update(custom_headers) | ||
| api_key = self.get_api_key(config) | ||
| headers[config.header_api_key_string] = api_key | ||
| function = getattr(requests, http_method) | ||
| kwargs = {"headers": headers} | ||
| if params: | ||
| kwargs["params"] = params | ||
| if json is not None: | ||
| kwargs["json"] = json | ||
| try: | ||
| response = function(url, **kwargs) | ||
| response.raise_for_status() # Raises HTTPError for bad responses | ||
| _logger.info( | ||
| f"Successful API call to {url}. Response status code: {response.status_code}" | ||
| ) | ||
| except requests.exceptions.HTTPError as e: | ||
| _logger.error( | ||
| "HTTP Error: %s \n%s", | ||
| e.response.status_code, | ||
| e.response.text[:500], | ||
| exc_info=True, | ||
| ) | ||
| raise UserError(f"HTTP Error: {str(e)}") from e | ||
| except requests.exceptions.RequestException as e: | ||
| raise UserError(f"Request Error: {str(e)}") from e | ||
| return response |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # Copyright 2023 Quartile (https://www.quartile.co) | ||
| # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). | ||
|
|
||
| from odoo import _, api, fields, models | ||
| from odoo.exceptions import ValidationError | ||
|
|
||
|
|
||
| class ApiConfig(models.Model): | ||
| _name = "api.config" | ||
| _description = "API Configuration" | ||
|
|
||
| name = fields.Char(required=True) | ||
| code = fields.Char( | ||
| required=True, | ||
| help="Expected to act as an identifier of the API configuration " | ||
| "record along with External System.", | ||
| ) | ||
| base_url = fields.Char(string="URL") | ||
| header_api_key_string = fields.Char( | ||
| required=True, | ||
| help="This string will be used as the key in the API header for the API key value.", | ||
| ) | ||
| external_system = fields.Selection( | ||
| [("generic", "Generic")], default="generic", required=True | ||
| ) | ||
| token_type = fields.Char("Api Token Type") | ||
| api_key = fields.Char(string="Api Key or Token", required=True) | ||
|
|
||
| @api.constrains("code") | ||
| def _check_code(self): | ||
| for record in self: | ||
| existing_rec = self.search( | ||
| [("code", "=", record.code), ("id", "!=", record.id)], | ||
| ) | ||
| if existing_rec: | ||
| raise ValidationError(_("Code must be unique.")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| 1. Go to Settings > API Connection > API Configuration. | ||
| 2. Create an API configuration record with 'base_url', 'header_api_key_string', 'code', and 'x_api_key'. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| This module facilitates API connections between Odoo and other web services. It is not usable on its own; rather, it is a low-level module intended to serve as a base for others. An example of such a module is 'project_task_capture'. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink | ||
| access_api_config_manager,api.config,model_api_config,base.group_system,1,1,1,1 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure we should still aim to put this module in the OCA, but
project_task_captureis certainly not part of it.