Add environment variable for default request method (#521)

This commit is contained in:
Mal Miller 2021-12-23 08:46:22 +00:00 committed by GitHub
parent 13645661d8
commit 8f1e2b0aae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 2 deletions

View File

@ -17,3 +17,12 @@ ENV_CLEARML_NO_DEFAULT_SERVER = EnvEntry("CLEARML_NO_DEFAULT_SERVER", "TRAINS_NO
ENV_DISABLE_VAULT_SUPPORT = EnvEntry('CLEARML_DISABLE_VAULT_SUPPORT', type=bool)
ENV_ENABLE_ENV_CONFIG_SECTION = EnvEntry('CLEARML_ENABLE_ENV_CONFIG_SECTION', type=bool)
ENV_ENABLE_FILES_CONFIG_SECTION = EnvEntry('CLEARML_ENABLE_FILES_CONFIG_SECTION', type=bool)
"""
Experimental option to set the request method for all API requests and auth login.
This could be useful when GET requests with payloads are blocked by a server as
POST requests can be used instead.
However this has not been vigorously tested and may have unintended consequences.
"""
ENV_API_DEFAULT_REQ_METHOD = EnvEntry("CLEARML_API_DEFAULT_REQ_METHOD")

View File

@ -1,14 +1,21 @@
import abc
import jsonschema
import os
import six
from .apimodel import ApiModel
from .datamodel import DataModel
from .defs import ENV_API_DEFAULT_REQ_METHOD
if ENV_API_DEFAULT_REQ_METHOD.exists() and ENV_API_DEFAULT_REQ_METHOD.get().upper() not in ("GET", "POST"):
raise ValueError(
"CLEARML_API_DEFAULT_REQ_METHOD environment variable must be 'get' or 'post' (any case is allowed)."
)
class Request(ApiModel):
_method = 'get'
_method = ENV_API_DEFAULT_REQ_METHOD.get(default="get")
def __init__(self, **kwargs):
allow_extra_fields = kwargs.pop("_allow_extra_fields_", False)

View File

@ -1,4 +1,5 @@
import json as json_lib
import os
import sys
import types
from socket import gethostname
@ -24,6 +25,7 @@ from .defs import (
ENV_DISABLE_VAULT_SUPPORT,
ENV_ENABLE_ENV_CONFIG_SECTION,
ENV_ENABLE_FILES_CONFIG_SECTION,
ENV_API_DEFAULT_REQ_METHOD,
)
from .request import Request, BatchRequest # noqa: F401
from .token_manager import TokenManager
@ -709,6 +711,7 @@ class Session(TokenManager):
try:
data = {"expiration_sec": exp} if exp else {}
res = self._send_request(
method=ENV_API_DEFAULT_REQ_METHOD.get(default="get"),
service="auth",
action="login",
auth=auth,