WIP: posting shiai to the wkKalendar via api
This commit is contained in:
@@ -12,7 +12,10 @@ class TestApi:
|
||||
argParser = argparse.ArgumentParser(
|
||||
"testApi"
|
||||
)
|
||||
|
||||
argParser.add_argument(
|
||||
"method",
|
||||
choices=['GET', 'POST']
|
||||
)
|
||||
argParser.add_argument(
|
||||
"--endpoint",
|
||||
default="shiai"
|
||||
@@ -40,6 +43,11 @@ class TestApi:
|
||||
choices=_LOG_LEVEL,
|
||||
default="WARNING"
|
||||
)
|
||||
argParser.add_argument(
|
||||
"--input", "-i",
|
||||
type=argparse.FileType("r"),
|
||||
default="-"
|
||||
)
|
||||
|
||||
return argParser.parse_args()
|
||||
|
||||
@@ -59,33 +67,47 @@ class TestApi:
|
||||
|
||||
def apiCall(self):
|
||||
return apiCall.call(
|
||||
method=self.config.method,
|
||||
host=self.config.host,
|
||||
url="/".join([self.config.path, self.config.endpoint]),
|
||||
headers={
|
||||
"Authorization": f"Basic {self.config.key}"
|
||||
}
|
||||
},
|
||||
input=load_json(self.config.input)
|
||||
)
|
||||
|
||||
|
||||
def load_json(jsonFile):
|
||||
import json
|
||||
with jsonFile as f:
|
||||
return json.load(f)
|
||||
|
||||
class apiCall:
|
||||
@staticmethod
|
||||
def call(
|
||||
method: str,
|
||||
host: str,
|
||||
url: str,
|
||||
headers: dict = None
|
||||
headers: dict = None,
|
||||
input: dict = None,
|
||||
) -> dict:
|
||||
import requests
|
||||
|
||||
r = requests.get(
|
||||
url=f"http://{host}/{url}",
|
||||
# @todo The client always awaits this timeout. Even when the
|
||||
# meaningful body is already received.params=
|
||||
# - I don't see any of the examples out there do it different from
|
||||
# me.
|
||||
# - The browser doesn't seem to have this problem.
|
||||
timeout=10,
|
||||
headers=headers
|
||||
)
|
||||
if(method=="GET"):
|
||||
r = requests.get(
|
||||
url=f"http://{host}/{url}",
|
||||
timeout=10,
|
||||
headers=headers
|
||||
)
|
||||
elif(method=="POST"):
|
||||
r = requests.post(
|
||||
json=input,
|
||||
url=f"http://{host}/{url}",
|
||||
timeout=10,
|
||||
headers=headers
|
||||
)
|
||||
else:
|
||||
logging.error("This line should never been reached!")
|
||||
|
||||
try:
|
||||
return r.json()
|
||||
|
||||
Reference in New Issue
Block a user