1- # -*- coding: utf-8 -*-
21from .authentication import Authentication
32from .authentication import DEFAULT_USER_AGENT
4- from .credentials import Credentials
3+ from .credentials import Profile
54from .requester import Requester
5+ from requests import Session
6+ from requests .adapters import HTTPAdapter
7+ from urllib3 .util .retry import Retry
8+ from urllib3 .util import parse_url
69
710import json
8- import os
9-
11+ import warnings
1012
1113class Call (object ):
1214 def __init__ (self , logger = None , limiter = None , ** kwargs ):
@@ -16,53 +18,47 @@ def __init__(self, logger=None, limiter=None, **kwargs):
1618 self .user_agent = kwargs .pop ("user_agent" , DEFAULT_USER_AGENT )
1719 self .logger = logger
1820 self .limiter = limiter
19- self .update_credentials (
21+ self .update_profile (
2022 access_key = kwargs .pop ("access_key" , None ),
2123 secret_key = kwargs .pop ("secret_key" , None ),
2224 region = kwargs .pop ("region" , None ),
2325 profile = kwargs .pop ("profile" , None ),
24- email = kwargs .pop ("email " , None ),
26+ login = kwargs .pop ("login " , None ),
2527 password = kwargs .pop ("password" , None ),
26- proxy = kwargs .pop ("proxy" , None ),
2728 x509_client_cert = kwargs .pop ("x509_client_cert" , None ),
28- max_retries = kwargs .pop ("max_retries" , None ),
29- retry_backoff_factor = kwargs .pop ("retry_backoff_factor" , None ),
30- retry_backoff_jitter = kwargs .pop ("retry_backoff_jitter" , None ),
31- retry_backoff_max = kwargs .pop ("retry_backoff_max" , None )
3229 )
3330 self .update_limiter (
3431 limiter_max_requests = kwargs .pop ("limiter_max_requests" , None ),
35- limiter_window = kwargs .pop ("limiter_window" , None )
32+ limiter_window = kwargs .pop ("limiter_window" , None ),
3633 )
34+ self .session = Session ()
35+ self .session .mount ("https://" , self .adapter )
36+ self .session .mount ("http://" , self .adapter )
3737
38- def update_credentials (
39- self ,
40- region = None ,
41- profile = None ,
42- access_key = None ,
43- secret_key = None ,
44- email = None ,
45- password = None ,
46- proxy = None ,
47- x509_client_cert = None ,
48- max_retries = None ,
49- retry_backoff_factor = None ,
50- retry_backoff_jitter = None ,
51- retry_backoff_max = None ,
52- ):
53- self .credentials = {
54- "access_key" : access_key ,
55- "secret_key" : secret_key ,
56- "region" : region ,
57- "profile" : profile ,
58- "email" : email ,
59- "password" : password ,
60- "x509_client_cert" : x509_client_cert ,
61- "max_retries" : max_retries ,
62- "retry_backoff_factor" : retry_backoff_factor ,
63- "retry_backoff_jitter" : retry_backoff_jitter ,
64- "retry_backoff_max" : retry_backoff_max ,
65- }
38+ def update_credentials (self , ** kwargs ):
39+ warnings .warn (
40+ "update_credentials is deprecated, use update_profile instead" ,
41+ DeprecationWarning ,
42+ stacklevel = 2 ,
43+ )
44+ self .update_profile (** kwargs )
45+
46+ def update_profile (self , ** kwargs ):
47+ self .profile = Profile .from_standard_configuration (
48+ kwargs .pop ("path" , None ), kwargs .pop ("profile" , None )
49+ )
50+ self .profile .merge (Profile (** kwargs ))
51+
52+ self .adapter = HTTPAdapter (
53+ max_retries = Retry (
54+ total = 3 ,
55+ backoff_factor = 1 ,
56+ backoff_jitter = 3 ,
57+ backoff_max = 30 ,
58+ status_forcelist = (400 , 429 , 500 , 503 ),
59+ allowed_methods = ("POST" , "GET" ),
60+ )
61+ )
6662
6763 def update_limiter (
6864 self ,
@@ -75,36 +71,27 @@ def update_limiter(
7571 if limiter_max_requests is not None :
7672 self .limiter .max_requests = limiter_max_requests
7773
78-
79- def api (self , action , ** data ):
74+ def api (self , action , service = "api" , ** data ):
8075 try :
81- credentials = Credentials (** self .credentials )
82- host = (
83- self .host
84- if self .host
85- else "api.{}.outscale.{}" .format (
86- credentials .region , credentials .get_url_extension ()
87- )
88- )
89- uri = "/api/{}/{}" .format (self .version , action )
90- protocol = "https" if self .ssl else "http"
91-
92- endpoint = os .environ .get ("OSC_ENDPOINT_API" )
93- if endpoint is None :
94- endpoint = "{}://{}{}" .format (protocol , host , uri )
95- else :
96- endpoint = "{}{}" .format (endpoint , uri )
76+ endpoint = self .profile .get_endpoint (service ) + "/" + action
77+ parsed_url = parse_url (endpoint )
78+ uri = parsed_url .path
79+ host = parsed_url .host
80+ print ("endpoint :" , endpoint )
81+ print ("uri: " , uri )
82+ print ("host: " , host )
9783
9884 if self .limiter is not None :
9985 self .limiter .acquire ()
10086
10187 requester = Requester (
102- Authentication (credentials , host , user_agent = self .user_agent ),
88+ self .session ,
89+ Authentication (
90+ self .profile ,
91+ host ,
92+ user_agent = self .user_agent ,
93+ ),
10394 endpoint ,
104- credentials .max_retries ,
105- credentials .retry_backoff_factor ,
106- credentials .retry_backoff_jitter ,
107- credentials .retry_backoff_max ,
10895 )
10996 if self .logger is not None :
11097 self .logger .do_log (
@@ -113,3 +100,7 @@ def api(self, action, **data):
113100 return requester .send (uri , json .dumps (data ))
114101 except Exception as err :
115102 raise err
103+
104+ def close (self ):
105+ if self .session :
106+ self .session .close ()
0 commit comments