Skip to content

Commit

Permalink
✨ add callAPI_to_serializedStr. #1
Browse files Browse the repository at this point in the history
  • Loading branch information
perillaroc committed Sep 24, 2019
1 parent d0aa29c commit 225855a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
36 changes: 36 additions & 0 deletions example/serialized_str.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# coding=UTF-8
from nuwe_cimiss.client import CimissClient
import click


@click.command()
@click.option("--user", help="user name", required=True)
@click.option("--password", help="password name", required=True)
@click.option("--client-config", help="client config file")
def cli(user, password, client_config=None):

interface_id = "getSurfEleByTimeRange"

server_id = "NMIC_MUSIC_CMADAAS"

params = {
"dataCode": "SURF_CHN_MUL_HOR",
"elements": "Station_Id_d,Lat,Lon,Alti,Day,Hour,PRS_Sea,TEM,"
"DPT,WIN_D_INST,WIN_S_INST,PRE_1h,PRE_6h,PRE_24h,PRS",
"timeRange": "[20190817000000,20190817020000)",
"orderby": "Station_ID_d:ASC",
"limitCnt": "10",
}

client = CimissClient(
user=user,
password=password,
config_file=client_config
)
result = client.callAPI_to_serializedStr(interface_id, params, data_format="json")

print(result)


if __name__ == "__main__":
cli()
42 changes: 41 additions & 1 deletion nuwe_cimiss/client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# coding: utf-8
import configparser
import json
import logging
import pathlib
from typing import Callable, Any
import warnings
from typing import Callable, Any

from nuwe_cimiss.connection import Connection
from nuwe_cimiss.data import (
Expand Down Expand Up @@ -145,6 +146,45 @@ def callAPI_to_fileList(
exception_handler=Connection.generate_exception_handler(data),
)

def callAPI_to_serializedStr(
self,
interface_id: str,
params: dict,
data_format: str,
server_id: str = None
):
if "dataFormat" not in params:
params["dataFormat"] = data_format

method = self.callAPI_to_serializedStr.__name__

def handle_success(content: bytes) -> str:
return content.decode("utf8")

def handle_failure(content: bytes) -> str:
getway_info = json.loads(content)
if getway_info is None:
return "parse getway return string error:" + content.decode("utf8")
else:
return "getway error: returnCode={return_code} returnMessage={return_message}".format(
return_code=getway_info["returnCode"],
return_message=getway_info["returnMessage"],
)

def handle_exception(exception: Exception):
logger.warning("Error retrieving data: " + str(exception))
return "Error retrieving data"

return self._do_request(
interface_id,
method,
params,
server_id,
success_handler=handle_success,
failure_handler=handle_failure,
exception_handler=handle_exception,
)

def _load_config(self) -> None:
if self.config_file is not None:
self.config_file = pathlib.Path(self.config_file)
Expand Down

0 comments on commit 225855a

Please sign in to comment.