I am pretty new to Python and just wanted to make sure I am going in the right direction with my coding style.
After reading this, I'm not so sure.
The following is a convenience class that has operations used a lot in my main code.
import requests
try: import simplejson as json
except ImportError: import json
"""
Basic operations involving users
"""
#The URL of the API
api_root = "http://api.stuff.com/v1/core";
class UserOperations(object):
def __init__(self, headers):
self.headers = headers
def move_user(self,source_account,destination_account, user_id):
user_id = str(user_id)
#Get user
user = self.get_user(source_account, user_id)
if user is not None:
#Remove user from source account
self.remove_user(source_account, user_id)
#Add user to destination account
self.add_user(destination_account, user)
def get_user(self, account_id, user_id):
#Get user
user = requests.get(api_root + "/accounts/" + account_id + "/users/" + user_id, headers=self.headers)
#Throw exception if non-200 response
user.raise_for_status()
print "\nGet User " + user_id + ": " + user.text
#Check user exists
if user.json()['Data'] is None:
return None
#Return user
return user.json()['Data']
def remove_user(self,account_id, user_id):
#Delete a user
result = requests.delete(api_root + "/accounts/" + account_id + "/users/" + user_id, headers=self.headers)
#Throw exception if non-200 response
result.raise_for_status()
#Result of delete operation
print "\nDelete Result " + result.text
def add_user(self,account_id, user):
#Add user to new account
result = requests.post(api_root + '/accounts/' + account_id + '/users', data=json.dumps(user), headers=self.headers)
#Throw exception if non-200 response
result.raise_for_status()
#Result of add operation
print "\nAdd User: " + result.text
Does this seem on the right track or are there any Python mantra's I'm missing out on.
Edit
Function to replace below code duplication
variable = requests.get(api_root + "/accounts/"....)
variable.raise_for_status()
Function:
def request(self, request, account_id, user):
if request is 'get':
#Get user
result = requests.get(api_root + "/accounts/" + account_id + "/users/" + user_id, headers=self.headers)
elif request is 'post':
#Add user to new account
result = requests.post(api_root + '/accounts/' + account_id + '/users', data=json.dumps(user), headers=self.headers)
elif request is 'delete':
#Delete user from account
result = requests.delete(api_root + "/accounts/" + account_id + "/users/" + user_id, headers=self.headers)
#Throw exception if non-200 response
result.raise_for_status()
return result
Edit Function attempt 2:
def request(self, request, account_id, user):
function_dict = {}
function_dict['get'] = requests.get(api_root + "/accounts/" + account_id + "/users/" + user, headers=self.headers)
function_dict['post'] = requests.post(api_root + '/accounts/' + account_id + '/users', data=json.dumps(user), headers=self.headers)
function_dict['delete'] = requests.delete(api_root + "/accounts/" + account_id + "/users/" + user, headers=self.headers)
result = function_dict.get(request)
#Throw exception if non-200 response
result.raise_for_status()
return result