gibson-cli 0.1.3__py3-none-any.whl → 0.1.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
gibson/api/BaseApi.py CHANGED
@@ -1,33 +1,100 @@
1
+ import os
1
2
  import requests
2
3
 
4
+ from gibson.core.Configuration import Configuration
5
+
3
6
 
4
7
  class BaseApi:
5
- def get_headers(self):
6
- raise NotImplementedError
8
+ API_ENV = os.environ.get("GIBSONAI_API_ENV", "staging")
9
+ VERSION = "v1"
10
+
11
+ def __init__(self, configuration: Configuration):
12
+ self.configuration = configuration
13
+
14
+ def api_domain(self):
15
+ domains = {
16
+ "local": "http://localhost:8000",
17
+ "staging": "https://staging-api.gibsonai.com",
18
+ "production": "https://api.gibsonai.com",
19
+ }
20
+ return domains[self.API_ENV]
21
+
22
+ def app_domain(self):
23
+ domains = {
24
+ "local": "http://localhost:5173",
25
+ "staging": "https://staging-app.gibsonai.com",
26
+ "production": "https://app.gibsonai.com",
27
+ }
28
+ return domains[self.API_ENV]
29
+
30
+ def base_url(self):
31
+ return f"{self.api_domain()}/{self.VERSION}"
7
32
 
8
- def _get(self, end_point):
9
- r = requests.get(self.get_url(end_point), headers=self.get_headers())
33
+ def client_id(self):
34
+ return {
35
+ "local": "9b0cbebd-3eb4-47be-89ac-4aa589316ff4",
36
+ "staging": "02459e16-f356-4c01-b689-59847ed04b0a",
37
+ "production": "da287371-240b-4b53-bfde-4b1581cca62a",
38
+ }[self.API_ENV]
39
+
40
+ def get(self, endpoint):
41
+ r = requests.get(self.url(endpoint), headers=self.headers())
42
+
43
+ if r.status_code == 401 and self.refresh_auth_tokens():
44
+ r = requests.get(self.url(endpoint), headers=self.headers())
10
45
 
11
46
  self.__raise_for_status(r)
12
47
 
13
48
  return r.json()
14
49
 
15
- def _post(self, end_point, json: dict):
16
- r = requests.post(
17
- self.get_url(end_point), headers=self.get_headers(), json=json
18
- )
50
+ def headers(self):
51
+ raise NotImplementedError
52
+
53
+ def post(self, endpoint, json: dict):
54
+ r = requests.post(self.url(endpoint), headers=self.headers(), json=json)
55
+
56
+ if r.status_code == 401 and self.refresh_auth_tokens():
57
+ r = requests.post(self.url(endpoint), headers=self.headers(), json=json)
19
58
 
20
59
  self.__raise_for_status(r)
21
60
 
22
61
  return r
23
62
 
24
- def _put(self, end_point, json: dict):
25
- r = requests.put(self.get_url(end_point), headers=self.get_headers(), json=json)
63
+ def put(self, endpoint, json: dict):
64
+ r = requests.put(self.url(endpoint), headers=self.headers(), json=json)
65
+
66
+ if r.status_code == 401 and self.refresh_auth_tokens():
67
+ r = requests.put(self.url(endpoint), headers=self.headers(), json=json)
26
68
 
27
69
  self.__raise_for_status(r)
28
70
 
29
71
  return r
30
72
 
73
+ def refresh_auth_tokens(self):
74
+ refresh_token = self.configuration.get_refresh_token()
75
+ if not refresh_token:
76
+ return False
77
+
78
+ r = requests.post(
79
+ f"{self.base_url()}/auth/token/refresh",
80
+ headers=self.headers(),
81
+ json={"refresh_token": refresh_token},
82
+ )
83
+
84
+ if r.status_code != 200:
85
+ return False
86
+
87
+ parsed = r.json()
88
+ self.configuration.set_auth_tokens(
89
+ parsed["access_token"], parsed["refresh_token"]
90
+ )
91
+ return True
92
+
93
+ def url(self, endpoint):
94
+ if self.PREFIX:
95
+ return f"{self.base_url()}/{self.PREFIX}/{endpoint}"
96
+ return f"{self.base_url()}/{endpoint}"
97
+
31
98
  def __raise_for_status(self, r):
32
99
  try:
33
100
  r.raise_for_status()
gibson/api/Cli.py CHANGED
@@ -1,7 +1,3 @@
1
- import os
2
- import requests
3
-
4
- from gibson.services.auth.Server import Server as AuthServer
5
1
  from gibson.core.Configuration import Configuration
6
2
  from gibson.lang.Python import Python
7
3
  from gibson.core.Memory import Memory
@@ -10,22 +6,16 @@ from .BaseApi import BaseApi
10
6
 
11
7
 
12
8
  class Cli(BaseApi):
13
- API_ENV = os.environ.get("GIBSONAI_API_ENV", "staging")
14
9
  PREFIX = "cli"
15
- VERSION = "v1"
16
-
17
- def __init__(self, configuration: Configuration):
18
- super().__init__()
19
- self.configuration = configuration
20
10
 
21
11
  def code_api(self):
22
- return self._post(
12
+ return self.post(
23
13
  "code/api",
24
14
  self.__structure_context_payload(self.configuration, with_stored=True),
25
15
  ).json()
26
16
 
27
17
  def code_base(self):
28
- return self._post(
18
+ return self.post(
29
19
  "code/base",
30
20
  self.__structure_context_payload(self.configuration, with_stored=True),
31
21
  ).json()
@@ -35,25 +25,25 @@ class Cli(BaseApi):
35
25
  payload["model"] = {"name": model_name}
36
26
  payload["q"] = instructions
37
27
 
38
- return self._post("code/model/attributes", payload).json()
28
+ return self.post("code/model/attributes", payload).json()
39
29
 
40
30
  def code_models(self, entities: list):
41
31
  payload = self.__structure_context_payload(self.configuration, with_stored=True)
42
32
  payload["entities"] = entities
43
33
 
44
- return self._post("code/models", payload).json()
34
+ return self.post("code/models", payload).json()
45
35
 
46
36
  def code_schemas(self, entities: list):
47
37
  payload = self.__structure_context_payload(self.configuration, with_stored=True)
48
38
  payload["entities"] = entities
49
39
 
50
- return self._post("code/schemas", payload).json()
40
+ return self.post("code/schemas", payload).json()
51
41
 
52
42
  def code_testing(self, entities: list):
53
43
  payload = self.__structure_context_payload(self.configuration, with_stored=True)
54
44
  payload["entities"] = entities
55
45
 
56
- return self._post("code/testing", payload).json()
46
+ return self.post("code/testing", payload).json()
57
47
 
58
48
  def code_writer_entity_modifier(self, context, name, definition, instructions):
59
49
  payload = self.__structure_context_payload(self.configuration)
@@ -61,38 +51,17 @@ class Cli(BaseApi):
61
51
  payload["entity"] = {"definition": definition, "name": name}
62
52
  payload["q"] = instructions
63
53
 
64
- return self._post("code/writer/entity/modifier", payload).json()
54
+ return self.post("code/writer/entity/modifier", payload).json()
65
55
 
66
56
  def code_writer_schema_context(self):
67
- return self._post(
57
+ return self.post(
68
58
  "code/writer/schema/context",
69
59
  self.__structure_context_payload(self.configuration, with_stored=True),
70
60
  ).json()
71
61
 
72
- def get_client_id(self):
73
- return {
74
- "local": "9b0cbebd-3eb4-47be-89ac-4aa589316ff4",
75
- "staging": "02459e16-f356-4c01-b689-59847ed04b0a",
76
- "production": "da287371-240b-4b53-bfde-4b1581cca62a",
77
- }[self.API_ENV]
78
-
79
- def get_api_domain(self):
80
- return {
81
- "local": "http://localhost:8000",
82
- "staging": "https://staging-api.gibsonai.com",
83
- "production": "https://api.gibsonai.com",
84
- }[self.API_ENV]
85
-
86
- def get_app_domain(self):
87
- return {
88
- "local": "http://localhost:5173",
89
- "staging": "https://staging-app.gibsonai.com",
90
- "production": "https://app.gibsonai.com",
91
- }[self.API_ENV]
92
-
93
- def get_headers(self):
62
+ def headers(self):
94
63
  headers = {
95
- "X-Gibson-Client-ID": self.get_client_id(),
64
+ "X-Gibson-Client-ID": self.client_id(),
96
65
  "X-Gibson-API-Key": self.configuration.project.api.key,
97
66
  }
98
67
 
@@ -102,15 +71,12 @@ class Cli(BaseApi):
102
71
 
103
72
  return headers
104
73
 
105
- def get_url(self, end_point):
106
- return f"{self.get_api_domain()}/{self.VERSION}/{self.PREFIX}/{end_point}"
107
-
108
74
  def import_(self):
109
- return self._get("import")
75
+ return self.get("import")
110
76
 
111
77
  def llm_query(self, instructions, has_file, has_python, has_sql):
112
78
  project_config = self.configuration.project
113
- r = self._post(
79
+ r = self.post(
114
80
  "llm/query",
115
81
  {
116
82
  "content": {
@@ -133,14 +99,10 @@ class Cli(BaseApi):
133
99
 
134
100
  return r.json()
135
101
 
136
- def login(self):
137
- access_token, refresh_token = AuthServer(self.get_app_domain()).get_tokens()
138
- return access_token, refresh_token
139
-
140
102
  def modeler_entity_modify(
141
103
  self, modeler_version, project_description, entity: dict, modifications: str
142
104
  ):
143
- r = self._put(
105
+ r = self.put(
144
106
  "modeler/entity/modify",
145
107
  {
146
108
  "entity": entity,
@@ -153,7 +115,7 @@ class Cli(BaseApi):
153
115
  return r.json()
154
116
 
155
117
  def modeler_entity_remove(self, modeler_version, entities: list, entity_name):
156
- r = self._put(
118
+ r = self.put(
157
119
  "modeler/entity/remove",
158
120
  {
159
121
  "entity": {"name": entity_name},
@@ -165,7 +127,7 @@ class Cli(BaseApi):
165
127
  return r.json()
166
128
 
167
129
  def modeler_entity_rename(self, modeler_version, entities: list, current, new):
168
- r = self._put(
130
+ r = self.put(
169
131
  "modeler/entity/rename",
170
132
  {
171
133
  "entity": {"current": current, "new": new},
@@ -177,7 +139,7 @@ class Cli(BaseApi):
177
139
  return r.json()
178
140
 
179
141
  def modeler_module(self, modeler_version, project_description, module):
180
- r = self._post(
142
+ r = self.post(
181
143
  "modeler/module",
182
144
  {
183
145
  "modeler": {"version": modeler_version},
@@ -189,7 +151,7 @@ class Cli(BaseApi):
189
151
  return r.json()
190
152
 
191
153
  def modeler_openapi(self, modeler_version, contents):
192
- r = self._post(
154
+ r = self.post(
193
155
  "modeler/openapi",
194
156
  {"contents": contents, "modeler": {"version": modeler_version}},
195
157
  )
@@ -197,7 +159,7 @@ class Cli(BaseApi):
197
159
  return r.json()
198
160
 
199
161
  def modeler_reconcile(self, modeler_version, entities: list):
200
- r = self._post(
162
+ r = self.post(
201
163
  "modeler/reconcile",
202
164
  {"modeler": {"version": modeler_version}, "schema_": entities},
203
165
  )
@@ -1,11 +1,12 @@
1
- from gibson.api.Cli import Cli
1
+ from gibson.api.BaseApi import BaseApi
2
2
  from gibson.command.BaseCommand import BaseCommand
3
+ from gibson.services.auth.Server import Server as AuthServer
3
4
 
4
5
 
5
6
  class Login(BaseCommand):
6
7
  def execute(self):
7
- cli = Cli(self.configuration)
8
- access_token, refresh_token = cli.login()
8
+ api = BaseApi(self.configuration)
9
+ access_token, refresh_token = AuthServer(api.app_domain()).get_tokens()
9
10
 
10
11
  if access_token is None or refresh_token is None:
11
12
  self.conversation.newline()
@@ -10,13 +10,11 @@ class Completions:
10
10
  completions_location = f"$HOME/{self.gibson_config}/bash_completion"
11
11
  installation = f"""\n[ -s "{completions_location}" ] && \\. "{completions_location}" # Load gibson auto completion\n"""
12
12
 
13
- with open(f"{self.user_home}/.bashrc", "r+") as f:
14
- if completions_location not in f.read():
15
- f.write(installation)
16
-
17
- with open(f"{self.user_home}/.zshrc", "r+") as f:
18
- if completions_location not in f.read():
19
- f.write(installation)
13
+ for file in [f"{self.user_home}/.bashrc", f"{self.user_home}/.zshrc"]:
14
+ with open(file, "a+") as f:
15
+ f.seek(0)
16
+ if completions_location not in f.read():
17
+ f.write(installation)
20
18
 
21
19
  return self
22
20
 
@@ -28,8 +26,12 @@ class Completions:
28
26
  except FileNotFoundError:
29
27
  return self
30
28
 
31
- completions = f"{self.user_home}/{self.gibson_config}/bash_completion"
32
- with open(completions, "w") as f:
29
+ try:
30
+ os.mkdir(f"{self.user_home}/{self.gibson_config}")
31
+ except FileExistsError:
32
+ pass
33
+
34
+ with open(f"{self.user_home}/{self.gibson_config}/bash_completion", "w") as f:
33
35
  f.write(contents)
34
36
 
35
37
  return self
@@ -269,6 +269,16 @@ class Configuration:
269
269
 
270
270
  def get_my_settings(self):
271
271
  return self.settings[self.project.name]
272
+
273
+ def get_refresh_token(self):
274
+ try:
275
+ with open(f"{self.paths.auth}/{self.API_ENV}", "r") as f:
276
+ contents = f.read()
277
+ except FileNotFoundError:
278
+ return None
279
+
280
+ token = json.loads(contents)
281
+ return token["refresh_token"]
272
282
 
273
283
  def initialize(self):
274
284
  self.conversation.message_welcome()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: gibson-cli
3
- Version: 0.1.3
3
+ Version: 0.1.5
4
4
  Summary: Gibson Command Line Interface
5
5
  Author-email: GibsonAI <noc@gibsonai.com>
6
6
  Project-URL: Homepage, https://gibsonai.com/
@@ -1,5 +1,5 @@
1
- gibson/api/BaseApi.py,sha256=W5C_Rq-sf_MOwArozYG0ILFL8nyXhSwX84dpLT69o1M,1055
2
- gibson/api/Cli.py,sha256=4M1KizWG97hxkU-KPGBSGXKA-q8ReTFxIsWGZ2yV-60,8453
1
+ gibson/api/BaseApi.py,sha256=SgEsVvEJ0t4U2jwJ-dp6YLVdBjh15pAOuT_wPQYn_v0,3233
2
+ gibson/api/Cli.py,sha256=WT2EvEwUJgfM2klxKBSMaDFWrO1maImo9LSeJM4-Eho,7146
3
3
  gibson/bin/gibson.py,sha256=N1mAWaww9pw8s5u0et87FC5cFHVU6JzN4Kls3lDn-xw,354
4
4
  gibson/command/Api.py,sha256=sSvAqEJXdgQjYcu0uiM6ndHE3GnfkfVL6eqP2Otkbww,1002
5
5
  gibson/command/Base.py,sha256=eqp9e3XlCpGt4j_aR2htsQnICgkZT6GWeQblCFM792g,819
@@ -32,7 +32,7 @@ gibson/command/Tree.py,sha256=JWvUimeHWY5-6vEh6axTkhAdI-dVTxbKSjx_4isAAx8,3041
32
32
  gibson/command/Version.py,sha256=Dbi1Mg56oRahWF-hxfCGFwRUli7vYguoCKKkJv-c7MI,227
33
33
  gibson/command/WarGames.py,sha256=V0KIpz-Z546qtQaOPdIVHQ6wp2n3r3M3tgKx-GRQzzU,1300
34
34
  gibson/command/auth/Auth.py,sha256=7R4Xt9NrkkSg8yYZeWi0VdhqCCK8ved2JUBdCbrMMZ8,717
35
- gibson/command/auth/Login.py,sha256=L0lMycs_nPogFHTQ603bsJtEgbXk8EDcnIocKqcU9FI,643
35
+ gibson/command/auth/Login.py,sha256=QpIQirzCAl_bDfV7zZLZKJIj2NGqTzmr6PDN000DIfI,746
36
36
  gibson/command/auth/Logout.py,sha256=9fs_iNdJdcdsE7q5Yi0XzqH_A0w8uqP34YZ6PWcZ3Pg,212
37
37
  gibson/command/tests/test_command_BaseCommand.py,sha256=hSbBfLFI3RTp_DdEHtm5oOLWoN6drI6ucFJypi7xxV8,364
38
38
  gibson/command/tests/test_command_Conf.py,sha256=NhN8tIZ_lwP0fV4jtplaxJbO3GTozsQVmiV5j1BNbxQ,583
@@ -55,8 +55,8 @@ gibson/conf/dev/Schema.py,sha256=kOSlX1jEyVb82xd8TO8jEAimLcaefIFJr6d2JYvyTqg,74
55
55
  gibson/conf/tests/test_conf_Dependencies.py,sha256=LITeeYiqXM5rKkyWFBqcnMvUR5pzDRuHVAngH372jWc,116
56
56
  gibson/conf/tests/test_conf_Platform.py,sha256=Zc53IsZmV-hT9VRrZEPNrsuehSdWnJXWKGMmOhEqWHo,138
57
57
  gibson/core/CommandRouter.py,sha256=jYKWpsCQGLjuCPkg7C8PPBnS07AddrUv3APTqB0s2C0,9558
58
- gibson/core/Completions.py,sha256=r9GL-da-mq2HftWs2rmxnmasMEkJ3MMhzOn02YZE9sE,1116
59
- gibson/core/Configuration.py,sha256=ruMpEkd_cKTmfPotPWyESXWKUYRrChTeSpNVegzbLS4,14009
58
+ gibson/core/Completions.py,sha256=N-mfeImSzw-d3Lrpu1KVnt0geMuKkbTaHpYTMYcPcAQ,1152
59
+ gibson/core/Configuration.py,sha256=6EX8vh7qf15OB1hDfr2lYyKz6r-nB3KG0ltiax80_dU,14300
60
60
  gibson/core/Conversation.py,sha256=rKSfJao5VGj1WyWHjfbKnYqWS9j0fPmDCF5uX9e04zs,8196
61
61
  gibson/core/Env.py,sha256=7HFKGah25KjLelyOjYS8ylR6yDScT6utyZe7HdB3aRE,431
62
62
  gibson/core/Memory.py,sha256=Anauq7vx883Bg5djoZCVyvAJBQeMtqQLzZtNecJMpZc,3921
@@ -101,8 +101,8 @@ gibson/structure/tests/test_Entity.py,sha256=Gl9f1NcEKdpWCx4W3takFFzp18mLhCYWKrd
101
101
  gibson/tests/test_Env.py,sha256=DPWmP0-aEelducq9bAwv7rKoY2NjWXUeCrzfJDQkn2M,369
102
102
  gibson/tests/test_Memory.py,sha256=YP7owToABAk_-s7fD5UG0HTc4lamDjdA39JUlLnk3Fg,2574
103
103
  gibson/tests/test_utils.py,sha256=r_y-EG05YTCNtL8MWiAK1KmPsmeoMgypKsQC_lVgOtM,559
104
- gibson_cli-0.1.3.dist-info/METADATA,sha256=WU7RIR2J3fg92RcjJdC8XMJZNmm2GuIB3WrwSDFBQS8,11437
105
- gibson_cli-0.1.3.dist-info/WHEEL,sha256=R0nc6qTxuoLk7ShA2_Y-UWkN8ZdfDBG2B6Eqpz2WXbs,91
106
- gibson_cli-0.1.3.dist-info/entry_points.txt,sha256=j5VUvq3AzL21xPvVC24zMoXFt-I5lUWulr66nL3OAPM,50
107
- gibson_cli-0.1.3.dist-info/top_level.txt,sha256=GIvzfJmscYHhX9eE-3SJmCIcjF3If-EtcoQdnlmsBuQ,7
108
- gibson_cli-0.1.3.dist-info/RECORD,,
104
+ gibson_cli-0.1.5.dist-info/METADATA,sha256=T4hgBIfOE1f_0YHtcf4n9vMbJHHKfK12HsgkXj-OLqk,11437
105
+ gibson_cli-0.1.5.dist-info/WHEEL,sha256=R0nc6qTxuoLk7ShA2_Y-UWkN8ZdfDBG2B6Eqpz2WXbs,91
106
+ gibson_cli-0.1.5.dist-info/entry_points.txt,sha256=j5VUvq3AzL21xPvVC24zMoXFt-I5lUWulr66nL3OAPM,50
107
+ gibson_cli-0.1.5.dist-info/top_level.txt,sha256=GIvzfJmscYHhX9eE-3SJmCIcjF3If-EtcoQdnlmsBuQ,7
108
+ gibson_cli-0.1.5.dist-info/RECORD,,