Skip to content

Commit

Permalink
Added OpenID authentication and check_response helper
Browse files Browse the repository at this point in the history
  • Loading branch information
nacx committed Nov 30, 2017
1 parent de394ff commit 945bf06
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ for dc in datacenters:

Note that you don't need to care about pagination, the client handles it internally for you.

### Using an OpenID Bearer access token

If your platform uses OpenID and you have a Bearer access token, you can configure the client
as follows:

```python
import json
from abiquo.client import Abiquo
from abiquo.auth import BearerTokenAuth

api = Abiquo(API_URL, auth=BearerTokenAuth(token))
```

### Using OAuth

To use OAuth first you have to register your client application in the Abiquo API. To do that, you can
Expand Down
23 changes: 23 additions & 0 deletions abiquo/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (C) 2008 Abiquo Holdings S.L.
#
# 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.

from requests.auth import AuthBase

class BearerTokenAuth(AuthBase):
def __init__(self, token):
self.token = token

def __call__(self, request):
request.headers['Authorization'] = "Bearer %s" % self.token
return request
11 changes: 11 additions & 0 deletions abiquo/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,14 @@ def _extract_link(self, rel):

def _has_link(self, rel):
return True if self._extract_link(rel) else False


def check_response(expected_code, code, errors):
if code != expected_code:
try:
first_error = errors.json['collection'][0]
except:
# If it is not an Abiquo controlled error, throw a generic error
raise Exception("HTTP(%s) Operation failed!" % code)
# If it is an Abiquo error, properly show the error code and error details
raise Exception("HTTP(%s) %s: %s" % (code, first_error['code'], first_error['message']))

0 comments on commit 945bf06

Please sign in to comment.