Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
workspaces
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
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ssa
workspaces
Merge requests
!73
SWS-31: Capability REST API update
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
SWS-31: Capability REST API update
SWS-31-capability-rest-api-update
into
main
Overview
4
Commits
17
Pipelines
8
Changes
10
Merged
Nathan Hertz
requested to merge
SWS-31-capability-rest-api-update
into
main
4 years ago
Overview
4
Commits
17
Pipelines
8
Changes
7
Expand
Goals of this story:
Overhaul capability server layout
Separate files for server entry point and server routes
Separate package for view logic
Server entry point properly collects all routes and views
Modify existing routes/views to match standard agreed upon a while back (can be found
here
)
Utilize more Pyramid features to make the server more robust
Add unit tests!
Edited
4 years ago
by
Nathan Hertz
0
0
Merge request reports
Compare
version 1
version 7
25a3dc68
4 years ago
version 6
c0f7103a
4 years ago
version 5
6a74e24f
4 years ago
version 4
e822bae0
4 years ago
version 3
06cd8b90
4 years ago
version 2
f584762d
4 years ago
version 1
c0a826b8
4 years ago
main (base)
and
version 3
latest version
ccf463fd
17 commits,
4 years ago
version 7
25a3dc68
16 commits,
4 years ago
version 6
c0f7103a
15 commits,
4 years ago
version 5
6a74e24f
15 commits,
4 years ago
version 4
e822bae0
15 commits,
4 years ago
version 3
06cd8b90
14 commits,
4 years ago
version 2
f584762d
11 commits,
4 years ago
version 1
c0a826b8
8 commits,
4 years ago
Show latest version
7 files
+
172
−
33
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
7
Search (e.g. *.vue) (Ctrl+P)
services/capability/src/capability/views/capability.py
+
51
−
6
Options
import
json
from
pyramid.httpexceptions
import
HTTPNotFound
from
pyramid.httpexceptions
import
HTTPBadRequest
,
HTTPNotFound
,
HTTPPreconditionFailed
from
pyramid.request
import
Request
from
pyramid.response
import
Response
from
pyramid.view
import
view_config
from
workspaces.capability.schema
import
Capability
@view_config
(
route_name
=
"
view_capability
"
,
renderer
=
"
json
"
)
def
view_capability
(
request
):
def
view_capability
(
request
:
Request
)
->
Response
:
"""
Pyramid view that accepts a request to view a capability and responds with the requested capability
'
s
info, if it exists
URL: capability/{capability_name}
:param request: GET request
:return: Response with JSON-formatted capability info if found or 404 response (HTTPNotFound)
"""
capability
=
request
.
capability_info
.
lookup_capability
(
request
.
matchdict
[
"
capability_name
"
])
if
capability
:
body
=
capability
.
__json__
()
return
Response
(
body
=
body
,
charset
=
"
UTF-8
"
,
content_type
=
"
test/json
"
)
return
Response
(
json_body
=
capability
.
__json__
())
else
:
return
HTTPNotFound
(
detail
=
f
"
Capability
{
request
.
matchdict
[
'
capability_name
'
]
}
not found.
"
)
@view_config
(
route_name
=
"
create_capability
"
,
renderer
=
"
json
"
)
def
create_capability
(
request
:
Request
)
->
Response
:
"""
Pyramid view that accepts a request to create a capability
URL: capability/create
:param request: POST request, expecting JSON parameters [
"
capability_name
"
,
"
steps
"
,
"
max_jobs
"
]
:return: Response with JSON-formatted capability info of newly created capability
or 400 response (HTTPBadRequest) if expected parameters not given
or 412 response (HTTPPreconditionFailed) if capability with given name already exists
"""
expected_params
=
[
"
capability_name
"
,
"
steps
"
,
"
max_jobs
"
]
params
=
request
.
json_body
if
not
all
([
expected
in
params
for
expected
in
expected_params
]):
# JSON params do not contain all expected params
params_not_given_msg
=
(
f
"
Expected JSON parameters
{
expected_params
}
. Received only
{
params
}
.
"
)
return
HTTPBadRequest
(
detail
=
params_not_given_msg
)
elif
request
.
capability_info
.
lookup_capability
(
params
[
"
capability_name
"
]):
# Capability with given name already exists
already_exists_msg
=
(
f
"
Capability
{
params
[
'
capability_name
'
]
}
already exists.
"
,
f
"
To instead edit existing capability, use capability/
{
params
[
'
capability_name
'
]
}
/edit
"
,
)
return
HTTPPreconditionFailed
(
detail
=
already_exists_msg
)
else
:
return
HTTPNotFound
(
detail
=
f
"
Capability with ID
{
request
.
matchdict
[
'
capability_name
'
]
}
not found.
"
new_capability
=
Capability
(
name
=
params
[
"
capability_name
"
],
steps
=
params
[
"
steps
"
],
max_jobs
=
params
[
"
max_jobs
"
],
)
request
.
capability_info
.
save_entity
(
new_capability
)
return
Response
(
json_body
=
new_capability
.
__json__
())
Loading