Skip to content

Commit

Permalink
Extract repeating code in _handle_proposal() to an inner helper routine
Browse files Browse the repository at this point in the history
  • Loading branch information
azawlocki committed Apr 14, 2021
1 parent 3d75556 commit a0f92df
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions yapapi/executor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ async def _handle_proposal(
a counter-proposal or stored in an agreements pool to be used
for negotiating an agreement.
"""

async def reject_proposal(reason: str) -> events.ProposalRejected:
"""Reject `proposal` due to given `reason`."""
await proposal.reject(reason)
return events.ProposalRejected(prop_id=proposal.id, reason=reason)

score = await self._strategy.score_offer(proposal, state.agreements_pool)
logger.debug(
"Scored offer %s, provider: %s, strategy: %s, score: %f",
Expand All @@ -269,9 +275,7 @@ async def _handle_proposal(
)

if score < SCORE_NEUTRAL:
reason = "Score too low"
await proposal.reject(reason=reason)
return events.ProposalRejected(prop_id=proposal.id, reason=reason)
return await reject_proposal("Score too low")

if not proposal.is_draft:
# Proposal is not yet a draft of an agreement
Expand All @@ -284,17 +288,13 @@ async def _handle_proposal(
)
else:
# reject proposal if there are no common payment platforms
reason = "No common payment platform"
await proposal.reject(reason=reason)
return events.ProposalRejected(prop_id=proposal.id, reason=reason)
return await reject_proposal("No common payment platform")

# Check if the timeout for debit note acceptance is not too low
timeout = proposal.props.get(DEBIT_NOTE_ACCEPTANCE_TIMEOUT_PROP)
if timeout:
if timeout < DEBIT_NOTE_MIN_TIMEOUT:
reason = "Debit note acceptance timeout is too short"
await proposal.reject(reason=reason)
return events.ProposalRejected(prop_id=proposal.id, reason=reason)
return await reject_proposal("Debit note acceptance timeout is too short")
else:
state.builder.properties[DEBIT_NOTE_ACCEPTANCE_TIMEOUT_PROP] = timeout

Expand Down

0 comments on commit a0f92df

Please sign in to comment.