-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
31387f1
commit 849c570
Showing
9 changed files
with
145 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
"""Loading this module automatically causes the command line to be parsed and | ||
the initialization files to be loaded. | ||
""" | ||
import iriusrisk | ||
|
||
iriusrisk.do_initialization() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,91 @@ | ||
"""This module provides helper methods for accessing IriusRisk API version v1. | ||
It also adds the --key command line parser, which expects the IriusRisk v1 API | ||
key as it's argument. | ||
Specific methods for calling into the API are provided in the iriusrisk.v1.facade | ||
module. | ||
"""This module contains helper methods for calling the IriusRisk API. | ||
""" | ||
import iriusrisk | ||
|
||
iriusrisk.get_commandline_parser().add_argument("-k", "--key", help="API Token to use when accessing the API") | ||
if (iriusrisk.parse_args_on_load): | ||
iriusrisk.parse_arguments() | ||
import http.client | ||
import json | ||
import logging | ||
from urllib.parse import quote | ||
from iriusrisk import get_config | ||
|
||
__all__=["do_get","call_endpoint"] | ||
|
||
# Provides helper methods that make accessing the IriusRisk v1 API easier. | ||
_log = logging.getLogger('iriusrisk.v1') | ||
|
||
def _build_path(path, encode_path): | ||
if type(path) is str: | ||
if encode_path: | ||
path = path.split("/") | ||
else: | ||
return path | ||
|
||
if encode_path: | ||
elements = [] | ||
|
||
for element in path: | ||
elements.append(quote(element)) | ||
|
||
path = elements | ||
|
||
return "/".join(path) | ||
|
||
def call_endpoint(path, verb, headers={}, params={}, convert_response=True, encode_path=False): | ||
"""Call a named endpoint of the IriusRisk API. | ||
Arguments: | ||
path : the endpoint path. May be a collection of strings, each element | ||
another call depth. So to call the URL | ||
"/api/v1/products/:productid/threats," you would pass three | ||
elements in the collection, ["products", f"{productid}", "threats"] | ||
verb : the verb when calling the endpoint, for instance GET, PUT, POST etc | ||
headers : (optional) any headers to include on the call | ||
params : (optional) any parameters to include on the call | ||
convert_response: (default: True): whether the response should be converted to JSON | ||
encode_path : (default: False): whether URL encoding should be applied to the | ||
various elements of the path. | ||
The method returns a tuple containing the HTTP response and data returned as the body | ||
of the response. The type of the data depends on two things. First, if convert_response | ||
is False, plain text is returned. Otherwise, it depends on the return type of the | ||
API call. If (for instance) the return type is "application/json," then a json object | ||
is returned. | ||
""" | ||
path = _build_path(path, encode_path) | ||
_log.info(f"Calling endpoint {path} with verb {verb}") | ||
|
||
config = get_config() | ||
|
||
if not "api-token" in headers: | ||
if config.key: | ||
headers["api-token"] = config.key | ||
else: | ||
_log.info("No API key was provided to this application; API call will likely fail") | ||
|
||
if not "accept" in headers: | ||
headers["accept"] = "application/json" | ||
|
||
path = f"/api/v1/{path}" | ||
_log.debug(f"Making a {verb} call to {path} at {config.url}") | ||
conn = http.client.HTTPSConnection(config.url) | ||
|
||
if config.dryrun: | ||
resp = None | ||
else : | ||
conn.request(verb, path, params, headers) | ||
resp = conn.getresponse() | ||
|
||
result = None | ||
if convert_response and not config.dryrun: | ||
data = resp.read().decode("utf-8") | ||
if resp.status == 200 and headers["accept"] == "application/json": | ||
result = json.loads(data) | ||
else: | ||
result = data | ||
|
||
return (resp, result) | ||
|
||
"""Call the specified endpoint using "GET." | ||
""" | ||
def do_get(path, headers={}, params={}, convert_response=True, encode_path=False): | ||
"""Call the indicated endpoint via GET. See call_endpoint for more details.""" | ||
return call_endpoint(path, "GET", headers, params, convert_response, encode_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,33 @@ | ||
"""This module contains helper methods for calling the IriusRisk API. | ||
"""This module is deprecated. Use the methods in the iriusrisk.v1 module instead. | ||
""" | ||
|
||
import http.client | ||
import json | ||
import iriusrisk | ||
import logging | ||
from urllib.parse import quote | ||
from iriusrisk import config | ||
|
||
# Provides helper methods that make accessing the IriusRisk v1 API easier. | ||
_log = logging.getLogger('iriusrisk.v1') | ||
_log = logging.getLogger("iriusrisk.v1.facade") | ||
|
||
def _build_path(path, encode_path): | ||
if type(path) is str: | ||
if encode_path: | ||
path = path.split("/") | ||
else: | ||
return path | ||
_deprected_do_get_reported = False | ||
_deprected_call_endpoint_reported = False | ||
|
||
if encode_path: | ||
elements = [] | ||
_log.warning("""Program is using deprecated module iriusrisk.v1.facade. It needs to use the same-named | ||
methods in iriusrisk.v1 instead. Note that loading this module automatically initializes the application | ||
by loading the init files and parsing the command line arguments. | ||
""") | ||
|
||
for element in path: | ||
elements.append(quote(element)) | ||
iriusrisk.do_initialization() | ||
|
||
path = elements | ||
def do_get(path, headers={}, params={}, convert_response=True, encode_path=False): | ||
global _deprected_do_get_reported | ||
if not _deprected_do_get_reported: | ||
_log.warning("Calling deprecated method iriusrisk.v1.facade.do_get; call iriusrisk.v1.do_get instead") | ||
_deprected_do_get_reported = True | ||
|
||
return "/".join(path) | ||
return iriusrisk.v1.do_get(path, headers, params, convert_response, encode_path) | ||
|
||
def call_endpoint(path, verb, headers={}, params={}, convert_response=True, encode_path=False): | ||
"""Call a named endpoint of the IriusRisk API. | ||
Arguments: | ||
path : the endpoint path. May be a collection of strings, each element | ||
another call depth. So to call the URL | ||
"/api/v1/products/:productid/threats," you would pass three | ||
elements in the collection, ["products", f"{productid}", "threats"] | ||
verb : the verb when calling the endpoint, for instance GET, PUT, POST etc | ||
headers : (optional) any headers to include on the call | ||
params : (optional) any parameters to include on the call | ||
convert_response: (default: True): whether the response should be converted to JSON | ||
encode_path : (default: False): whether URL encoding should be applied to the | ||
various elements of the path. | ||
The method returns a tuple containing the HTTP response and data returned as the body | ||
of the response. The type of the data depends on two things. First, if convert_response | ||
is False, plain text is returned. Otherwise, it depends on the return type of the | ||
API call. If (for instance) the return type is "application/json," then a json object | ||
is returned. | ||
""" | ||
path = _build_path(path, encode_path) | ||
_log.info(f"Calling endpoint {path} with verb {verb}") | ||
|
||
if not "api-token" in headers: | ||
if config.key: | ||
headers["api-token"] = config.key | ||
else: | ||
_log.info("No API key was provided to this application; API call will likely fail") | ||
|
||
if not "accept" in headers: | ||
headers["accept"] = "application/json" | ||
|
||
path = f"/api/v1/{path}" | ||
_log.debug(f"Making a {verb} call to {path} at {config.url}") | ||
conn = http.client.HTTPSConnection(config.url) | ||
global _deprected_call_endpoint_reported | ||
if not _deprected_call_endpoint_reported: | ||
_log.warning("Calling deprecated method iriusrisk.v1.facade.call_endpoint; call iriusrisk.v1.call_endpoint instead") | ||
_deprected_call_endpoint_reported = True | ||
|
||
if config.dryrun: | ||
resp = None | ||
else : | ||
conn.request(verb, path, params, headers) | ||
resp = conn.getresponse() | ||
|
||
result = None | ||
if convert_response and not config.dryrun: | ||
data = resp.read().decode("utf-8") | ||
if resp.status == 200 and headers["accept"] == "application/json": | ||
result = json.loads(data) | ||
else: | ||
result = data | ||
|
||
return (resp, result) | ||
|
||
"""Call the specified endpoint using "GET." | ||
""" | ||
def do_get(path, headers={}, params={}, convert_response=True, encode_path=False): | ||
"""Call the indicated endpoint via GET. See call_endpoint for more details.""" | ||
return call_endpoint(path, "GET", headers, params, convert_response, encode_path) | ||
return iriusrisk.v1.call_endpoint(path, verb, headers, params, convert_response, encode_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ includes=["iriusrisk/"] | |
|
||
[project] | ||
name = "iriusrisk_apishell_v1" | ||
version = "0.3.1" | ||
version = "0.3.2" | ||
authors = [ | ||
{ name="Walter Gildersleeve", email="[email protected]"}, | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters