Skip to content

Commit da91561

Browse files
authored
Make sure template REST API is in sync with GRPC (#130)
* make sure template REST API is in sync with GRPC * Borrow template id * Update Open API spec * Fix path for upload * Fix Spec * Rearrange methods * Generate new spec * Update endpoint * Fix versions * Fix integration tests
1 parent 3fc1a10 commit da91561

File tree

3 files changed

+166
-62
lines changed

3 files changed

+166
-62
lines changed

golem-cli

golem-template-service/src/api/template.rs

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,11 @@ pub struct TemplateApi {
111111

112112
#[OpenApi(prefix_path = "/v2/templates", tag = ApiTags::Template)]
113113
impl TemplateApi {
114-
#[oai(
115-
path = "/:template_id",
116-
method = "get",
117-
operation_id = "get_template_by_id"
118-
)]
119-
async fn get_template_by_id(
120-
&self,
121-
template_id: Path<TemplateId>,
122-
) -> Result<Json<Vec<Template>>> {
123-
let response = self.template_service.get(&template_id.0).await?;
114+
#[oai(path = "/", method = "post", operation_id = "create_template")]
115+
async fn create_template(&self, payload: UploadPayload) -> Result<Json<Template>> {
116+
let data = payload.template.into_vec().await?;
117+
let template_name = payload.name;
118+
let response = self.template_service.create(&template_name, data).await?;
124119
Ok(Json(response))
125120
}
126121

@@ -139,14 +134,6 @@ impl TemplateApi {
139134
Ok(Json(response))
140135
}
141136

142-
#[oai(path = "/", method = "post", operation_id = "upload_template")]
143-
async fn upload_template(&self, payload: UploadPayload) -> Result<Json<Template>> {
144-
let data = payload.template.into_vec().await?;
145-
let template_name = payload.name;
146-
let response = self.template_service.create(&template_name, data).await?;
147-
Ok(Json(response))
148-
}
149-
150137
#[oai(
151138
path = "/:template_id/download",
152139
method = "get",
@@ -164,12 +151,65 @@ impl TemplateApi {
164151
Ok(Binary(Body::from(bytes)))
165152
}
166153

154+
#[oai(
155+
path = "/:template_id",
156+
method = "get",
157+
operation_id = "get_template_metadata_all_versions"
158+
)]
159+
async fn get_template_metadata_all_versions(
160+
&self,
161+
template_id: Path<TemplateId>,
162+
) -> Result<Json<Vec<Template>>> {
163+
let response = self.template_service.get(&template_id.0).await?;
164+
Ok(Json(response))
165+
}
166+
167+
#[oai(
168+
path = "/:template_id/versions/:version",
169+
method = "get",
170+
operation_id = "get_template_metadata"
171+
)]
172+
async fn get_template_metadata(
173+
&self,
174+
#[oai(name = "template_id")] template_id: Path<TemplateId>,
175+
#[oai(name = "version")] version: Path<String>,
176+
) -> Result<Json<Template>> {
177+
let version_int = match version.0.parse::<i32>() {
178+
Ok(v) => v,
179+
Err(_) => {
180+
return Err(TemplateError::BadRequest(Json(ErrorsBody {
181+
errors: vec!["Invalid version".to_string()],
182+
})))
183+
}
184+
};
185+
186+
let versioned_template_id = VersionedTemplateId {
187+
template_id: template_id.0,
188+
version: version_int,
189+
};
190+
191+
let response = self
192+
.template_service
193+
.get_by_version(&versioned_template_id)
194+
.await?;
195+
196+
match response {
197+
Some(template) => Ok(Json(template)),
198+
None => Err(TemplateError::NotFound(Json(ErrorBody {
199+
error: "Template not found".to_string(),
200+
}))),
201+
}
202+
}
203+
167204
#[oai(
168205
path = "/:template_id/latest",
169206
method = "get",
170-
operation_id = "get_latest_version"
207+
operation_id = "get_latest_template_version"
171208
)]
172-
async fn get_latest_version(&self, template_id: Path<TemplateId>) -> Result<Json<i32>> {
209+
async fn get_latest_template_version(
210+
&self,
211+
template_id: Path<TemplateId>,
212+
) -> Result<Json<i32>> {
173213
let response = self
174214
.template_service
175215
.get_latest_version(&template_id.0)
@@ -183,8 +223,8 @@ impl TemplateApi {
183223
}
184224
}
185225

186-
#[oai(path = "/", method = "get", operation_id = "get_all_templates")]
187-
async fn get_all_templates(
226+
#[oai(path = "/", method = "get", operation_id = "get_templates")]
227+
async fn get_templates(
188228
&self,
189229
#[oai(name = "template-name")] template_name: Query<Option<TemplateName>>,
190230
) -> Result<Json<Vec<Template>>> {

openapi/golem-service.yaml

Lines changed: 103 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,78 @@ tags:
99
- name: Template
1010
- name: Worker
1111
paths:
12-
/v2/templates/{template_id}:
12+
/v2/templates:
13+
post:
14+
tags:
15+
- Template
16+
requestBody:
17+
content:
18+
multipart/form-data:
19+
schema:
20+
type: object
21+
required:
22+
- name
23+
- template
24+
properties:
25+
name:
26+
type: string
27+
template:
28+
type: string
29+
format: binary
30+
required: true
31+
responses:
32+
'200':
33+
description: ''
34+
content:
35+
application/json; charset=utf-8:
36+
schema:
37+
$ref: '#/components/schemas/Template'
38+
'400':
39+
description: ''
40+
content:
41+
application/json; charset=utf-8:
42+
schema:
43+
$ref: '#/components/schemas/ErrorsBody'
44+
'401':
45+
description: ''
46+
content:
47+
application/json; charset=utf-8:
48+
schema:
49+
$ref: '#/components/schemas/ErrorBody'
50+
'403':
51+
description: ''
52+
content:
53+
application/json; charset=utf-8:
54+
schema:
55+
$ref: '#/components/schemas/ErrorBody'
56+
'404':
57+
description: ''
58+
content:
59+
application/json; charset=utf-8:
60+
schema:
61+
$ref: '#/components/schemas/ErrorBody'
62+
'409':
63+
description: ''
64+
content:
65+
application/json; charset=utf-8:
66+
schema:
67+
$ref: '#/components/schemas/ErrorBody'
68+
'500':
69+
description: ''
70+
content:
71+
application/json; charset=utf-8:
72+
schema:
73+
$ref: '#/components/schemas/ErrorBody'
74+
operationId: create_template
1375
get:
1476
tags:
1577
- Template
1678
parameters:
17-
- name: template_id
79+
- name: template-name
1880
schema:
1981
type: string
20-
format: uuid
21-
in: path
22-
required: true
82+
in: query
83+
required: false
2384
deprecated: false
2485
explode: true
2586
responses:
@@ -67,7 +128,7 @@ paths:
67128
application/json; charset=utf-8:
68129
schema:
69130
$ref: '#/components/schemas/ErrorBody'
70-
operationId: get_template_by_id
131+
operationId: get_templates
71132
/v2/templates/{template_id}/upload:
72133
put:
73134
tags:
@@ -132,32 +193,35 @@ paths:
132193
schema:
133194
$ref: '#/components/schemas/ErrorBody'
134195
operationId: update_template
135-
/v2/templates:
136-
post:
196+
/v2/templates/{template_id}/download:
197+
get:
137198
tags:
138199
- Template
139-
requestBody:
140-
content:
141-
multipart/form-data:
142-
schema:
143-
type: object
144-
required:
145-
- name
146-
- template
147-
properties:
148-
name:
149-
type: string
150-
template:
151-
type: string
152-
format: binary
200+
parameters:
201+
- name: template_id
202+
schema:
203+
type: string
204+
format: uuid
205+
in: path
153206
required: true
207+
deprecated: false
208+
explode: true
209+
- name: version
210+
schema:
211+
type: integer
212+
format: int32
213+
in: query
214+
required: false
215+
deprecated: false
216+
explode: true
154217
responses:
155218
'200':
156219
description: ''
157220
content:
158-
application/json; charset=utf-8:
221+
application/octet-stream:
159222
schema:
160-
$ref: '#/components/schemas/Template'
223+
type: string
224+
format: binary
161225
'400':
162226
description: ''
163227
content:
@@ -194,16 +258,18 @@ paths:
194258
application/json; charset=utf-8:
195259
schema:
196260
$ref: '#/components/schemas/ErrorBody'
197-
operationId: upload_template
261+
operationId: download_template
262+
/v2/templates/{template_id}:
198263
get:
199264
tags:
200265
- Template
201266
parameters:
202-
- name: template-name
267+
- name: template_id
203268
schema:
204269
type: string
205-
in: query
206-
required: false
270+
format: uuid
271+
in: path
272+
required: true
207273
deprecated: false
208274
explode: true
209275
responses:
@@ -251,8 +317,8 @@ paths:
251317
application/json; charset=utf-8:
252318
schema:
253319
$ref: '#/components/schemas/ErrorBody'
254-
operationId: get_all_templates
255-
/v2/templates/{template_id}/download:
320+
operationId: get_template_metadata_all_versions
321+
/v2/templates/{template_id}/versions/{version}:
256322
get:
257323
tags:
258324
- Template
@@ -267,20 +333,18 @@ paths:
267333
explode: true
268334
- name: version
269335
schema:
270-
type: integer
271-
format: int32
272-
in: query
273-
required: false
336+
type: string
337+
in: path
338+
required: true
274339
deprecated: false
275340
explode: true
276341
responses:
277342
'200':
278343
description: ''
279344
content:
280-
application/octet-stream:
345+
application/json; charset=utf-8:
281346
schema:
282-
type: string
283-
format: binary
347+
$ref: '#/components/schemas/Template'
284348
'400':
285349
description: ''
286350
content:
@@ -317,7 +381,7 @@ paths:
317381
application/json; charset=utf-8:
318382
schema:
319383
$ref: '#/components/schemas/ErrorBody'
320-
operationId: download_template
384+
operationId: get_template_metadata
321385
/v2/templates/{template_id}/latest:
322386
get:
323387
tags:
@@ -375,7 +439,7 @@ paths:
375439
application/json; charset=utf-8:
376440
schema:
377441
$ref: '#/components/schemas/ErrorBody'
378-
operationId: get_latest_version
442+
operationId: get_latest_template_version
379443
/v2/templates/workers/{worker_id}:
380444
get:
381445
tags:

0 commit comments

Comments
 (0)