Skip to content

Commit

Permalink
josuebrunel#37 : test_add_player successful
Browse files Browse the repository at this point in the history
  • Loading branch information
peacing committed Jul 28, 2015
1 parent 6b7cd9d commit 2635573
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 34 deletions.
46 changes: 35 additions & 11 deletions fantasy_sport/fantasy_sport.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import absolute_import
from fantasy_sport.roster import Player, Roster, Transaction


class FantasySport(object):
Expand Down Expand Up @@ -43,14 +44,28 @@ def _put(self, uri, roster):
- uri : roster resource uri
- roster : roster object
"""
#headers = {'Content-Type':'application/{0}'.self.fmt}
headers = {'Content-Type':'application/{0}'.format(self.fmt)}
data = roster.to_json() if self.fmt == 'json' else roster.to_xml() # Getting roster xml or json according to self.fmt

response = self.oauth.session.put(uri, data=data, headers=headers)
print response.status_code
print response.reason


return response

def _post(self, uri, transaction):
"""
- uri : roster resource uri
- transaction : roster object
"""
headers = {'Content-Type':'application/xml'}
data = transaction.to_xml()

response = self.oauth.session.post(uri, data=data, headers=headers)
print response.status_code
print response.reason

return response

def _add_login(self, uri):
"""Add users;use_login=1/ to the uri
Expand All @@ -72,7 +87,7 @@ def _build_uri(self, resources, keys, sub=None):
else:
uri = '{0}'.format(self._format_resources_key(keys))

if sub and isinstance(sub, str) :
if sub and isinstance(sub, str):
uri += "/{0}".format(sub)
if sub and not isinstance(sub, str):
uri += ";out={0}".format(','.join([e for e in sub]))
Expand Down Expand Up @@ -401,7 +416,7 @@ def get_all_completed_leagues_transactions(self, league_keys):
response = self._get(uri)
return response

def get_teams_pending_transactions(self, league_key, team_key)
def get_teams_pending_transactions(self, league_key, team_key):
"""Return pending transactions for a specific team
>>> yfs.get_teams_pending_transactions('league_key', 'team_key')
"""
Expand All @@ -424,6 +439,7 @@ def edit_waivers(self, roster):
uri = self._build_uri(None, None, sub='transaction')

response = self._put(uri, roster)
return response

def trade_response(self, roster):
"""
Expand All @@ -435,6 +451,7 @@ def trade_response(self, roster):
uri = self._build_uri(None, None, sub='transaction')

response = self._put(uri, roster)
return response

def commissioner_trade_response(self, roster):
"""
Expand All @@ -446,6 +463,7 @@ def commissioner_trade_response(self, roster):
uri = self._build_uri(None, None, sub='transaction')

response = self._put(uri, roster)
return response

def vote_against_trade(self, roster):
"""
Expand All @@ -457,10 +475,11 @@ def vote_against_trade(self, roster):
uri = self._build_uri(None, None, sub='transaction')

response = self._put(uri, roster)
return response

### POST Functions

def make_roster_move(self, league_keys, roster)
def make_roster_move(self, league_keys, roster):
"""
Add, drop, or add & drop a player
>>> from fantasy_sport import Transaction
Expand All @@ -469,18 +488,23 @@ def make_roster_move(self, league_keys, roster)
"""
uri = self._build_uri(None, league_keys, sub='roster')
uri = 'league/{0}'.format(uri)
return response

def add_player(self, player_key, team_key)
def add_player(self, player_key, team_key, league_key):
"""
Add a player to your team
Only two things to specify are player key and team key
yfs.add_player('346.p.9171', '346.l.1328.t.12')
Three things to specify are player key, team key, league key
yfs.add_player('346.p.9171', '346.l.1328.t.12', ['346.l.1328'])
"""
uri = self._build_uri(None, league_key, sub='transactions')
uri = 'league/{0}'.format(uri)

self.player_key = player_key
self.team_key = team_key
print uri

roster = Transaction(type='add', player_key=self.player_key, team_key=self.team_key)
transaction = Transaction('add', player_key, team_key)

response = self._post(uri, transaction)
return response



43 changes: 20 additions & 23 deletions fantasy_sport/roster.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Base(object):
"""

@abc.abstractmethod
def xml_builder(self,):
def xml_builder_add(self,):
raise NotImplementedError

@abc.abstractmethod
Expand All @@ -28,7 +28,7 @@ def to_xml(self,):
"""Return object as a xml string
"""
return ctree.tostring(self.xml)

"""
@abc.abstractmethod
def xml_builder_waiver(self,):
raise NotImplementedError
Expand All @@ -52,7 +52,7 @@ def xml_builder_adddrop(self,):
@abc.abstractmethod
def xml_builder_pending_trade_post(self,):
raise NotImplementedError

"""

class Roster(Base):
"""Roster class
Expand Down Expand Up @@ -159,10 +159,8 @@ class Transaction(Base):
- action
"""

def __init__(self, type, transaction_key=None, waiver_priority=None, faab_bid=None,
action=None, trade_note=None, voter_team_key=None
player_key=None, team_key=None,
):
#def __init__(self, type, transaction_key=None, waiver_priority=None, faab_bid=None, action=None, trade_note=None, voter_team_key=None, player_key=None, team_key=None):
def __init__(self, type=None, player_key=None, team_key=None,):
"""Initialize a Transaction object
- types
-- PUT: waiver, pending_trade
Expand All @@ -172,25 +170,24 @@ def __init__(self, type, transaction_key=None, waiver_priority=None, faab_bid=No
super(Base, self).__init__()

self.type = type
self.transaction_key = transaction_key
self.waiver_priority = waiver_priority
self.faab_bid = faab_bid
self.action = action
self.trade_note = trade_note
self.voter_team_key = voter_team_key
#self.transaction_key = transaction_key
#self.waiver_priority = waiver_priority
#self.faab_bid = faab_bid
#self.action = action
#self.trade_note = trade_note
#self.voter_team_key = voter_team_key
self.player_key = player_key
self.team_key = team_key

types = {'waiver': xml_builder_waiver, 'pending_trade': xml_builder_pending_trade_put,
'add': xml_builder_add, 'drop': xml_builder_drop, 'add/drop': xml_builder_adddrop
}
#types = {'waiver': xml_builder_waiver, 'pending_trade': xml_builder_pending_trade_put, 'add': xml_builder_add, 'drop': xml_builder_drop, 'add/drop': xml_builder_adddrop}

if self.type in types:
types[self.type](
else:
raise Exception("Method %s not implemented" % self.type

self.xml_builder()
#if self.type in types:
#types[self.type]()
#else:
#raise Exception("Method %s not implemented" % self.type

self.xml_builder_add()
#self.xml_builder()
#self.json_builder()

def xml_builder_add(self,):
Expand All @@ -203,7 +200,7 @@ def xml_builder_add(self,):
type = ctree.SubElement(transaction, 'type')
type.text = self.type

player = ctreeSubElement(transaction, 'player')
player = ctree.SubElement(transaction, 'player')

player_key = ctree.SubElement(player, 'player_key')
player_key.text = self.player_key
Expand Down
6 changes: 6 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ def test_get_all_completed_leagues_transactions(self,):
logging.debug(pretty_json(response.content))
self.assertEqual(response.status_code, 200)

def test_add_player(self,):
response = self.yfs.add_player('346.p.9723', '346.l.1328.t.12', ['346.l.1328'])
logging.debug(pretty_xml(response.content))
self.assertEqual(response.status_code, 200)


class TestFantasySportRoster(unittest.TestCase):

def setUp(self,):
Expand Down

0 comments on commit 2635573

Please sign in to comment.