Ejemplo n.º 1
0
 def _delete_offer(self, payload):
     transaction_id = self._get_transaction_id(payload)
     delete_offer_response_channel = f'{self.channel_prefix}/response/delete_offer'
     if not check_for_connected_and_reply(
             self.redis, delete_offer_response_channel, self.connected):
         return
     try:
         arguments = json.loads(payload["data"])
         if ("offer" in arguments and arguments["offer"] is not None) and \
                 not self.offers.is_offer_posted(self.market.id, arguments["offer"]):
             raise Exception(
                 "Offer_id is not associated with any posted offer.")
     except Exception as e:
         logging.error(
             f"Error when handling delete offer request. Payload {payload}. "
             f"Exception {str(e)}.")
         self.redis.publish_json(
             delete_offer_response_channel, {
                 "command": "offer_delete",
                 "error":
                 "Incorrect delete offer request. Available parameters: (offer).",
                 "transaction_id": transaction_id
             })
     else:
         self.pending_requests.append(
             IncomingRequest("delete_offer", arguments,
                             delete_offer_response_channel))
Ejemplo n.º 2
0
 def _delete_bid(self, payload):
     transaction_id = self._get_transaction_id(payload)
     delete_bid_response_channel = f'{self.channel_prefix}/response/delete_bid'
     if not check_for_connected_and_reply(
             self.redis, delete_bid_response_channel, self.connected):
         return
     try:
         arguments = json.loads(payload["data"])
         if ("bid" in arguments and arguments["bid"] is not None) and \
                 not self.is_bid_posted(self.market, arguments["bid"]):
             raise Exception(
                 "Bid_id is not associated with any posted bid.")
     except Exception as e:
         self.redis.publish_json(
             delete_bid_response_channel, {
                 "command":
                 "bid_delete",
                 "error":
                 f"Incorrect delete bid request. Available parameters: (bid)."
                 f"Exception: {str(e)}",
                 "transaction_id":
                 transaction_id
             })
     else:
         self.pending_requests.append(
             IncomingRequest("delete_bid", arguments,
                             delete_bid_response_channel))
Ejemplo n.º 3
0
    def bid(self, payload):
        transaction_id = self._get_transaction_id(payload)
        required_args = {"price", "energy", "transaction_id"}
        allowed_args = required_args.union(
            {"replace_existing", "attributes", "requirements"})

        bid_response_channel = f"{self.channel_prefix}/response/bid"
        if not check_for_connected_and_reply(self.redis, bid_response_channel,
                                             self.connected):
            return
        try:
            arguments = json.loads(payload["data"])

            # Check that all required arguments have been provided
            assert all(arg in arguments.keys() for arg in required_args)
            # Check that every provided argument is allowed
            assert all(arg in allowed_args for arg in arguments.keys())

        except Exception:
            self.redis.publish_json(
                bid_response_channel, {
                    "command":
                    "bid",
                    "error": ("Incorrect bid request. ",
                              f"Required parameters: {required_args}"
                              f"Available parameters: {allowed_args}."),
                    "transaction_id":
                    transaction_id
                })
        else:
            self.pending_requests.append(
                IncomingRequest("bid", arguments, bid_response_channel))
Ejemplo n.º 4
0
 def _offer(self, payload):
     transaction_id = self._get_transaction_id(payload)
     offer_response_channel = f'{self.channel_prefix}/response/offer'
     if not check_for_connected_and_reply(
             self.redis, offer_response_channel, self.connected):
         return
     try:
         arguments = json.loads(payload["data"])
         assert set(
             arguments.keys()) == {'price', 'energy', 'transaction_id'}
         arguments['seller'] = self.device.name
         arguments['seller_origin'] = self.device.name
     except Exception as e:
         logging.error(
             f"Incorrect offer request. Payload {payload}. Exception {str(e)}."
         )
         self.redis.publish_json(
             offer_response_channel, {
                 "command": "offer",
                 "error":
                 "Incorrect offer request. Available parameters: (price, energy).",
                 "transaction_id": transaction_id
             })
     else:
         self.pending_requests.append(
             IncomingRequest("offer", arguments, offer_response_channel))
Ejemplo n.º 5
0
 def _list_offers(self, payload):
     self._get_transaction_id(payload)
     list_offers_response_channel = f'{self.channel_prefix}/response/list_offers'
     if not check_for_connected_and_reply(self.redis, list_offers_response_channel,
                                          self.connected):
         return
     arguments = json.loads(payload["data"])
     self.pending_requests.append(
         IncomingRequest("list_offers", arguments, list_offers_response_channel))
Ejemplo n.º 6
0
 def test_delete_offer_successful(self, ext_strategy_fixture):
     transaction_id = str(uuid.uuid4())
     arguments = {"transaction_id": transaction_id}
     payload = {"data": json.dumps(arguments)}
     assert ext_strategy_fixture.pending_requests == deque([])
     ext_strategy_fixture.delete_offer(payload)
     assert len(ext_strategy_fixture.pending_requests) > 0
     response_channel = f"{ext_strategy_fixture.channel_prefix}/response/delete_offer"
     assert (ext_strategy_fixture.pending_requests == deque(
         [IncomingRequest("delete_offer", arguments, response_channel)]))
Ejemplo n.º 7
0
 def test_receive_bid_successful(self, ext_strategy_fixture):
     transaction_id = str(uuid.uuid4())
     arguments = {"transaction_id": transaction_id, "price": 1, "energy": 2}
     payload = {"data": json.dumps(arguments)}
     assert ext_strategy_fixture.pending_requests == deque([])
     ext_strategy_fixture.bid(payload)
     assert len(ext_strategy_fixture.pending_requests) > 0
     response_channel = f"{ext_strategy_fixture.channel_prefix}/response/bid"
     assert (ext_strategy_fixture.pending_requests == deque(
         [IncomingRequest("bid", arguments, response_channel)]))
Ejemplo n.º 8
0
def check_external_command_endpoint_with_correct_payload_succeeds(ext_strategy_fixture,
                                                                  command: str,
                                                                  arguments: Dict):
    transaction_id = str(uuid.uuid4())
    arguments.update({"transaction_id": transaction_id})
    payload = {"data": json.dumps(arguments)}
    assert ext_strategy_fixture.pending_requests == deque([])
    getattr(ext_strategy_fixture, command)(payload)
    assert len(ext_strategy_fixture.pending_requests) > 0
    response_channel = f"{ext_strategy_fixture.channel_prefix}/response/{command}"
    assert (ext_strategy_fixture.pending_requests ==
            deque([IncomingRequest(command, arguments, response_channel)]))
Ejemplo n.º 9
0
 def _bid(self, payload):
     transaction_id = self._get_transaction_id(payload)
     bid_response_channel = f'{self.channel_prefix}/response/bid'
     if not check_for_connected_and_reply(self.redis, bid_response_channel, self.connected):
         return
     try:
         arguments = json.loads(payload["data"])
         assert set(arguments.keys()) == {'price', 'energy', 'transaction_id'}
         arguments['buyer'] = self.device.name
         arguments['buyer_origin'] = self.device.name
     except Exception:
         self.redis.publish_json(
             bid_response_channel,
             {"command": "bid",
              "error": "Incorrect bid request. Available parameters: (price, energy).",
              "transaction_id": transaction_id})
     else:
         self.pending_requests.append(
             IncomingRequest("bid", arguments, bid_response_channel))