Skip to content

Commit

Permalink
Merge pull request timkpaine#348 from timkpaine/tkp/contenttype
Browse files Browse the repository at this point in the history
Determine content type locally
  • Loading branch information
timkpaine committed Nov 30, 2021
2 parents e81c04c + 019d47a commit 53f64c8
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 26 deletions.
1 change: 1 addition & 0 deletions pyEX/common/__init__.py
Expand Up @@ -32,6 +32,7 @@
_timeseriesWrapper,
_toDatetime,
json_normalize,
_interpolateDatatype,
)
from .exception import *
from .timing import _EST, _PYEX_CACHE_FOLDER, _UTC, _expire, _interval
Expand Down
20 changes: 20 additions & 0 deletions pyEX/common/checks.py
Expand Up @@ -12,6 +12,7 @@

import re
import pandas as pd
import json
from six import string_types

from .exception import PyEXception
Expand Down Expand Up @@ -535,6 +536,25 @@ def _overrideFormat(kwargs):
kwargs["format"] = "json"


def _interpolateDatatype(data):
"""Attempt to determine the data type from the data"""
if isinstance(data, str):
# check if json or else default to csv
try:
if data.strip().startswith("[{") or data.strip().startswith("{"):
return data, {"content-type": "application/json"}
return data, {"content-type": "text/csv"}

except ValueError:
return data, {"content-type": "text/csv"}

if isinstance(data, list):
if data and isinstance(data[0], dict):
# json
return json.dumps(data), {"content-type": "application/json"}
raise PyEXception("Could not determine data type")


try:
if pd.__version__ > "1.":
json_normalize = pd.json_normalize
Expand Down

0 comments on commit 53f64c8

Please sign in to comment.