#!/usr/bin/env python
# -*- coding:utf8 -*-

#      Copyright 2017, Schuberg Philis BV
#
#      Licensed to the Apache Software Foundation (ASF) under one
#      or more contributor license agreements.  See the NOTICE file
#      distributed with this work for additional information
#      regarding copyright ownership.  The ASF licenses this file
#      to you under the Apache License, Version 2.0 (the
#      "License"); you may not use this file except in compliance
#      with the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#      Unless required by applicable law or agreed to in writing,
#      software distributed under the License is distributed on an
#      "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#      KIND, either express or implied.  See the License for the
#      specific language governing permissions and limitations
#      under the License.

#      Romero Galiza Jr. - rgaliza@schubergphilis.com

import requests


class HttpRequestor(object):

    """Defines a basic REST API client to be extended by components."""

    def __init__(self, url, port, user, key, warn=False):
        """ CloudianREST constructor, requires information regarding the admin
        API server provided by Cloudian Inc. Hyperstore.

        :param url:         the admin url configured on your hyperstore cluster
        :type url:          str
        :param port:        TCP port configured to listen HTTP/S requests
        :type port:         int
        :param user:        admin API user configured on your cluster
        :type user:         str
        :param key:         API key configured on your cluster
        :type key:          str
        :param warn:        if set to True, warnings are enabled
        :type warn:         bool
        :rtype:             HttpRequestor
        """
        self.url = url
        self.port = port
        self.user = user
        self.key = key
        self.warn = warn

    def request(self, url, data=None, json=None, method='GET'):
        """ Builds a request and fetches its response from the admin API
        server, returns a JSON encoded string. SSL verification is disabled.
        Simply put: this method works as a request.requests() wrapper.

        :param url:    the endpoint with all parameters to GET request
        :type url:     str
        :param method:      HTTP method (GET, POST, HEAD, DELETE, etc)
        :type method:       str
        :param data:        file-like object to send in the body of the request
        :type data:         dict
        :param json:        json data to send in the body of the request
        :type json:         dict
        :rtype:             str
        """
        if not self.warn:
            requests.urllib3.disable_warnings()

        api_call = '{url}:{port}/{call}'.format(
            url=self.url, port=self.port, call=url
        )

        try:
            response = requests.request(
                verify=False,
                method=method,
                url=api_call,
                data=data,
                json=json,
                auth=(
                    self.user, self.key
                ),
            )
            if response.status_code == 200:
                try:
                    return response.json()
                except ValueError:
                    # GET /system/version
                    return response.text
            else:
                return {
                    'reason': response.reason,
                    'status_code': response.status_code,
                    'url': response.request.url
                }
        except requests.exceptions.ConnectionError as err:
            print(str(err.message))
            exit(1)

@@ -2932,33 +2932,16 @@
      )%0A%0A
-        try:%0A    
         
@@ -2969,20 +2969,16 @@
 equest(%0A
-    
         
@@ -3007,20 +3007,16 @@
         
-    
 method=m
@@ -3030,28 +3030,24 @@
             
-  
-  
 url=api_call
@@ -3060,20 +3060,16 @@
         
-    
 data=dat
@@ -3079,28 +3079,24 @@
             
- 
-   
 json=json,%0A 
@@ -3110,23 +3110,15 @@
     
-   
- 
 auth=(%0A
-    
     
@@ -3161,20 +3161,16 @@
         
-    
 ),%0A     
@@ -3176,18 +3176,19 @@
     
+)%0A
     
-)%0A
     
+%0A
     
@@ -3235,20 +3235,16 @@
         
-    
 try:%0A   
@@ -3248,36 +3248,32 @@
                 
-  
-  
 return response.
@@ -3287,28 +3287,24 @@
             
-    
 except Value
@@ -3326,20 +3326,16 @@
         
- 
-   
 # GET /s
@@ -3348,20 +3348,16 @@
 version%0A
-    
         
@@ -3393,20 +3393,16 @@
         
-    
 else:%0A  
@@ -3407,28 +3407,24 @@
             
-  
-  
 return %7B%0A   
@@ -3420,20 +3420,16 @@
 eturn %7B%0A
-    
         
@@ -3479,20 +3479,16 @@
         
-    
 'status_
@@ -3532,20 +3532,16 @@
         
-   
- 
 'url': r
@@ -3576,125 +3576,7 @@
     
-    %7D%0A        except requests.exceptions.ConnectionError as err:%0A            print(str(err.message))%0A            exit(1)
+%7D%0A
 %0A

remove exit(1)