|
37 | 37 | DEFAULT_SCHEME = 'http+unix://' |
38 | 38 |
|
39 | 39 |
|
| 40 | +class PodmanAPIError(Exception): |
| 41 | + pass |
| 42 | + |
| 43 | + |
40 | 44 | # The following was adapted from some code from docker-py |
41 | 45 | # https://github.com/docker/docker-py/blob/master/docker/transport/unixconn.py |
42 | 46 | class UnixHTTPConnection(httplib.HTTPConnection, object): |
@@ -80,7 +84,7 @@ def _new_conn(self): |
80 | 84 | if HAS_REQUESTS: |
81 | 85 | class UnixAdapter(HTTPAdapter): |
82 | 86 |
|
83 | | - def __init__(self, timeout=60, pool_connections=25, *args, **kwargs): |
| 87 | + def __init__(self, *args, timeout=60, pool_connections=25, **kwargs): |
84 | 88 | super(UnixAdapter, self).__init__(*args, **kwargs) |
85 | 89 | self.timeout = timeout |
86 | 90 | self.pools = urllib3._collections.RecentlyUsedContainer( |
@@ -114,16 +118,18 @@ def close(self): |
114 | 118 |
|
115 | 119 | if HAS_REQUESTS: |
116 | 120 | class APISession(requests.Session): |
117 | | - def __init__(self, url_scheme=DEFAULT_SCHEME, *args, **kwargs): |
| 121 | + def __init__(self, *args, url_scheme=DEFAULT_SCHEME, **kwargs): |
118 | 122 | super(APISession, self).__init__(*args, **kwargs) |
119 | 123 | self.mount(url_scheme, UnixAdapter()) |
120 | 124 |
|
121 | 125 |
|
122 | 126 | class PodmanAPIHTTP: |
123 | | - def __init__(self, base_url): |
124 | | - self.api_url = "".join((DEFAULT_SCHEME, |
| 127 | + def __init__(self, base_url, scheme=DEFAULT_SCHEME): |
| 128 | + self.api_url = "".join((scheme, |
125 | 129 | quote(base_url, safe=""), |
126 | 130 | "/v2.0.0/libpod")) |
| 131 | + if scheme == "http://": |
| 132 | + self.api_url = "".join((scheme, base_url, "/v2.0.0/libpod")) |
127 | 133 | self.session = APISession() |
128 | 134 |
|
129 | 135 | def request(self, method, url, **kwargs): |
@@ -157,14 +163,17 @@ def options(self, url, **kwargs): |
157 | 163 | class PodmanAPIClient: |
158 | 164 | def __init__(self, base_url): |
159 | 165 | if not HAS_REQUESTS: |
160 | | - raise Exception("requests package is required for podman API") |
| 166 | + raise PodmanAPIError("requests package is required for podman API") |
161 | 167 | socket_opt = urlparse(base_url) |
162 | | - if socket_opt.scheme != "unix": |
163 | | - raise Exception("Scheme %s is not supported! Use %s" % ( |
| 168 | + if socket_opt.scheme not in ("unix", "http"): |
| 169 | + raise PodmanAPIError("Scheme %s is not supported! Use %s" % ( |
164 | 170 | socket_opt.scheme, |
165 | 171 | DEFAULT_SCHEME |
166 | 172 | )) |
167 | | - self.api = PodmanAPIHTTP(socket_opt.path) |
| 173 | + if socket_opt.scheme == "http": |
| 174 | + self.api = PodmanAPIHTTP(socket_opt.netloc, "http://") |
| 175 | + else: |
| 176 | + self.api = PodmanAPIHTTP(socket_opt.path) |
168 | 177 | self.containers = PodmanAPIContainers(self.api) |
169 | 178 | self.images = PodmanAPIImages(api=self.api) |
170 | 179 |
|
@@ -204,8 +213,8 @@ def create(self, **container_data): |
204 | 213 | ) |
205 | 214 | if response.ok: |
206 | 215 | return response.json() |
207 | | - raise Exception("Container %s failed to create! Error: %s" % |
208 | | - (container_data.get('name'), response.text)) |
| 216 | + raise PodmanAPIError("Container %s failed to create! Error: %s" % |
| 217 | + (container_data.get('name'), response.text)) |
209 | 218 |
|
210 | 219 | def get(self, name): |
211 | 220 | response = self.api.get( |
|
0 commit comments