<commit_before>from oauth2_authorization_server import manager
import urllib2
import json
from collections import defaultdict
from oauth2app.models import *
import whitelist
from django.core.urlresolvers import reverse
import urllib2

#depricated
def getResource(resource, client, user):
	token = manager.getTokenForUser(client, user)
	if token == None: return {'error': 'no token available'}
	endpoint = manager.getEndpoint(resource, client)
	url = endpoint+'?access_token='+token
	try: response = urllib2.urlopen(endpoint+'?access_token='+token).read()
	except: response = {'error': 'connection error'}
	
	if type(response) == str: response = json.loads(response)
	return response

def userStatus(client, user):
        #available, initiated, enrolled
	response = getResource('userStatus', client, user)
	if 'error' in response: status = 'unknown'
	else: status = response['user_status']

        if status == 'unknown': status = 'available'
        return status



#depricated
def getServices(user):
	services = defaultdict(lambda: defaultdict(dict))
        clients = manager.getClients(user)
        for client in clients:
		status = userStatus(client, user)
                services[status][client.name] = {}
                services[status][client.name]['description'] = client.description
                services[status][client.name]['authorizations'] = manager.clientAuthorizations(client, user)
                services[status][client.name]['redirect_uri'] = client.redirect_uri
                services[status][client.name]['client_id'] = client.key
                services[status][client.name]['status'] = status
	
	#TODO: authorizations that the study requests from the config

	for v in services:
		services[v] = dict(services[v])

        return dict(services)

def getServices2(user):
	services = {}
	clients = manager.getClients('study')
	for client in clients:
		if not whitelist.checkWhitelist(user, client): continue
		discovery = discoverService(client)
		services[client.name] = {}
		token = manager.getTokenForUser(client=client, user=user, scope='enroll')

		if 'error' in token: 
			services[client.name]['error'] = 'unauthorized'
			services[client.name]['authorize_url'] = client.authorize_uri
			services[client.name]['discovery'] = discovery
			continue

		status = getUserStatus(client, token)
		if 'error' in status: 
			services[client.name]['error'] = 'unauthorized'
			services[client.name]['authorize_url'] = client.authorize_uri
			services[client.name]['discovery'] = discovery
			continue
		services[client.name] = status

	return services

def discoverService(client):
	url = client.api_uri + 'discover/'
	response = urllib2.urlopen(url).read()
	return json.loads(response)

def getTos(client):
	url = client.api_uri + 'tos/'
	response = urllib2.urlopen(url).read()
	return json.loads(response)

def getUserStatus(client, token):
	url = client.api_uri+'userStatus/'
	url += '?access_token='+token
	try: response = urllib2.urlopen(url).read()
	except: response = {'error': 'connection error'}
	
	if type(response) == str: response = json.loads(response)
	return response

def getAuthorizations(user, services):
	authorizations = {}
	for user_status in services:
		for service in services[user_status]:
			authorizations[service] = getAuthorizationsForService(user, services[user_status][service])


	return authorizations

def getAuthorizationsForService(user, service):
	client = Client.objects.get(key=service['client_id'])
	authorizations = getResource('serviceAuthorizations', client, user)
	return authorizations



<commit_msg>handling case when service is offline<commit_after>from oauth2_authorization_server import manager
import urllib2
import json
from collections import defaultdict
from oauth2app.models import *
import whitelist
from django.core.urlresolvers import reverse
import urllib2

#depricated
def getResource(resource, client, user):
	token = manager.getTokenForUser(client, user)
	if token == None: return {'error': 'no token available'}
	endpoint = manager.getEndpoint(resource, client)
	url = endpoint+'?access_token='+token
	try: response = urllib2.urlopen(endpoint+'?access_token='+token).read()
	except: response = {'error': 'connection error'}
	
	if type(response) == str: response = json.loads(response)
	return response

def userStatus(client, user):
        #available, initiated, enrolled
	response = getResource('userStatus', client, user)
	if 'error' in response: status = 'unknown'
	else: status = response['user_status']

        if status == 'unknown': status = 'available'
        return status



#depricated
def getServices(user):
	services = defaultdict(lambda: defaultdict(dict))
        clients = manager.getClients(user)
        for client in clients:
		status = userStatus(client, user)
                services[status][client.name] = {}
                services[status][client.name]['description'] = client.description
                services[status][client.name]['authorizations'] = manager.clientAuthorizations(client, user)
                services[status][client.name]['redirect_uri'] = client.redirect_uri
                services[status][client.name]['client_id'] = client.key
                services[status][client.name]['status'] = status
	
	#TODO: authorizations that the study requests from the config

	for v in services:
		services[v] = dict(services[v])

        return dict(services)

def getServices2(user):
	services = {}
	clients = manager.getClients('study')
	for client in clients:
		if not whitelist.checkWhitelist(user, client): continue
		discovery = discoverService(client)
		services[client.name] = {}
		token = manager.getTokenForUser(client=client, user=user, scope='enroll')

		if 'error' in token: 
			services[client.name]['error'] = 'unauthorized'
			services[client.name]['authorize_url'] = client.authorize_uri
			services[client.name]['discovery'] = discovery
			continue

		status = getUserStatus(client, token)
		if 'error' in status: 
			services[client.name]['error'] = 'unauthorized'
			services[client.name]['authorize_url'] = client.authorize_uri
			services[client.name]['discovery'] = discovery
			continue
		services[client.name] = status

	return services

def discoverService(client):
	#TODO set service to 'offline' if there is an error
	url = client.api_uri + 'discover/'
	try:response = urllib2.urlopen(url).read()
	except urllib2.HTTPError: return {'error':'connection refused'}
	except urllib2.URLError: return {'error':'connection refused'}
	return json.loads(response)

def getTos(client):
	url = client.api_uri + 'tos/'
	response = urllib2.urlopen(url).read()
	return json.loads(response)

def getUserStatus(client, token):
	url = client.api_uri+'userStatus/'
	url += '?access_token='+token
	try: response = urllib2.urlopen(url).read()
	except: response = {'error': 'connection error'}
	
	if type(response) == str: response = json.loads(response)
	return response

def getAuthorizations(user, services):
	authorizations = {}
	for user_status in services:
		for service in services[user_status]:
			authorizations[service] = getAuthorizationsForService(user, services[user_status][service])


	return authorizations

def getAuthorizationsForService(user, service):
	client = Client.objects.get(key=service['client_id'])
	authorizations = getResource('serviceAuthorizations', client, user)
	return authorizations




{'fraction_non_alphanumeric': 0.12014134275618374, 'fraction_numerical': 0.002718129926610492, 'mean_word_length': 4.8637450199203185, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6269119', 'n_tokens_mistral': 2231, 'n_tokens_neox': 2118, 'n_words': 519}