Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
api
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cubicweb
cubes
api
Commits
2af675c2fd36
Commit
2af675c2fd36
authored
2 years ago
by
Arnaud Vergnet
Browse files
Options
Downloads
Patches
Plain Diff
feat: store api routes in enum
parent
52ea512c28b5
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!7
feat: use pyramid decorators and rework error handling
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
cubicweb_api/routes.py
+22
-20
22 additions, 20 deletions
cubicweb_api/routes.py
with
22 additions
and
20 deletions
cubicweb_api/routes.py
+
22
−
20
View file @
2af675c2
...
...
@@ -14,6 +14,7 @@
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from
enum
import
Enum
from
typing
import
Union
from
cubicweb
import
AuthenticationError
...
...
@@ -52,6 +53,17 @@
}
class
ApiRoutes
(
str
,
Enum
):
schema
=
"
schema
"
rql
=
"
rql
"
login
=
"
login
"
transaction_begin
=
"
transaction/begin
"
transaction_execute
=
"
transaction/execute
"
transaction_commit
=
"
transaction/commit
"
transaction_rollback
=
"
transaction/rollback
"
help
=
"
help
"
def
get_cw_request
(
request
:
Request
)
->
CubicWebPyramidRequest
:
return
request
.
cw_request
...
...
@@ -67,7 +79,7 @@
)
@cw_view_config
(
route_name
=
"
schema
"
,
request_method
=
"
GET
"
)
@cw_view_config
(
route_name
=
ApiRoutes
.
schema
,
request_method
=
"
GET
"
)
def
schema_route
(
request
:
Request
):
repo
=
get_cw_repo
(
request
)
exporter
=
JSONSchemaExporter
()
...
...
@@ -80,7 +92,7 @@
return
exported_schema
@cw_view_config
(
route_name
=
"
rql
"
)
@cw_view_config
(
route_name
=
ApiRoutes
.
rql
)
def
rql_route
(
request
:
Request
):
schema
=
class_schema
(
RqlParams
)()
request_params
:
RqlParams
=
get_request_params
(
request
,
schema
)
...
...
@@ -96,7 +108,7 @@
raise
get_http_error
(
400
,
e
.
__class__
.
__name__
,
str
(
e
))
@cw_view_config
(
route_name
=
"
login
"
)
@cw_view_config
(
route_name
=
ApiRoutes
.
login
)
def
login_route
(
request
:
Request
):
schema
=
class_schema
(
LoginParams
)()
request_params
:
LoginParams
=
get_request_params
(
request
,
schema
)
...
...
@@ -122,10 +134,10 @@
request
.
response
.
status_code
=
204
@cw_view_config
(
route_name
=
"
transaction
/
begin
"
)
@cw_view_config
(
route_name
=
ApiRoutes
.
transaction
_
begin
)
def
transaction_begin_route
(
request
:
Request
):
transactions
=
get_cw_repo
(
request
).
api_transactions
user
=
get_cw_request
(
request
).
user
return
transactions
.
begin_transaction
(
user
)
...
...
@@ -126,10 +138,10 @@
def
transaction_begin_route
(
request
:
Request
):
transactions
=
get_cw_repo
(
request
).
api_transactions
user
=
get_cw_request
(
request
).
user
return
transactions
.
begin_transaction
(
user
)
@cw_view_config
(
route_name
=
"
transaction
/
execute
"
)
@cw_view_config
(
route_name
=
ApiRoutes
.
transaction
_
execute
)
def
transaction_execute_route
(
request
:
Request
):
transactions
=
get_cw_repo
(
request
).
api_transactions
schema
=
class_schema
(
TransactionExecuteParams
)()
...
...
@@ -142,7 +154,7 @@
raise
get_http_error
(
400
,
e
.
__class__
.
__name__
,
str
(
e
))
@cw_view_config
(
route_name
=
"
transaction
/
commit
"
)
@cw_view_config
(
route_name
=
ApiRoutes
.
transaction
_
commit
)
def
transaction_commit_route
(
request
:
Request
):
transactions
=
get_cw_repo
(
request
).
api_transactions
schema
=
class_schema
(
TransactionParams
)()
...
...
@@ -157,7 +169,7 @@
raise
get_http_error
(
400
,
e
.
__class__
.
__name__
,
str
(
e
))
@cw_view_config
(
route_name
=
"
transaction
/
rollback
"
)
@cw_view_config
(
route_name
=
ApiRoutes
.
transaction
_
rollback
)
def
transaction_rollback_route
(
request
:
Request
):
transactions
=
get_cw_repo
(
request
).
api_transactions
schema
=
class_schema
(
TransactionParams
)()
...
...
@@ -172,9 +184,9 @@
raise
get_http_error
(
400
,
e
.
__class__
.
__name__
,
str
(
e
))
@cw_view_config
(
route_name
=
"
help
"
,
request_method
=
"
GET
"
)
@cw_view_config
(
route_name
=
ApiRoutes
.
help
,
request_method
=
"
GET
"
)
def
help_route
(
_request
:
Request
):
"""
TO IMPLEMENT
"""
return
...
...
@@ -176,10 +188,6 @@
def
help_route
(
_request
:
Request
):
"""
TO IMPLEMENT
"""
return
def
add_route
(
config
:
Configurator
,
name
:
str
):
config
.
add_route
(
f
"
{
API_PATTERN_PREFIX
}{
name
}
"
,
f
"
/
{
name
}
"
)
def
add_cw_routes
(
config
:
Configurator
):
...
...
@@ -185,12 +193,6 @@
def
add_cw_routes
(
config
:
Configurator
):
add_route
(
config
,
"
schema
"
)
add_route
(
config
,
"
rql
"
)
add_route
(
config
,
"
login
"
)
add_route
(
config
,
"
transaction/begin
"
)
add_route
(
config
,
"
transaction/execute
"
)
add_route
(
config
,
"
transaction/commit
"
)
add_route
(
config
,
"
transaction/rollback
"
)
add_route
(
config
,
"
help
"
)
for
r
in
ApiRoutes
:
config
.
add_route
(
f
"
{
API_PATTERN_PREFIX
}{
r
}
"
,
f
"
/
{
r
}
"
)
config
.
scan
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment