Skip to content
This repository was archived by the owner on Mar 30, 2026. It is now read-only.

Commit 3822176

Browse files
authored
Alpha 6 Release (#140)
* Allow for overriding enforce_json * Update default API gateway URL * Fix docstring spelling mistake * Remove overriding urllib warnings * Update query example * Update heading * Reformat with black * Bump version * Reformat with black * Add transactions attribute * Implement module-level logging * Avoid applying header credentials twice when no method credentials are present * Update HISTORY
1 parent 6a71253 commit 3822176

File tree

12 files changed

+49
-36
lines changed

12 files changed

+49
-36
lines changed

HISTORY.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
# History
22

3-
## 2.0.0-alpha5 (2020-02-20)
3+
## 2.0.0-alpha6 (2020-03-04)
4+
5+
- Chnaged `iter_job_results()` method to allow overriding `enforce_json`
6+
- Updated default API gateway URL
7+
- Minor corrections to docstrings
8+
- Removeed suppression of urllib3 warnings
9+
- Updated example scripts
10+
- Updated README
11+
- Reformatted/refactoring with black
12+
- Added `transactions` attribute to `ApiStats` with default value
13+
- Reimplemented module-level logging
14+
- Applied change to `HTTPClient` to avoid applying header credentials twice when no method credentials are present
15+
16+
## 2.0.0-alpha5 (2020-03-03)
417

518
- Fixed `Credentials` storage adapter import
619

7-
## 2.0.0-alpha4 (2020-02-20)
20+
## 2.0.0-alpha4 (2020-03-03)
821

922
- Updated MANIFEST
1023

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
![Tests](https://github.com/PaloAltoNetworks/pan-cortex-data-lake-python/workflows/Tests/badge.svg) [![PyPI version](https://badge.fury.io/py/pan-cortex-data-lake.svg)](https://badge.fury.io/py/pan-cortex-data-lake)
22

3-
# Palo Alto Networks Cortex™ Data Lake Python SDK
3+
# Palo Alto Networks Cortex™ Data Lake SDK
44

55
Python idiomatic SDK for the Cortex™ Data Lake.
66

examples/credentials_generate.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@
1212
curpath = os.path.dirname(os.path.abspath(__file__))
1313
sys.path[:0] = [os.path.join(curpath, os.pardir)]
1414

15-
from cortex import Credentials
15+
from pan_cortex_data_lake import Credentials
1616

1717

1818
def confirm_write(profile):
1919
"""Prompt user to enter Y or N (case-insensitive) to continue."""
2020
answer = ""
2121
while answer not in ["y", "n"]:
22-
answer = input(
23-
"\nWrite credentials to PROFILE '%s' [Y/N]? " % profile
24-
).lower()
22+
answer = input("\nWrite credentials to PROFILE '%s' [Y/N]? " % profile).lower()
2523
return answer == "y"
2624

2725

@@ -32,10 +30,12 @@ def main():
3230
client_secret = getpass.getpass(prompt="CLIENT_SECRET: ")
3331
refresh_token = getpass.getpass(prompt="REFRESH_TOKEN: ")
3432
profile = input("PROFILE [default]: ") or None
35-
c = Credentials(client_id=client_id,
36-
client_secret=client_secret,
37-
refresh_token=refresh_token,
38-
profile=profile)
33+
c = Credentials(
34+
client_id=client_id,
35+
client_secret=client_secret,
36+
refresh_token=refresh_token,
37+
profile=profile,
38+
)
3939
if confirm_write(profile):
4040
print("Writing credentials file...")
4141
c.write_credentials()

examples/credentials_remove.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
curpath = os.path.dirname(os.path.abspath(__file__))
1212
sys.path[:0] = [os.path.join(curpath, os.pardir)]
1313

14-
from cortex import Credentials
14+
from pan_cortex_data_lake import Credentials
1515

1616

1717
def confirm_delete(profile):

examples/query_all.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
import os
77
import sys
88
import time
9+
import logging
910

1011
# Necessary to reference cortex package in relative path
1112
curpath = os.path.dirname(os.path.abspath(__file__))
1213
sys.path[:0] = [os.path.join(curpath, os.pardir)]
1314

1415
from pan_cortex_data_lake import Credentials, QueryService
1516

16-
url = "https://cortex-prd1-api.us.cdl.paloaltonetworks.com" # prod us
17+
url = "https://api.us.cdl.paloaltonetworks.com" # prod us
1718

1819
# Create Credentials instance
1920
# export PAN_DEVELOPER_TOKEN for quick access
20-
c = Credentials(verify=False)
21+
c = Credentials()
2122

2223
# Create Query Service instance
2324
qs = QueryService(url=url, force_trace=True, credentials=c)
@@ -40,9 +41,7 @@
4041

4142
# Iterate through job results (pages)
4243
print("Iterate through job results: \n")
43-
for p in qs.iter_job_results(
44-
job_id=job_id, page_size=1, result_format="valuesDictionary"
45-
):
44+
for p in qs.iter_job_results(job_id=job_id, result_format="valuesDictionary"):
4645
print("RESULTS: {}\n".format(p.text))
4746

4847
print("STATS: {}".format(qs.stats))

pan_cortex_data_lake/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""Main package for cortex."""
44

55
__author__ = "Palo Alto Networks"
6-
__version__ = "2.0.0-a5"
6+
__version__ = "2.0.0-a6"
77

88
from .exceptions import ( # noqa: F401
99
CortexError,

pan_cortex_data_lake/adapters/adapter.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from abc import ABCMeta, abstractmethod
77

88
# Python 2.7 and 3.5+ compatibility
9-
ABC = ABCMeta('ABC', (object,), {'__slots__': ()})
9+
ABC = ABCMeta("ABC", (object,), {"__slots__": ()})
1010

1111

1212
class StorageAdapter(ABC): # enforce StorageAdapter interface
@@ -39,8 +39,7 @@ def remove_profile(self, profile=None):
3939
pass
4040

4141
@abstractmethod
42-
def write_credentials(self, credentials=None, profile=None,
43-
cache_token=None):
42+
def write_credentials(self, credentials=None, profile=None, cache_token=None):
4443
"""Write credentials.
4544
4645
Write credentials to store.

pan_cortex_data_lake/httpclient.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66

77
import logging
88

9+
logger = logging.getLogger(__name__)
10+
911
import requests
1012
from requests.adapters import HTTPAdapter
1113

12-
# support ujson in place of standrad json library
14+
# support ujson in place of standard json library
1315
try:
1416
import ujson
1517

1618
requests.models.complexjson = ujson
17-
logging.debug("Monkey patched requests with ujson")
19+
logger.debug("Monkey patched requests with ujson")
1820
except ImportError:
1921
pass
2022

@@ -56,8 +58,6 @@ def __init__(self, **kwargs):
5658
**kwargs: Supported :class:`~requests.Session` and :class:`~requests.adapters.HTTPAdapter` parameters.
5759
5860
"""
59-
if not logging.getLogger(__name__).isEnabledFor(logging.DEBUG):
60-
requests.packages.urllib3.disable_warnings()
6161
self.kwargs = kwargs.copy() # used for __repr__
6262
with requests.Session() as self.session:
6363
self._default_headers() # apply default headers
@@ -89,14 +89,13 @@ def __init__(self, **kwargs):
8989
self.session.headers.update({"x-envoy-force-trace": ""})
9090
self.port = kwargs.pop("port", 443)
9191
self.raise_for_status = kwargs.pop("raise_for_status", False)
92-
self.url = kwargs.pop(
93-
"url", "https://cortex-prd1-api.us.cdl.paloaltonetworks.com"
94-
)
92+
self.url = kwargs.pop("url", "https://api.us.cdl.paloaltonetworks.com")
9593

9694
if len(kwargs) > 0: # Handle invalid kwargs
9795
raise UnexpectedKwargsError(kwargs)
9896

9997
if self.credentials:
98+
logger.debug("Applying session-level credentials")
10099
self._apply_credentials(
101100
auto_refresh=self.auto_refresh,
102101
credentials=self.credentials,
@@ -139,12 +138,12 @@ def _apply_credentials(auto_refresh=True, credentials=None, headers=None):
139138
if auto_refresh is True:
140139
if token is None:
141140
token = credentials.refresh(access_token=None, timeout=10)
142-
logging.debug("Token refreshed due to 'None' condition")
141+
logger.debug("Token refreshed due to 'None' condition")
143142
elif credentials.jwt_is_expired():
144143
token = credentials.refresh(timeout=10)
145-
logging.debug("Token refreshed due to 'expired' condition")
144+
logger.debug("Token refreshed due to 'expired' condition")
146145
headers.update({"Authorization": "Bearer {}".format(token)})
147-
logging.debug("Credentials applied to authorization header")
146+
logger.debug("Credentials applied to authorization header")
148147

149148
def _default_headers(self):
150149
"""Update default headers.
@@ -159,7 +158,7 @@ def _default_headers(self):
159158
"User-Agent": "%s/%s" % ("cortex-sdk-python", __version__),
160159
}
161160
)
162-
logging.debug("Default headers applied: %r" % self.session.headers)
161+
logger.debug("Default headers applied: %r" % self.session.headers)
163162

164163
def _send_request(self, enforce_json, method, raise_for_status, url, **kwargs):
165164
"""Send HTTP request.
@@ -221,13 +220,14 @@ def request(self, **kwargs):
221220

222221
# Non-Requests key-word arguments
223222
auto_refresh = kwargs.pop("auto_refresh", self.auto_refresh)
224-
credentials = kwargs.pop("credentials", self.credentials)
223+
credentials = kwargs.pop("credentials", None)
225224
endpoint = kwargs.pop("endpoint", "") # default to empty endpoint
226225
enforce_json = kwargs.pop("enforce_json", self.enforce_json)
227226
raise_for_status = kwargs.pop("raise_for_status", self.raise_for_status)
228227
url = "{}:{}{}".format(url, self.port, endpoint)
229228

230229
if credentials:
230+
logger.debug("Applying method-level credentials")
231231
self._apply_credentials(
232232
auto_refresh=auto_refresh, credentials=credentials, headers=headers
233233
)

pan_cortex_data_lake/query.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ def iter_job_results(
220220
"""
221221
credentials = kwargs.pop("credentials", None)
222222
params = kwargs.pop("params", {})
223+
enforce_json = kwargs.pop("enforce_json", True)
223224
for name, value in [
224225
("maxWait", max_wait),
225226
("offset", offset),
@@ -234,7 +235,7 @@ def iter_job_results(
234235
r = self.get_job_results(
235236
job_id=job_id,
236237
params=params,
237-
enforce_json=True,
238+
enforce_json=enforce_json,
238239
credentials=credentials,
239240
**kwargs
240241
)

pan_cortex_data_lake/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class ApiStats(dict):
1212

1313
def __init__(self, *args, **kwargs):
1414
super(ApiStats, self).__init__(*args, **kwargs)
15+
self.transactions = 0
1516
for arg in args:
1617
if isinstance(arg, dict):
1718
for k, v in arg.items():

0 commit comments

Comments
 (0)