Skip to content

Commit

Permalink
Merge pull request #23 from SermetPekin/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
SermetPekin authored Apr 22, 2024
2 parents db0d70e + 32a399f commit fd55a7b
Show file tree
Hide file tree
Showing 15 changed files with 533 additions and 463 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Byte-compiled / optimized / DLL files
__pycache__/



*.py[cod]
*$py.class
.idea/
Expand Down Expand Up @@ -268,3 +270,6 @@ cython_debug/
#.idea/
/_version.py
/EVDSlocal/APIKEY_FOLDER/


!Api_requester.py
43 changes: 34 additions & 9 deletions evdspy/EVDSlocal/index_requests/get_series_indexes.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@

from typing import Union, Any, Optional

import pandas as pd
from evdspy.EVDSlocal.config.apikey_class import ApikeyClass
from evdspy.EVDSlocal.index_requests.get_series_indexes_utils import default_start_date_fnc, default_end_date_fnc
from evdspy.EVDSlocal.index_requests.user_requests.user_requests import ProxyManager, UrlBuilder, ApiRequester, \
DataProcessor, RequestConfig

from evdspy.EVDSlocal.index_requests.user_requests.Request_config import RequestConfig


def initial_api_process_when_given(api_key: Optional[str] = None) -> None:
from evdspy.EVDSlocal.config.apikey_class import ApikeyClass
from evdspy.EVDSlocal.index_requests.get_series_indexes_utils import default_start_date_fnc, default_end_date_fnc

from evdspy.EVDSlocal.index_requests.user_requests import RequestConfig, ProxyManager, \
UrlBuilder, DataProcessor
from evdspy.EVDSlocal.index_requests.user_requests.Api_requester import ApiRequester


if api_key is None:
return
if ApikeyClass().get_valid_api_key(check=False) is False:
from evdspy.EVDSlocal.initial.load_commands_cmds_to_load import save_apikey
save_apikey(api_key)

from evdspy.EVDSlocal.index_requests.get_series_indexes_utils import (
default_start_date_fnc,
default_end_date_fnc,
correct_types,
)

def get_series(
index: Union[str, tuple[Any, ...]],
start_date: str = default_start_date_fnc(),
Expand Down Expand Up @@ -67,6 +79,13 @@ def get_series(
ValueError
If an invalid API key is provided or required parameters are missing.
"""
from evdspy.EVDSlocal.config.apikey_class import ApikeyClass
from evdspy.EVDSlocal.index_requests.get_series_indexes_utils import default_start_date_fnc, default_end_date_fnc

from evdspy.EVDSlocal.index_requests.user_requests import RequestConfig, ProxyManager, \
UrlBuilder, DataProcessor
from evdspy.EVDSlocal.index_requests.user_requests.Api_requester import ApiRequester

# ............initial_api_process_when_given...............
initial_api_process_when_given(api_key)
# ............RequestConfig................................
Expand All @@ -89,19 +108,25 @@ def get_series(
# ............DataProcessor................................
data_processor = DataProcessor(api_requester())
return data_processor()


def test_get_series2(capsys):
with capsys.disabled():
# setup()
df = get_series(
"TP.ODEMGZS.BDTTOPLAM",
cache=False
"TP.ODEMGZS.BDTTOPLAM",
cache=False
)
assert isinstance(df, pd.DataFrame)


def t_stream():
import streamlit as st
df = get_series("TP.ODEMGZS.BDTTOPLAM",
cache=True)
st.write(df)


__all__ = (
'get_series',
)
'get_series',
)
96 changes: 96 additions & 0 deletions evdspy/EVDSlocal/index_requests/user_requests/Api_requester.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# ...........................................................................................
import traceback
import requests
# ...........................................................................................
from evdspy.EVDSlocal.index_requests.user_requests import UrlBuilder, ProxyManager, RequestConfig
from evdspy.EVDSlocal.index_requests.user_requests.User_request_utils import cache_or_raw_fnc
from evdspy.EVDSlocal.utils.utils_test import ApiClassWhileTesting
from evdspy.EVDSlocal.requests_.real_requests import *
from evdspy.EVDSlocal.utils.github_actions import PytestTesting, GithubActions
# ...........................................................................................


# ....................................................................... ApiRequester
class ApiRequester:
def __init__(self, url_builder: UrlBuilder, proxy_manager: ProxyManager):
self.url_builder = url_builder
self.proxy_manager = proxy_manager
self.proxies = self.proxy_manager.get_proxies()
self.url = self.url_builder.url
self.response = None

def get(self):
return self.request()

def __call__(self, *args, **kwargs):
return self.get()

def dry_request(self) -> RequestConfig:
api_key = self.get_api_key(check=False)
print(f"""
---------------------------------
[debug mode is turned on]
api_key = {self.obscure(api_key)}
proxies = {self.proxies}
url = {self.url}
! request was not made because debug mode is turned on
in order to make the request run get_series(... , debug = False )
---------------------------------
""")
return self.url_builder.config

def is_response_ok(self, response):
return isinstance(response, requests.Response) \
and response.status_code == 200

@staticmethod
def obscure(string: str):
return ApikeyClass().obscure(string)

def get_api_key(self, check=True) -> str:
if PytestTesting().is_testing() or GithubActions().is_testing():
api_key = self.get_api_key_while_testing()
else:
api_key = ApikeyClass().get_valid_api_key(check=check)
return api_key

def get_api_key_while_testing(self):
return ApiClassWhileTesting()()

def request(self) -> Any:
api_key = self.get_api_key()
if api_key is False:
if GithubActions().is_testing():
return self.dry_request()
if PytestTesting().is_testing():
raise NotImplementedError
proxies = self.proxy_manager.get_proxies()

def local_request(url: str) -> requests.Response:
requester = RealRequestWithParam(url,
proxies=proxies,
api_key=api_key)
return requester.request()

request_func = cache_or_raw_fnc(local_request,
cache=self.url_builder.config.cache)
response = False
try:
response = request_func(self.url)
except Exception as exc:
traceback.print_exc()
self.response = response
if not self.is_response_ok(response):
return False
# raise HTTPError(response=response)
return response.json()

def is_ok(self) -> bool:
response = self.response
ok = isinstance(response, requests.Response) and response.status_code == 200
if not ok:
self.dry_request()
raise ValueError("request Not Ok")
else:
print("<data is here>")
return ok
35 changes: 35 additions & 0 deletions evdspy/EVDSlocal/index_requests/user_requests/Data_processor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# ....................................................................... DataProcessor
import traceback
from typing import Any

import pandas as pd

from evdspy.EVDSlocal.index_requests.index_util_funcs import make_df_float, json_to_df
from evdspy.EVDSlocal.index_requests.user_requests.User_req_typings import T_maybeDf


class DataProcessor:
def __init__(self, data: Any):
self.data = data

def process_to_dataframe(self) -> T_maybeDf:
if self.data is False:
return False
try:
df = json_to_df(self.data)
except Exception as e:
print(e)
traceback.print_exc()
return None
if isinstance(df, pd.DataFrame):
df = make_df_float(df)
return df

def __call__(self, *args, **kwargs) -> T_maybeDf:
return self.process_to_dataframe()


def test_DataProcessor(capsys):
with capsys.disabled():
d = DataProcessor(False)
print(d)
29 changes: 29 additions & 0 deletions evdspy/EVDSlocal/index_requests/user_requests/Proxy_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# ....................................................................... ProxyManager
from dataclasses import dataclass
from typing import Optional, Any


@dataclass
class ProxyManager:
proxy: Optional[str] = None
proxies: Optional[dict[Any, Any]] = None

def get_proxies(self) -> Optional[dict[Any, Any]]:
if self.proxies is None:
if self.proxy is None:
proxies = None
else:
proxies = self.get_proxies_helper()
else:
proxies = self.proxies
return proxies

def get_proxies_helper(self) -> Optional[dict[Any, Any]]:
if self.proxy is None:
return None
proxy = self.proxy
proxies = {
'http': proxy,
'https': proxy,
}
return proxies
Loading

0 comments on commit fd55a7b

Please sign in to comment.