def test_basic_authentication_alters_client(): client = APIClient( authentication_method=BasicAuthentication(username="******", password="******"), response_handler=BaseResponseHandler, request_formatter=BaseRequestFormatter, ) assert client.get_default_query_params() == {} assert client.get_default_headers() == {} assert client.get_default_username_password_authentication() == ("uname", "password")
def test_header_authentication_overwriting_parameter(): client = APIClient( authentication_method=HeaderAuthentication(token="secret", parameter="APIKEY"), response_handler=BaseResponseHandler, request_formatter=BaseRequestFormatter, ) assert client.get_default_query_params() == {} assert client.get_default_headers() == {"APIKEY": "Bearer secret"} assert client.get_default_username_password_authentication() is None
def test_scheme_is_not_included_when_evaluates_to_false(scheme): client = APIClient( authentication_method=HeaderAuthentication(token="secret", parameter="APIKEY", scheme=scheme), response_handler=BaseResponseHandler, request_formatter=BaseRequestFormatter, ) assert client.get_default_query_params() == {} assert client.get_default_headers() == {"APIKEY": "secret"} assert client.get_default_username_password_authentication() is None
def test_no_authentication_method_does_not_alter_client(): client = APIClient( authentication_method=NoAuthentication(), response_handler=BaseResponseHandler, request_formatter=BaseRequestFormatter, ) assert client.get_default_query_params() == {} assert client.get_default_headers() == {} assert client.get_default_username_password_authentication() is None
def test_query_parameter_authentication_alters_client_default_query_parameters(): client = APIClient( authentication_method=QueryParameterAuthentication(parameter="apikey", token="secret"), response_handler=BaseResponseHandler, request_formatter=BaseRequestFormatter, ) assert client.get_default_query_params() == {"apikey": "secret"} assert client.get_default_headers() == {} assert client.get_default_username_password_authentication() is None
def test_header_authentication_with_extra_parameters(): client = APIClient( authentication_method=HeaderAuthentication( token="secret", parameter="APIKEY", extra={"another key": "another value"}), response_handler=BaseResponseHandler, request_formatter=BaseRequestFormatter, ) assert client.get_default_query_params() == {} assert client.get_default_headers() == { "APIKEY": "Bearer secret", "another key": "another value" } assert client.get_default_username_password_authentication() is None
class App(object): def __init__(self): self.gui = gui("RFID Warenkorbsystem", self.getResolution()) self.gui.setResizable(False) self.gui.setGeometry("fullscreen") self.gui.setSticky("news") self.gui.setExpand("both") self.gui.setFont(18) self.gui.startLabelFrame("Details") self.gui.addLabel("status", 'Initialisiere PN532...') self.gui.addButton("Clear", self.clear) self.gui.stopLabelFrame() self.gui.addGrid("products", [["Produkt", "Preis"], []]) self.gui.setGridWidth("products", 1920) reader = Reader(self) self.apiClient = APIClient() self.gui.registerEvent(reader.read) self.gui.go() def getResolution(self): window = gtk.Window() screen = window.get_screen() return str(screen.get_width()) + "x" + str(screen.get_height()) def addProduct(self, productId): productInformation = self.apiClient.getProductInformation(productId) price = int(productInformation['price']) / 10 self.gui.addGridRow("products", [productInformation['name'], "CHF " + str(price)]) def clear(self, event): self.gui.removeGrid("products") self.gui.addGrid("products", [["Produkt", "Preis"], []])
def set_strategy(client: APIClient, strategy: BaseRequestStrategy): """Set a strategy on the client and then set it back after running.""" temporary_client = client.clone() temporary_client.set_request_strategy(strategy) try: yield temporary_client finally: del temporary_client
def __init__(self): self.gui = gui("RFID Warenkorbsystem", self.getResolution()) self.gui.setResizable(False) self.gui.setGeometry("fullscreen") self.gui.setSticky("news") self.gui.setExpand("both") self.gui.setFont(18) self.gui.startLabelFrame("Details") self.gui.addLabel("status", 'Initialisiere PN532...') self.gui.addButton("Clear", self.clear) self.gui.stopLabelFrame() self.gui.addGrid("products", [["Produkt", "Preis"], []]) self.gui.setGridWidth("products", 1920) reader = Reader(self) self.apiClient = APIClient() self.gui.registerEvent(reader.read) self.gui.go()
def test_cookie_authentication_makes_request_on_client_initialization(mock_requests): cookiejar = http.cookiejar.CookieJar() mocker = mock_requests.get("http://example.com/authenticate", status_code=200, cookies=cookiejar) APIClient( authentication_method=CookieAuthentication( auth_url="http://example.com/authenticate", authentication=HeaderAuthentication(token="foo") ), response_handler=RequestsResponseHandler, request_formatter=NoOpRequestFormatter, ) assert mocker.called assert mocker.call_count == 1
def run(self): print("Starting server...") self.updater = Updater(token = BotServer.TOKEN) self.dispatcher = self.updater.dispatcher self.job_queue = self.updater.job_queue # self.ipchecker = ipchecker.IPChecker(self) # self.ipchecker.start() # self.db = database.Database() # self.db.connect() self.api = APIClient() logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) start_handler = CommandHandler('start', self.start) echo_handler = MessageHandler(Filters.text, BotServer.echo) caps_handler = CommandHandler('caps', BotServer.caps, pass_args=True) inline_caps_handler = InlineQueryHandler(BotServer.inline_caps) # checkip_handler = CommandHandler('checkip', self.check_ip) get_languages_handler = CommandHandler('get_languages', self.get_languages) unknown_handler = MessageHandler(Filters.command, BotServer.unknown) self.dispatcher.add_handler(start_handler) self.dispatcher.add_handler(echo_handler) self.dispatcher.add_handler(caps_handler) self.dispatcher.add_handler(inline_caps_handler) # self.dispatcher.add_handler(checkip_handler) self.dispatcher.add_handler(get_languages_handler) self.dispatcher.add_handler(unknown_handler) print("Server started.") self.updater.start_polling() try: while True: pass except KeyboardInterrupt: print("Stopping server. Please wait...") # self.db.close() # self.ipchecker.join() self.updater.stop() print("Server stopped.")
def main(): cl = APIClient(CLIENT_ID, API_KEY) print(cl.get_doc()) pprint(cl.get_active_droplets()) pprint(cl.get_droplet(0))
def test_mock_strategy(): mock_strategy = Mock(spec=BaseRequestStrategy) client = APIClient(request_strategy=mock_strategy) client.get("http://google.com") mock_strategy.get.assert_called_with("http://google.com", params=None)