Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for state oauth parameter #18

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ fenixedu.ini

# Files created to test api
test*.py

# Virtual environment
env/
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: python

install:
- pip install .
- pip install -r requirements.txt
# command to run tests

# command to run tests
script: nosetests
20 changes: 20 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2015 Samuel Coelho

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
62 changes: 46 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
fenix_python_sdk
fenix_python_sdk <img src="https://travis-ci.org/samfcmc/fenixedu-python-sdk.svg?branch=master">
================

## Installation
<code>pip install fenixedu</code>
```
$ pip install fenixedu
```

## Usage

### Instantiating the client

* Import python sdk

<code>import fenixedu</code>
```
import fenixedu
```

#### Instantiating a configuration object

Expand All @@ -20,63 +24,89 @@ fenix_python_sdk

* Copy file fenixedu.sample.ini to a new one named 'fenixedu.ini' or with another name if you want

<code>cp fenixedu.sample.ini FILENAME</code>
```
$ cp fenixedu.sample.ini FILENAME
```

* Edit the file according to your application info

* Instantiate a configuration object using the file

<code>config = fenixedu.FenixEduConfiguration.fromConfigFile('FILENAME')</code>
```python
config = fenixedu.FenixEduConfiguration.fromConfigFile('FILENAME')
```

* If no FILENAME is provided it will use 'fenixedu.ini'

##### Without a configuration file

<code>config = fenixedu.FenixEduConfiguration('CLIENT_ID, 'REDIRECT_URI', 'CLIENT_SECRET', 'BASE_URL')</code>
```python
config = fenixedu.FenixEduConfiguration('CLIENT_ID', 'REDIRECT_URI', 'CLIENT_SECRET', 'BASE_URL')
```

#### Instantiating the client
* Instantiate an API client object in your source code

<code>client = fenixedu.FenixEduClient(config)</code>
```python
client = fenixedu.FenixEduClient(config)
```

### Authentication

* Get the authentication URL

<code>url = client.get_authentication_url()</code>
```python
url = client.get_authentication_url()
```

* Redirect your user to that URL

* If the user authorizes your application he will be redirected to an URL like this:

<code>redirect_uri?code=CODE</code>
```
redirect_uri?code=CODE
```

* Get the code parameter in URL and get an object with the user details:

<code>user = client.get_user_by_code(CODE)</code>
```python
user = client.get_user_by_code('CODE')
```

* It will request an access token and returns no errors if everything is fine

* This user object now can be used to make requests that belong to the private scope like this one:

<code>person = client.get_person(user)</code>
```python
person = client.get_person(user)
```

### Examples of usage

#### Get degrees
<code>degrees = client.get_degrees()</code>
```python
degrees = client.get_degrees()
```

#### Get spaces
<code>spaces = client.get_spaces()</code>
```python
spaces = client.get_spaces()
```

#### Get information about the user
<code>person = client.get_person(user)</code>
```python
person = client.get_person(user)
```

#### Get user's classes calendar
<code>classes = client.get_person_calendar_classes(user)</code>
```python
classes = client.get_person_calendar_classes(user)
```

#### Get user's payments
<code>payments = client.get_person_payments(user)</code>
```python
payments = client.get_person_payments(user)
```

### Full endpoint list

Expand Down
10 changes: 7 additions & 3 deletions fenixedu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def _api_private_request(self, endpoint, user, params=None, method=None, headers
if r.status_code == 401:
self._refresh_access_token(user)
""" Repeat the request """
params['access_token'] = user.access_token
r = self._request(url, params = params, method = method, headers = headers)
return r

Expand All @@ -74,16 +75,19 @@ def _refresh_access_token(self, user):
r_headers = {'content-type' : 'application/x-www-form-urlencoded'}
r = self._request(url, params = req_params, method = Requests.POST, headers = r_headers)
refresh = r.json()
user.access_token = refresh['access_token']
user.token_expires = refresh['expires_in']
if "access_token" in refresh:
user.access_token = refresh['access_token']
user.token_expires = refresh['expires_in']

def _api_public_request(self, endpoint, params=None, method=None, headers=None, endpoint_params=None):
url = self._get_api_endpoint_url(endpoint, endpoint_params)
return self._request(url, params, method, headers = headers)

def get_authentication_url(self):
def get_authentication_url(self, state=None):
auth_url = self._get_oauth_endpoint_url('userdialog')
params = {'client_id': self.config.client_id, 'redirect_uri': self.config.redirect_uri}
if state:
params['state'] = state

return self._add_parameters_to_url(auth_url, params)

Expand Down
4 changes: 0 additions & 4 deletions reqs.txt

This file was deleted.

2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
configparser==3.3.0.post2
requests==2.5.3
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from distutils.core import setup, Extension

setup(name='fenixedu',
version='1.0.0',
version='1.0.1',
description='FenixEdu API SDK for python',
author='Samuel Coelho',
author_email='[email protected]',
Expand Down