# Path: flask_restaction/res.py
# class Res:
#     """
#     A tool for calling API
# 
#     Will keep a session and handle auth token automatic
# 
#     Usage:
# 
#         >>> res = Res(test_client=app.test_client)  # used in testing
#         >>> res = Res("http://127.0.0.1:5000")  # request remote api
#         >>> res.ajax("/hello")
#         {'message': 'Hello world, Welcome to flask-restaction!'}
#         >>> res.hello.get()
#         {'message': 'Hello world, Welcome to flask-restaction!'}
#         >>> res.hello.get({"name":"kk"})
#         {'message': 'Hello kk, Welcome to flask-restaction!'}
#         >>> res.xxx.get()
#         ...
#         requests.exceptions.HTTPError:
#             404 Client Error: NOT FOUND for url: http://127.0.0.1:5000/xxx
# 
#     Args:
#         url_prefix: url prefix of API
#         auth_header: auth header name of API
#     Attributes:
#         url_prefix: url prefix
#         auth_header: auth header
#         session: requests.Session or TestClientSession
#     """
# 
#     def __init__(self, url_prefix="", test_client=None,
#                  auth_header="Authorization"):
#         self.url_prefix = url_prefix
#         self.auth_header = auth_header
#         if test_client is None:
#             self.session = requests.Session()
#         else:
#             self.session = TestClientSession(test_client)
#         self.session.headers.update({'Accept': 'application/json'})
# 
#     def ajax(self, url, method="GET", data=None, headers=None):
#         """Send request"""
#         params = {
#             "method": method,
#             "url": self.url_prefix + url,
#             "headers": headers
#         }
#         if data is not None:
#             if method in ["GET", "DELETE"]:
#                 params["params"] = data
#             elif method in ["POST", "PUT", "PATCH"]:
#                 params["json"] = data
#         resp = self.session.request(**params)
#         if self.auth_header in resp.headers:
#             self.session.headers[
#                 self.auth_header] = resp.headers[self.auth_header]
#         return resp_json(resp)
# 
#     def _request(self, resource, action, data=None, headers=None):
#         """
#         Send request
# 
#         Args:
#             resource: resource
#             action: action
#             data: string or object which can be json.dumps
#             headers: http headers
#         """
#         url, httpmethod = res_to_url(resource, action)
#         return self.ajax(url, httpmethod, data, headers)
# 
#     def __getattr__(self, resource):
#         return Resource(self, resource)


import importlib
import sys
import pytest
from flask_restaction import Res
from requests import HTTPError


sys.path.append('./server')
app = importlib.import_module("index").app


@pytest.fixture(params=[
    {"url_prefix": "http://127.0.0.1:5000"},
    {"test_client": app.test_client}
])
def res(request):

# Path: flask_restaction/res.py
# class Res:
#     """
#     A tool for calling API
# 
#     Will keep a session and handle auth token automatic
# 
#     Usage:
# 
#         >>> res = Res(test_client=app.test_client)  # used in testing
#         >>> res = Res("http://127.0.0.1:5000")  # request remote api
#         >>> res.ajax("/hello")
#         {'message': 'Hello world, Welcome to flask-restaction!'}
#         >>> res.hello.get()
#         {'message': 'Hello world, Welcome to flask-restaction!'}
#         >>> res.hello.get({"name":"kk"})
#         {'message': 'Hello kk, Welcome to flask-restaction!'}
#         >>> res.xxx.get()
#         ...
#         requests.exceptions.HTTPError:
#             404 Client Error: NOT FOUND for url: http://127.0.0.1:5000/xxx
# 
#     Args:
#         url_prefix: url prefix of API
#         auth_header: auth header name of API
#     Attributes:
#         url_prefix: url prefix
#         auth_header: auth header
#         session: requests.Session or TestClientSession
#     """
# 
#     def __init__(self, url_prefix="", test_client=None,
#                  auth_header="Authorization"):
#         self.url_prefix = url_prefix
#         self.auth_header = auth_header
#         if test_client is None:
#             self.session = requests.Session()
#         else:
#             self.session = TestClientSession(test_client)
#         self.session.headers.update({'Accept': 'application/json'})
# 
#     def ajax(self, url, method="GET", data=None, headers=None):
#         """Send request"""
#         params = {
#             "method": method,
#             "url": self.url_prefix + url,
#             "headers": headers
#         }
#         if data is not None:
#             if method in ["GET", "DELETE"]:
#                 params["params"] = data
#             elif method in ["POST", "PUT", "PATCH"]:
#                 params["json"] = data
#         resp = self.session.request(**params)
#         if self.auth_header in resp.headers:
#             self.session.headers[
#                 self.auth_header] = resp.headers[self.auth_header]
#         return resp_json(resp)
# 
#     def _request(self, resource, action, data=None, headers=None):
#         """
#         Send request
# 
#         Args:
#             resource: resource
#             action: action
#             data: string or object which can be json.dumps
#             headers: http headers
#         """
#         url, httpmethod = res_to_url(resource, action)
#         return self.ajax(url, httpmethod, data, headers)
# 
#     def __getattr__(self, resource):
#         return Resource(self, resource)


# Path: tests/test_res.py
import importlib
import sys
import pytest
from flask_restaction import Res
from requests import HTTPError



sys.path.append('./server')
app = importlib.import_module("index").app


@pytest.fixture(params=[
    {"url_prefix": "http://127.0.0.1:5000"},
    {"test_client": app.test_client}
])
def res(request):

    return Res(**request.param)