Coverage for /opt/conda/envs/apienv/lib/python3.10/site-packages/daiquiri/cli/rest.py: 0%
80 statements
« prev ^ index » next coverage.py v7.6.5, created at 2024-11-15 02:12 +0000
« prev ^ index » next coverage.py v7.6.5, created at 2024-11-15 02:12 +0000
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
4"""
5Use and display result from Daiquiri rest API
7If enabled, the following URL can be used to manually list the available
8services:
10- http://localhost:8080/swagger-ui
11"""
13import enum
14import json
15from argparse import ArgumentParser
16from daiquiri.cli.client_requests import RestClient
19class MethodEnum(enum.Enum):
20 GET = "get"
21 POST = "post"
22 PUT = "put"
23 PATCH = "patch"
24 DELETE = "delete"
27def main():
28 parser = ArgumentParser(description="Use and display result from Daiquiri REST API")
29 parser.add_argument(
30 "url",
31 type=str,
32 help="URL of the REST API. Can be a local url: /components/config, or a fully qualified url: https://host:port/api/components/config",
33 )
34 parser.add_argument(
35 "-m",
36 "--method",
37 type=MethodEnum,
38 default=MethodEnum.GET,
39 help="HTTP method to use: get, post, put, patch, delete",
40 )
41 parser.add_argument(
42 "--session", type=str, default="blc00001-1", help="Name of the beamline session"
43 )
44 parser.add_argument(
45 "-ns", "--no-session", action="store_true", help="Dont select a session"
46 )
47 parser.add_argument("-c", "--control", action="store_true", help="Request control")
48 parser.add_argument("-l", "--login", type=str, default="abcd", help="Login to use")
49 parser.add_argument("-a", "--admin", action="store_true", help="Login as admin")
50 parser.add_argument(
51 "-d", "--data", type=str, help="Data to pass to the request as a json string"
52 )
53 parser.add_argument(
54 "-b", "--base-url", type=str, default="localhost", help="Base url"
55 )
56 parser.add_argument("-s", "--https", action="store_true", help="Use https")
57 parser.add_argument(
58 "-nv",
59 "--no-verify",
60 action="store_true",
61 help="Dont verify https certificate, assume --https",
62 )
64 options = parser.parse_args()
66 print("- Log-in to the service")
67 _base_url = options.base_url
68 url = options.url
69 https = options.https
70 port = None
71 if options.url.startswith("http"):
72 if options.url.startswith("https"):
73 https = True
75 parts = options.url.split("/")
76 _base_url = parts[2]
78 parts2 = _base_url.split(":")
79 if len(parts2) == 2:
80 _base_url = parts2[0]
81 port = parts2[1]
83 url = "/" + "/".join(parts[3:])
85 base_url = f"{'https' if (https or options.no_verify) else 'http'}://{_base_url}"
87 if options.no_verify:
88 import urllib3
90 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
92 client = RestClient(base_url, port=port, verify=not options.no_verify)
93 if not client.login("efgh" if options.admin else options.login, "a"):
94 print("** Could not login **")
95 exit(0)
97 try:
98 if not options.no_session:
99 print(f"- Connect to BL session: {options.session}")
100 client.req(
101 "/metadata/sessions/select",
102 method="post",
103 data={"session": options.session},
104 )
106 if options.control:
107 print("- Requesting control")
108 resp = client.req("/session/current/control", method="post")
109 if not resp.status_code == 200:
110 print("** Could not get control **")
111 exit(0)
112 else:
113 print(" Got control")
115 if url.startswith("/api"):
116 url = url[4:]
118 print("- Send request:")
119 print(f" method: {options.method.value}")
120 print(f" url : {url}")
122 data = None
123 if options.data:
124 try:
125 data = json.loads(options.data)
126 print(f" data : {options.data}")
127 except json.JSONDecodeError as e:
128 print("** Could not deseralise json data **")
129 print(f" {str(e)}")
130 exit(0)
132 print("-" * 80)
133 client.req(url, method=options.method.value, data=data, pprint=True)
134 print("-" * 80)
135 finally:
136 print("- Log-out")
137 client.logout()
138 print("- Terminated")
141if __name__ == "__main__":
142 main()