Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

Add tox and travis support #9

Merged
merged 3 commits into from
Jun 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,9 @@ ENV/

# Rope project settings
.ropeproject

# Intellij IDEA files
.idea/*
*.iml
.vscode

33 changes: 33 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# ref: https://docs.travis-ci.com/user/languages/python
language: python
sudo: required

matrix:
include:
- python: 2.7
env: TOXENV=py27
- python: 2.7
env: TOXENV=py27-functional
- python: 2.7
env: TOXENV=update-pep8
- python: 2.7
env: TOXENV=docs
- python: 2.7
env: TOXENV=coverage,codecov
- python: 3.4
env: TOXENV=py34
- python: 3.5
env: TOXENV=py35
- python: 3.5
env: TOXENV=py35-functional
- python: 3.6
env: TOXENV=py36
- python: 3.6
env: TOXENV=py36-functional

install:
- pip install tox

script:
- ./run_tox.sh tox

80 changes: 45 additions & 35 deletions api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,21 @@

from __future__ import absolute_import

from . import models
from . import ws_client
from .rest import RESTClientObject
from .rest import ApiException

import os
import re
import json
import mimetypes
import os
import re
import tempfile
import threading

from datetime import datetime
from datetime import date
from datetime import date, datetime

# python 2 and python 3 compatibility library
from six import PY3, integer_types, iteritems, text_type
from six.moves.urllib.parse import quote

from . import models, ws_client
from .configuration import configuration
from .rest import ApiException, RESTClientObject


class ApiClient(object):
Expand All @@ -59,9 +54,9 @@ class ApiClient(object):
:param header_name: a header to pass when making calls to the API.
:param header_value: a header value to pass when making calls to the API.
"""

def __init__(self, host=None, header_name=None, header_value=None,
cookie=None, config=configuration):

"""
Constructor of the class.
"""
Expand Down Expand Up @@ -99,8 +94,8 @@ def __call_api(self, resource_path, method,
path_params=None, query_params=None, header_params=None,
body=None, post_params=None, files=None,
response_type=None, auth_settings=None, callback=None,
_return_http_data_only=None, collection_formats=None, _preload_content=True,
_request_timeout=None):
_return_http_data_only=None, collection_formats=None,
_preload_content=True, _request_timeout=None):

# header parameters
header_params = header_params or {}
Expand Down Expand Up @@ -163,11 +158,16 @@ def __call_api(self, resource_path, method,
return_data = None

if callback:
callback(return_data) if _return_http_data_only else callback((return_data, response_data.status, response_data.getheaders()))
if _return_http_data_only:
callback(return_data)
else:
callback((return_data,
response_data.status, response_data.getheaders()))
elif _return_http_data_only:
return (return_data)
else:
return (return_data, response_data.status, response_data.getheaders())
return (return_data, response_data.status,
response_data.getheaders())

def sanitize_for_serialization(self, obj):
"""
Expand All @@ -194,7 +194,7 @@ def sanitize_for_serialization(self, obj):
for sub_obj in obj]
elif isinstance(obj, tuple):
return tuple(self.sanitize_for_serialization(sub_obj)
for sub_obj in obj)
for sub_obj in obj)
elif isinstance(obj, (datetime, date)):
return obj.isoformat()
else:
Expand Down Expand Up @@ -248,7 +248,7 @@ def __deserialize(self, data, klass):
if data is None:
return None

if type(klass) == str:
if isinstance(klass, str):
if klass.startswith('list['):
sub_kls = re.match('list\[(.*)\]', klass).group(1)
return [self.__deserialize(sub_data, sub_kls)
Expand Down Expand Up @@ -285,8 +285,8 @@ def call_api(self, resource_path, method,
path_params=None, query_params=None, header_params=None,
body=None, post_params=None, files=None,
response_type=None, auth_settings=None, callback=None,
_return_http_data_only=None, collection_formats=None, _preload_content=True,
_request_timeout=None):
_return_http_data_only=None, collection_formats=None,
_preload_content=True, _request_timeout=None):
"""
Makes the HTTP request (synchronous) and return the deserialized data.
To make an async request, define a function for callback.
Expand All @@ -307,13 +307,18 @@ def call_api(self, resource_path, method,
:param callback function: Callback function for asynchronous request.
If provide this parameter,
the request will be called asynchronously.
:param _return_http_data_only: response data without head status code and headers
:param _return_http_data_only: response data without head status code
and headers
:param collection_formats: dict of collection formats for path, query,
header, and post parameters.
:param _preload_content: if False, the urllib3.HTTPResponse object will be returned without
reading/decoding response data. Default is True.
:param _request_timeout: timeout setting for this request. If one number provided, it will be total request
timeout. It can also be a pair (tuple) of (connection, read) timeouts.
:param _preload_content: if False, the urllib3.HTTPResponse object will
be returned without
reading/decoding response data.
Default is True.
:param _request_timeout: timeout setting for this request. If one
number provided, it will be total request
timeout. It can also be a pair (tuple) of
(connection, read) timeouts.
:return:
If provide parameter callback,
the request will be called asynchronously.
Expand All @@ -326,7 +331,8 @@ def call_api(self, resource_path, method,
path_params, query_params, header_params,
body, post_params, files,
response_type, auth_settings, callback,
_return_http_data_only, collection_formats, _preload_content, _request_timeout)
_return_http_data_only, collection_formats,
_preload_content, _request_timeout)
else:
thread = threading.Thread(target=self.__call_api,
args=(resource_path, method,
Expand All @@ -335,18 +341,22 @@ def call_api(self, resource_path, method,
post_params, files,
response_type, auth_settings,
callback, _return_http_data_only,
collection_formats, _preload_content, _request_timeout))
collection_formats,
_preload_content,
_request_timeout))
thread.start()
return thread

def request(self, method, url, query_params=None, headers=None,
post_params=None, body=None, _preload_content=True, _request_timeout=None):
post_params=None, body=None, _preload_content=True,
_request_timeout=None):
"""
Makes the HTTP request using RESTClient.
"""
# FIXME(dims) : We need a better way to figure out which
# calls end up using web sockets
if (url.endswith('/exec') or url.endswith('/attach')) and (method == "GET" or method == "POST"):
if (url.endswith('/exec') or url.endswith('/attach')) and \
(method == "GET" or method == "POST"):
return ws_client.websocket_call(self.config,
url,
query_params=query_params,
Expand Down Expand Up @@ -458,14 +468,15 @@ def prepare_post_parameters(self, post_params=None, files=None):
for k, v in iteritems(files):
if not v:
continue
file_names = v if type(v) is list else [v]
file_names = v if isinstance(v, list) else [v]
for n in file_names:
with open(n, 'rb') as f:
filename = os.path.basename(f.name)
filedata = f.read()
mimetype = mimetypes.\
guess_type(filename)[0] or 'application/octet-stream'
params.append(tuple([k, tuple([filename, filedata, mimetype])]))
mimetype = (mimetypes.guess_type(filename)[0] or
'application/octet-stream')
params.append(tuple([k, tuple([filename, filedata,
mimetype])]))

return params

Expand Down Expand Up @@ -543,9 +554,8 @@ def __deserialize_file(self, response):

content_disposition = response.getheader("Content-Disposition")
if content_disposition:
filename = re.\
search(r'filename=[\'"]?([^\'"\s]+)[\'"]?', content_disposition).\
group(1)
filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?',
content_disposition).group(1)
path = os.path.join(os.path.dirname(path), filename)

with open(path, "w") as f:
Expand Down
4 changes: 2 additions & 2 deletions config/kube_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ def set_active_context(self, context_name=None):
context_name = self._config['current-context']
self._current_context = self._config['contexts'].get_with_name(
context_name)
if (self._current_context['context'].safe_get('user')
and self._config.safe_get('users')):
if (self._current_context['context'].safe_get('user') and
self._config.safe_get('users')):
user = self._config['users'].get_with_name(
self._current_context['context']['user'], safe=True)
if user:
Expand Down
29 changes: 14 additions & 15 deletions configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
"""
Kubernetes

No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)

OpenAPI spec version: v1.5.0-snapshot

Generated by: https://github.com/swagger-api/swagger-codegen.git
First version Generated by: https://github.com/swagger-api/swagger-codegen

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -24,11 +20,10 @@

from __future__ import absolute_import

import urllib3

import sys
import logging
import sys

import urllib3
from six import iteritems
from six.moves import http_client as httplib

Expand Down Expand Up @@ -77,7 +72,8 @@ def __init__(self):
self.debug = False

# SSL/TLS verification
# Set this to false to skip verifying SSL certificate when calling API from https server.
# Set this to false to skip verifying SSL certificate when calling API
# from https server.
self.verify_ssl = True
# Set this to customize the certificate file to verify the peer.
self.ssl_ca_cert = None
Expand All @@ -101,8 +97,8 @@ def logger_file(self, value):
"""
Sets the logger_file.

If the logger_file is None, then add stream handler and remove file handler.
Otherwise, add file handler and remove stream handler.
If the logger_file is None, then add stream handler and remove file
handler. Otherwise, add file handler and remove stream handler.

:param value: The logger_file path.
:type: str
Expand Down Expand Up @@ -184,8 +180,10 @@ def get_api_key_with_prefix(self, identifier):
:param identifier: The identifier of apiKey.
:return: The token for api key authentication.
"""
if self.api_key.get(identifier) and self.api_key_prefix.get(identifier):
return self.api_key_prefix[identifier] + ' ' + self.api_key[identifier]
if (self.api_key.get(identifier) and
self.api_key_prefix.get(identifier)):
return (self.api_key_prefix[identifier] + ' ' +
self.api_key[identifier])
elif self.api_key.get(identifier):
return self.api_key[identifier]

Expand All @@ -195,8 +193,9 @@ def get_basic_auth_token(self):

:return: The token for basic HTTP authentication.
"""
return urllib3.util.make_headers(basic_auth=self.username + ':' + self.password)\
.get('authorization')
return urllib3.util.make_headers(
basic_auth=self.username + ':' + self.password).get(
'authorization')

def auth_settings(self):
"""
Expand Down
Loading