-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathdomain.py
More file actions
146 lines (122 loc) · 5.8 KB
/
domain.py
File metadata and controls
146 lines (122 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -*- coding: utf-8 -*-
#
# Copyright 2014-2025 BigML
#
# Licensed 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.
"""Domain class to handle domain assignation for VPCs
"""
import os
# Default domain and protocol
DEFAULT_DOMAIN = 'bigml.io'
DEFAULT_PROTOCOL = 'https'
DEFAULT_API_VERSION = 'andromeda'
# Base Domain
BIGML_DOMAIN = os.environ.get('BIGML_DOMAIN', DEFAULT_DOMAIN)
# Default API version
BIGML_API_VERSION = os.environ.get('BIGML_API_VERSION', DEFAULT_API_VERSION)
# Protocol for main server
BIGML_PROTOCOL = os.environ.get('BIGML_PROTOCOL',
DEFAULT_PROTOCOL)
# SSL Verification
BIGML_SSL_VERIFY = os.environ.get('BIGML_SSL_VERIFY')
# Domain for prediction server
BIGML_PREDICTION_DOMAIN = os.environ.get('BIGML_PREDICTION_DOMAIN',
BIGML_DOMAIN)
# Protocol for prediction server
BIGML_PREDICTION_PROTOCOL = os.environ.get('BIGML_PREDICTION_PROTOCOL',
DEFAULT_PROTOCOL)
# SSL Verification for prediction server
BIGML_PREDICTION_SSL_VERIFY = os.environ.get('BIGML_PREDICTION_SSL_VERIFY')
class Domain():
"""A Domain object to store the remote domain information for the API
The domain that serves the remote resources can be set globally for
all the resources either by setting the BIGML_DOMAIN environment
variable
export BIGML_DOMAIN=my_VPC.bigml.io
or can be given in the constructor using the `domain` argument.
my_domain = Domain("my_VPC.bigml.io")
You can also specify a separate domain to handle predictions. This can
be set by using the BIGML_PREDICTION_DOMAIN and
BIGML_PREDICTION_PROTOCOL
environment variables
export BIGML_PREDICTION_DOMAIN=my_prediction_server.bigml.com
export BIGML_PREDICITION_PROTOCOL=https
or the `prediction_server` and `prediction_protocol` arguments.
The constructor values will override the environment settings.
"""
def __init__(self, domain=None, prediction_domain=None,
prediction_protocol=None, protocol=None, verify=None,
prediction_verify=None, api_version=None):
"""Domain object constructor.
@param: domain string Domain name
@param: prediction_domain string Domain for the prediction server
(when different from the general domain)
@param: prediction_protocol string Protocol for prediction server
(when different from the general protocol)
@param: protocol string Protocol for the service
(when different from HTTPS)
@param: verify boolean Sets on/off the SSL verification
@param: prediction_verify boolean Sets on/off the SSL verification
for the prediction server (when different from the general
SSL verification)
@param: api_version string Name of the API version
"""
# Base domain for remote resources
self.general_domain = domain if domain is not None else BIGML_DOMAIN
self.general_protocol = protocol if protocol is not None else \
BIGML_PROTOCOL
self.api_version = api_version if api_version is not None else \
BIGML_API_VERSION
# Usually, predictions are served from the same domain
if prediction_domain is None:
if domain is not None:
self.prediction_domain = domain
self.prediction_protocol = protocol if protocol is not None \
else BIGML_PROTOCOL
else:
self.prediction_domain = BIGML_PREDICTION_DOMAIN
self.prediction_protocol = BIGML_PREDICTION_PROTOCOL
# If the domain for predictions is different from the general domain,
# for instance in high-availability prediction servers
else:
self.prediction_domain = prediction_domain
self.prediction_protocol = prediction_protocol if \
prediction_protocol is not None else \
BIGML_PREDICTION_PROTOCOL
# Check SSL when comming from `bigml.io` subdomains or when forced
# by the external BIGML_SSL_VERIFY environment variable or verify
# arguments
self.verify = None
self.verify_prediction = None
if self.general_protocol == BIGML_PROTOCOL and \
(verify is not None or BIGML_SSL_VERIFY is not None):
try:
self.verify = verify if verify is not None \
else bool(int(BIGML_SSL_VERIFY))
except ValueError:
pass
if self.verify is None:
self.verify = self.general_domain.lower().endswith(DEFAULT_DOMAIN)
if self.prediction_protocol == BIGML_PROTOCOL and \
(prediction_verify is not None or \
BIGML_PREDICTION_SSL_VERIFY is not None):
try:
self.verify_prediction = prediction_verify \
if prediction_verify is not None else \
bool(int(BIGML_PREDICTION_SSL_VERIFY))
except ValueError:
pass
if self.verify_prediction is None:
self.verify_prediction = (
(self.prediction_domain.lower().endswith(DEFAULT_DOMAIN) and
self.prediction_protocol == DEFAULT_PROTOCOL))