Пример #1
0
    def find_data(self, entity_name, version, data):
        """
        Find data according to data query
        Args:
            entity_name (str): entity name
            version (str/None): entity version
            data (dict): data contains query and projection field

        Returns:
            - dict - result of search and projection query

        """
        try:
            if version is not None:
                LOGGER.debug("%s - %s -%s", 'POST',
                             self.data_api.find[entity_name][version],
                             data['query'])
                response = POST(self.data_api.find[entity_name][version], data)
            else:
                LOGGER.debug("%s - %s -%s", 'POST',
                             self.data_api.find[entity_name], data['query'])
                response = POST(self.data_api.find[entity_name], data)

            self.log_response(response)

            return response
        except BeanBagException:
            LOGGER.exception("Find data failed")
            LOGGER.debug(json.dumps(data))
Пример #2
0
    def update_data(self, entity_name, version, data):
        """
        Update data according to data query and update field
        Args:
            entity_name (str): entity_name
            version (str/None): entity version
            data (dict): data contains query and update field

        Returns:
            - dict - lightblue response

        """
        try:
            if version is not None:
                LOGGER.debug("%s - %s -%s", 'POST',
                             self.data_api.update[entity_name][version],
                             data['query'])
                response = POST(self.data_api.update[entity_name][version],
                                data)
            else:
                LOGGER.debug("%s - %s -%s", 'POST',
                             self.data_api.update[entity_name], data['query'])
                response = POST(self.data_api.update[entity_name], data)

            self.log_response(response)

            return response
        except BeanBagException:
            LOGGER.exception("Update data failed")
            LOGGER.debug(json.dumps(data))
Пример #3
0
def test_bb():
    s = FakeSession()
    b = BeanBag("http://www.example.org/path/", session=s)

    assert str(b) == "http://www.example.org/path/"

    s.expect("GET", "http://www.example.org/path/")
    GET(b)

    s.expect("GET", "http://www.example.org/path/", params=dict(a=1, b=2))
    GET(b(a=1, b=2))

    s.expect("PUT", "http://www.example.org/path/")
    PUT(b._, None)

    s.expect("PATCH", "http://www.example.org/path/")
    PATCH(b._, None)

    s.expect("DELETE", "http://www.example.org/path/")
    DELETE(b._)

    s.expect("POST", "http://www.example.org/path/foo", data={"a": 1})
    POST(b.foo, dict(a=1))

    s.expect("GET", "http://www.example.org/path/", params=dict(status=300))
    with pytest.raises(BeanBagException) as e:
        GET(b(status=300))
    assert "Bad response code: 300" == e.value.msg

    s.expect("GET", "http://www.example.org/path/", params=dict(result="BAD"))
    with pytest.raises(BeanBagException) as e:
        GET(b(result="BAD"))
    assert "Could not decode response" == e.value.msg
Пример #4
0
def test_sane_inheritance():
    class MyBeanBag(BeanBag):
        def helper(self, param):
            pass

        def encode(self, body):
            return super(~MyBeanBag, self).encode(body)

        def decode(self, response):
            return super(~MyBeanBag, self).decode(response)

    s = FakeSession()
    bb = MyBeanBag("http://www.example.org/path", session=s)

    assert type(bb) is MyBeanBag
    assert type(bb.subpath) is MyBeanBag
    assert type(bb[1]) is MyBeanBag

    assert hasattr(~MyBeanBag, "helper")

    s.expect("GET", "http://www.example.org/path/")
    GET(bb)

    s.expect("POST", "http://www.example.org/path/foo", data={"a": 1})
    r = POST(bb.foo, dict(a=1))
    assert type(r) is AttrDict
    assert r.data == '{"a": 1}' and r.method == 'POST'
Пример #5
0
 def listRRs(self, name=None, type=None, data=None):
     payload = {}
     if name is not None:
         payload['name'] = name
     if type is not None:
         payload['type'] = type
     if data is not None:
         payload['data'] = data
     return POST(self.beanbag.listRRs, payload)
Пример #6
0
    def test_parse_with_nfsnbeanbag(self):
        """ The NfsnBeanBag class can handle the 'application/x-nfsn-api'
        content-type in the server's HTTP response. """

        nfsn = NfsnBeanBag(self.endpoint_url)

        httpretty.register_uri(httpretty.POST,
                               self.api_url,
                               body=self.request_callback)

        assert POST(nfsn.dns['example.com'].listRRs)
Пример #7
0
    def test_parse_fail_with_beanbag(self):
        """ An ordinary BeanBag object will fail because of the
        'application/x-nfsn-api' content-type header in the server's HTTP
        response. """
        nfsn = BeanBag(self.endpoint_url)

        httpretty.register_uri(httpretty.POST,
                               self.api_url,
                               body=self.request_callback)

        # When this test no longer raises a BeanBagException, it's possible
        # that the BeanBag authors might have updated their decoding function
        # to be more lax about handling non-application/json content-types. If
        # that's the case, this NfsnBeanBag wrapper class might become
        # unnecessary.
        with pytest.raises(BeanBagException):
            POST(nfsn.dns['example.com'].listRRs)
Пример #8
0
 def removeRR(self, name, type, data):
     payload = {'name': name, 'type': type, 'data': data}
     return POST(self.beanbag.removeRR, payload)
Пример #9
0
 def listForwards(self):
     return POST(self.beanbag.listForwards)
Пример #10
0
 def removeWarning(self, balance):
     return POST(self.beanbag.removeWarning, {'balance': balance})
Пример #11
0
 def addWarning(self, balance):
     return POST(self.beanbag.addWarning, {'balance': balance})
Пример #12
0
 def addSite(self, site):
     return POST(self.beanbag.addSite, {'site': site})
Пример #13
0
 def removeAlias(self, alias):
     return POST(self.beanbag.removeAlias, {'alias': alias})
Пример #14
0
 def addAlias(self, alias):
     return POST(self.beanbag.addAlias, {'alias': alias})
Пример #15
0
 def setForward(self, forward, dest_email):
     payload = {'forward': forward, 'dest_email': dest_email}
     return POST(self.beanbag.setForward, payload)
Пример #16
0
 def removeForward(self, forward):
     return POST(self.beanbag.removeForward, {'forward': forward})
Пример #17
0
 def addRR(self, name, type, data, ttl=None):
     payload = {'name': name, 'type': type, 'data': data}
     if ttl is not None:
         payload['ttl'] = ttl
     return POST(self.beanbag.addRR, payload)
Пример #18
0
 def updateSerial(self):
     return POST(self.beanbag.updateSerial)