Skip to content

Commit f672395

Browse files
authored
Add --use-port option to %%gremlin and %%oc (#712)
* Add enable port option to %%gremlin and %%oc * update changelog
1 parent db7d7d3 commit f672395

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Starting with v1.31.6, this file will contain a record of major features and updates made in each release of graph-notebook.
44

55
## Upcoming
6+
- Added Neptune `--use-port` option to `%%gremlin` and `%%oc` ([Link to PR](https://github.com/aws/graph-notebook/pull/712))
67

78
## Release 4.6.1 (October 1, 2024)
89

src/graph_notebook/magics/graph_magic.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,9 @@ def gremlin(self, line, cell, local_ns: dict = None):
11391139
help='Enable debug mode.')
11401140
parser.add_argument('--profile-misc-args', type=str, default='{}',
11411141
help='Additional profile options, passed in as a map.')
1142+
parser.add_argument('--use-port', action='store_true', default=False,
1143+
help='Includes the port in the URI for applicable Neptune HTTP requests where it is '
1144+
'excluded by default.')
11421145
parser.add_argument('-sp', '--stop-physics', action='store_true', default=False,
11431146
help="Disable visualization physics after the initial simulation stabilizes.")
11441147
parser.add_argument('-sd', '--simulation-duration', type=int, default=1500,
@@ -1208,7 +1211,8 @@ def gremlin(self, line, cell, local_ns: dict = None):
12081211
if self.client.is_analytics_domain() and query_params:
12091212
explain_args['parameters'] = query_params
12101213
res = self.client.gremlin_explain(cell,
1211-
args=explain_args)
1214+
args=explain_args,
1215+
use_port=args.use_port)
12121216
res.raise_for_status()
12131217
except Exception as e:
12141218
if self.client.is_analytics_domain():
@@ -1251,7 +1255,9 @@ def gremlin(self, line, cell, local_ns: dict = None):
12511255
print('--profile-misc-args received invalid input, please check that you are passing in a valid '
12521256
'string representation of a map, ex. "{\'profile.x\':\'true\'}"')
12531257
try:
1254-
res = self.client.gremlin_profile(query=cell, args=profile_args)
1258+
res = self.client.gremlin_profile(query=cell,
1259+
args=profile_args,
1260+
use_port=args.use_port)
12551261
res.raise_for_status()
12561262
except Exception as e:
12571263
if self.client.is_analytics_domain():
@@ -1302,7 +1308,8 @@ def gremlin(self, line, cell, local_ns: dict = None):
13021308
passed_params = query_params if self.client.is_analytics_domain() else None
13031309
query_res_http = self.client.gremlin_http_query(cell,
13041310
headers=headers,
1305-
query_params=passed_params)
1311+
query_params=passed_params,
1312+
use_port=args.use_port)
13061313
query_res_http.raise_for_status()
13071314
try:
13081315
query_res_http_json = query_res_http.json()
@@ -3550,6 +3557,9 @@ def handle_opencypher_query(self, line, cell, local_ns):
35503557
parser.add_argument('-qp', '--query-parameters', type=str, default='',
35513558
help='Parameter definitions to apply to the query. This option can accept a local variable '
35523559
'name, or a string representation of the map.')
3560+
parser.add_argument('--use-port', action='store_true', default=False,
3561+
help='Includes the port in the URI for applicable Neptune HTTP requests where it is '
3562+
'excluded by default.')
35533563
parser.add_argument('-g', '--group-by', type=str, default='~labels',
35543564
help='Property used to group nodes (e.g. code, ~id) default is ~labels')
35553565
parser.add_argument('-gd', '--group-by-depth', action='store_true', default=False,
@@ -3638,7 +3648,8 @@ def handle_opencypher_query(self, line, cell, local_ns):
36383648
explain=args.explain_type,
36393649
query_params=query_params,
36403650
plan_cache=args.plan_cache,
3641-
query_timeout=args.query_timeout)
3651+
query_timeout=args.query_timeout,
3652+
use_port=args.use_port)
36423653
query_time = time.time() * 1000 - query_start
36433654
res_replace_chars = res.content.replace(b'$', b'\$')
36443655
explain = res_replace_chars.decode("utf-8")
@@ -3660,7 +3671,8 @@ def handle_opencypher_query(self, line, cell, local_ns):
36603671
oc_http = self.client.opencypher_http(cell,
36613672
query_params=query_params,
36623673
plan_cache=args.plan_cache,
3663-
query_timeout=args.query_timeout)
3674+
query_timeout=args.query_timeout,
3675+
use_port=args.use_port)
36643676
query_time = time.time() * 1000 - query_start
36653677
if oc_http.status_code == 400 and not self.client.is_analytics_domain() and args.plan_cache != "auto":
36663678
try:

src/graph_notebook/neptune/client.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -466,14 +466,15 @@ def gremlin_query(self, query, transport_args=None, bindings=None):
466466
c.close()
467467
raise e
468468

469-
def gremlin_http_query(self, query, headers=None, query_params: dict = None) -> requests.Response:
469+
def gremlin_http_query(self, query, headers=None, query_params: dict = None,
470+
use_port: bool = False) -> requests.Response:
470471
if headers is None:
471472
headers = {}
472473

473474
data = {}
474475
use_proxy = True if self.proxy_host != '' else False
475476
if self.is_analytics_domain():
476-
uri = f'{self.get_uri(use_websocket=False, use_proxy=use_proxy, include_port=False)}/queries'
477+
uri = f'{self.get_uri(use_websocket=False, use_proxy=use_proxy, include_port=use_port)}/queries'
477478
data['query'] = query
478479
data['language'] = 'gremlin'
479480
headers['content-type'] = 'application/json'
@@ -498,17 +499,20 @@ def gremlin_cancel(self, query_id: str):
498499
raise ValueError('query_id must be a non-empty string')
499500
return self._query_status('gremlin', query_id=query_id, cancelQuery=True)
500501

501-
def gremlin_explain(self, query: str, args={}) -> requests.Response:
502-
return self._gremlin_query_plan(query=query, plan_type='explain', args=args)
502+
def gremlin_explain(self, query: str, use_port: bool = False, args={}) -> requests.Response:
503+
return self._gremlin_query_plan(query=query, plan_type='explain', args=args, use_port=use_port)
503504

504-
def gremlin_profile(self, query: str, args={}) -> requests.Response:
505-
return self._gremlin_query_plan(query=query, plan_type='profile', args=args)
505+
def gremlin_profile(self, query: str, use_port: bool = False, args={}) -> requests.Response:
506+
return self._gremlin_query_plan(query=query, plan_type='profile', args=args, use_port=use_port)
506507

507-
def _gremlin_query_plan(self, query: str, plan_type: str, args: dict, ) -> requests.Response:
508+
def _gremlin_query_plan(self, query: str, plan_type: str, args: dict,
509+
use_port: bool = False) -> requests.Response:
508510
data = {}
509511
headers = {}
510512
url = f'{self._http_protocol}://{self.host}'
511513
if self.is_analytics_domain():
514+
if use_port:
515+
url += f':{self.port}'
512516
url += '/queries'
513517
data['query'] = query
514518
data['language'] = 'gremlin'
@@ -537,7 +541,8 @@ def _gremlin_query_plan(self, query: str, plan_type: str, args: dict, ) -> reque
537541
def opencypher_http(self, query: str, headers: dict = None, explain: str = None,
538542
query_params: dict = None,
539543
plan_cache: str = None,
540-
query_timeout: int = None) -> requests.Response:
544+
query_timeout: int = None,
545+
use_port: bool = False) -> requests.Response:
541546
if headers is None:
542547
headers = {}
543548

@@ -546,6 +551,8 @@ def opencypher_http(self, query: str, headers: dict = None, explain: str = None,
546551
if self.is_neptune_domain():
547552
data = {}
548553
if self.is_analytics_domain():
554+
if use_port:
555+
url += f':{self.port}'
549556
url += f'/queries'
550557
data['language'] = 'opencypher'
551558
else:

0 commit comments

Comments
 (0)