GithubHelp home page GithubHelp logo

zhiyuli / hs_restclient Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hydroshare/hs_restclient

0.0 2.0 0.0 325 KB

Python client for the https://www.hydroshare.org REST API

Python 99.65% Shell 0.35%

hs_restclient's Introduction

Python-based client library for HydroShare REST API

Questions? [email protected]

To run tests

To run unit tests:

cd tests
python test_hs_restclient.py

To use

To get a listing of public resources:

from hs_restclient import HydroShare
hs = HydroShare()
for resource in hs.getResourceList():
    print(resource)

To authenticate using HTTP Basic authentication, and then get a list of resources you have access to:

from hs_restclient import HydroShare, HydroShareAuthBasic
auth = HydroShareAuthBasic(username='myusername', password='mypassword')
hs = HydroShare(auth=auth)
for resource in hs.getResourceList():
    print(resource)

To authenticate using OAuth2 authentication (using a user and password supplied by the user), and then get a list of resources you have access to:

from oauthlib.oauth2 import TokenExpiredError
from hs_restclient import HydroShare, HydroShareAuthOAuth2

# Get a client ID and client secret by registering a new application at:
# https://www.hydroshare.org/o/applications/
# Choose client type "Confidential" and authorization grant type "Resource owner password-based"
# Keep these secret!
client_id = 'MYCLIENTID'
client_secret = 'MYCLIENTSECRET'

auth = HydroShareAuthOAuth2(client_id, client_secret,
                            username='myusername', password='mypassword')
hs = HydroShare(auth=auth)

try:
    for resource in hs.getResourceList():
        print(resource)
except TokenExpiredError as e:
    hs = HydroShare(auth=auth)
    for resource in hs.getResourceList():
        print(resource)

Note that currently the client does not handle token renewal, hence the need to catch TokenExpiredError.

To authenticate using OAuth2 authentication (using an existing token), and then get a list of resources you have access to:

from oauthlib.oauth2 import TokenExpiredError
from hs_restclient import HydroShare, HydroShareAuthOAuth2

# Get a client ID and client secret by registering a new application at:
# https://www.hydroshare.org/o/applications/
# Choose client type "Confidential" and authorization grant type "Resource owner password-based"
# Keep these secret!
client_id = 'MYCLIENTID'
client_secret = 'MYCLIENTSECRET'

# A token dictionary obtained separately from HydroShare of the form:
#   {
#       "access_token": "<your_access_token>",
#       "token_type": "Bearer",
#       "expires_in": 36000,
#       "refresh_token": "<your_refresh_token>",
#       "scope": "read write groups"
#   }
# get_token() is a stand in for how you get a new token on your system.
token = get_token()

auth = HydroShareAuthOAuth2(client_id, client_secret,
                            token=token)
try:
    hs = HydroShare(auth=auth)
    for resource in hs.getResourceList():
        print(resource)
except:
    # get_token() is a stand in for how you get a new token on your system.
    token = get_token()
    hs = HydroShare(auth=auth)
    for resource in hs.getResourceList():
        print(resource)

Note that currently the client does not handle token renewal, hence the need to catch TokenExpiredError.

To connect to a development HydroShare server that uses a self-sign security certificate:

from hs_restclient import HydroShare
hs = HydroShare(hostname='mydev.mydomain.net', verify=False)
for resource in hs.getResourceList():
    print(resource)

To connect to a development HydroShare server that is not running HTTPS:

from hs_restclient import HydroShare
hs = HydroShare(hostname='mydev.mydomain.net', port=8000, use_https=False)
for resource in hs.getResourceList():
    print(resource)

Note that authenticated connections must use HTTPS.

For more usage options see the documentation.

Documentation

Complete installation and usage documentation is available at http://hs-restclient.readthedocs.org/en/latest/

hs_restclient's People

Contributors

selimnairb avatar pkdash avatar aphelionz avatar sblack-usu avatar shawncrawley avatar mt-digital avatar mjstealey avatar valentinedwv avatar castronova avatar

Watchers

James Cloos avatar Zhiyu(Drew) Li avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.