44# SPDX-License-Identifier: Apache-2.0
55
66import json
7+ import pathlib
78
89import click
910
1011from globus_registered_api .clients import create_flows_client
12+ from globus_registered_api .commands .api ._common import echo_registered_api
1113from globus_registered_api .context import CLIContext
1214from globus_registered_api .context import with_cli_context
13- from globus_registered_api .domain import HTTP_METHODS
14- from globus_registered_api .domain import TargetSpecifier
15- from globus_registered_api .openapi import AmbiguousContentTypeError
16- from globus_registered_api .openapi import OpenAPILoadError
17- from globus_registered_api .openapi import TargetNotFoundError
18- from globus_registered_api .openapi import process_target
1915
2016
2117@click .command ("create" )
22- @click .argument ("openapi_spec" )
23- @click .argument ("method" , type = click .Choice (HTTP_METHODS , case_sensitive = False ))
24- @click .argument ("route" )
2518@click .argument ("name" )
2619@click .option (
27- "--content-type" ,
28- default = "*" ,
29- help = "Target content-type for request body (required if multiple exist)" ,
20+ "--target" ,
21+ required = True ,
22+ type = click .Path (exists = True , dir_okay = False , readable = True , path_type = pathlib .Path ),
23+ help = "Filepath to a JSON object containing the target definition" ,
3024)
3125@click .option (
3226 "--description" ,
3327 required = True ,
3428 help = "Description for the registered API" ,
3529)
30+ @click .option (
31+ "--owner" ,
32+ "owners" ,
33+ multiple = True ,
34+ help = "Set owner URN (can specify multiple, can only be set by owners)" ,
35+ )
36+ @click .option (
37+ "--administrator" ,
38+ "administrators" ,
39+ multiple = True ,
40+ help = "Set administrator URN (can specify multiple, can only be set by owners)" ,
41+ )
42+ @click .option (
43+ "--viewer" ,
44+ "viewers" ,
45+ multiple = True ,
46+ help = (
47+ "Set viewer URN (can specify multiple, can only be set by owners "
48+ "and administrators)"
49+ ),
50+ )
3651@click .option ("--format" , type = click .Choice (["json" , "text" ]), default = "text" )
3752@with_cli_context
3853def create_command (
3954 ctx : CLIContext ,
40- openapi_spec : str ,
41- method : str ,
42- route : str ,
55+ target : pathlib .Path ,
4356 name : str ,
44- content_type : str ,
4557 description : str ,
58+ owners : tuple [str , ...],
59+ administrators : tuple [str , ...],
60+ viewers : tuple [str , ...],
4661 format : str ,
4762) -> None :
4863 """
@@ -51,41 +66,23 @@ def create_command(
5166 Extracts a target endpoint from an OpenAPI spec and registers it with
5267 the Flows service.
5368
54- OPENAPI_SPEC - A filepath or URL to an OpenAPI specification (JSON or YAML).
55-
56- METHOD - Target API's HTTP method (e.g., get, post, put, delete).
57-
58- ROUTE - Target API's route path (e.g., /items or /items/{item_id}).
59-
60- NAME - Name for the registered API.
69+ NAME - Name of the new registered API.
6170
6271 Example:
6372
6473 \b
65- gra api create ./spec.json get /items "My API" --description "My API"
74+ gra api create "My API" --target ./target.json --description "My API"
6675 """
67- try :
68- target = TargetSpecifier .create (method , route , content_type )
69- except ValueError as e :
70- raise click .ClickException (str (e ))
71-
72- try :
73- result = process_target (openapi_spec , target )
74- except (OpenAPILoadError , TargetNotFoundError , AmbiguousContentTypeError ) as e :
75- raise click .ClickException (str (e ))
76-
7776 flows_client = create_flows_client (ctx .globus_app )
7877
78+ target_content = json .loads (target .read_text ())
7979 res = flows_client .create_registered_api (
8080 name = name ,
8181 description = description ,
82- target = result .to_dict (),
82+ target = target_content ,
83+ owners = list (owners ),
84+ administrators = list (administrators ),
85+ viewers = list (viewers ),
8386 )
8487
85- if format == "json" :
86- click .echo (json .dumps (res .data , indent = 2 ))
87- else :
88- click .echo (f"ID: { res ['id' ]} " )
89- click .echo (f"Name: { res ['name' ]} " )
90- click .echo (f"Description: { res ['description' ]} " )
91- click .echo (f"Created: { res ['created_timestamp' ]} " )
88+ echo_registered_api (res , format )
0 commit comments