Skip to content

Instantly share code, notes, and snippets.

@nurav
Created August 15, 2017 10:11
Show Gist options
  • Save nurav/471e8e6c0fa62180a0a306ac8fad4688 to your computer and use it in GitHub Desktop.
Save nurav/471e8e6c0fa62180a0a306ac8fad4688 to your computer and use it in GitHub Desktop.
import json
import requests
import os
from cern_webinfra_service_engine.message_processor import MessageProcessor
from cern_webinfra_service_engine.iresource import IResource
URSH_LOCATION = 'http://test-ursh.web.cern.ch'
URSH_TOKEN = ''
class URL(IResource):
def post(self, parameters):
shortcut = parameters.get('shortcut')
data = json.loads(parameters['data'])
auth_header = {'Authorization': 'Bearer {}'.format(os.environ.get('URSH_TOKEN'))}
response = None
if shortcut:
response = requests.put('{}/urls/{}'.format(URSH_LOCATION, shortcut), data=data, headers=auth_header)
else:
response = requests.post('{}/urls'.format(URSH_LOCATION), data=data, headers=auth_header)
def put(self, parameters):
shortcut = parameters.get('shortcut')
data = json.loads(parameters['data'])
auth_header = {'Authorization': 'Bearer {}'.format(os.environ.get('URSH_TOKEN'))}
response = None
if shortcut:
response = requests.patch('{}/urls/{}'.format(URSH_LOCATION, shortcut), data=data, headers=auth_header)
def delete(self, parameters):
shortcut = parameters.get('shortcut')
data = json.loads(parameters['data'])
auth_header = {'Authorization': 'Bearer {}'.format(os.environ.get('URSH_TOKEN'))}
response = None
if shortcut:
response = requests.delete('{}/urls/{}'.format(URSH_LOCATION, shortcut), headers=auth_header)
class Token(IResource):
def post(self, parameters):
data = json.loads(parameters['data'])
auth_header = {'Authorization': 'Bearer {}'.format(os.environ.get('URSH_TOKEN'))}
response = requests.post('{}/tokens'.format(URSH_LOCATION), data=data, headers=auth_header)
def put(self, parameters):
data = json.loads(parameters['data'])
api_key = data['api_key']
auth_header = {'Authorization': 'Bearer {}'.format(os.environ.get('URSH_TOKEN'))}
response = requests.patch('{}/tokens/{}'.format(URSH_LOCATION, api_key), data=data, headers=auth_header)
def delete(self, parameters):
data = json.loads(parameters['data'])
api_key = data['api_key']
auth_header = {'Authorization': 'Bearer {}'.format(os.environ.get('URSH_TOKEN'))}
response = requests.patch('{}/tokens/{}'.format(URSH_LOCATION, api_key), headers=auth_header)
class Resources(MessageProcessor):
def __init__(self, conn):
super(Resources, self).__init__(conn)
self.add_resource(Token, [
'ursh/tokens',
])
self.add_resource(URL, [
'ursh/urls',
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment