Esempio n. 1
0
    def test_post(self):
        """Test sending a cuds object to the server."""
        def request_callback(request):
            headers = {"request-id": "728d329e-0e86-11e4-a748-0c84dc037c13"}
            return (200, headers, request.body)

        responses.add_callback(
            responses.POST,
            "http://dsms.com",
            callback=request_callback,
            content_type="application/ld+json",
        )

        c, p1, p2, p3, n1, n2, s1 = get_test_city()
        response = post("http://dsms.com", c)

        serialized = serializable([c, p1, p2, p3, n1, n2, s1],
                                  partition_cuds=False,
                                  mark_first=True)
        assertJsonLdEqual(self, serialized, response.json())

        response = post("http://dsms.com", c, max_depth=1)
        serialized = serializable([c, p1, p2, p3, n1, n2],
                                  partition_cuds=False,
                                  mark_first=True)
        assertJsonLdEqual(self, serialized, response.json())
 def test_serializable(self):
     """Test function to make Cuds objects json serializable."""
     p = city.Citizen(age=23, name="Peter", uid=uuid.UUID(int=123))
     c = city.City(name="Freiburg", uid=uuid.UUID(int=1))
     c1 = city.Person(uid=uuid.UUID(int=2))
     c2 = city.Person(uid=uuid.UUID(int=3))
     p.add(c, rel=city.INVERSE_OF_hasInhabitant)
     p.add(c1, c2, rel=city.hasChild)
     assertJsonLdEqual(self, CUDS_DICT, serializable(p))
     assertJsonLdEqual(self, [CUDS_DICT], serializable([p]))
     assertJsonLdEqual(self, None, serializable(None))
     assertJsonLdEqual(self, [None, None], serializable([None, None]))
     assertJsonLdEqual(self,
                       {"UUID": "00000000-0000-0000-0000-000000000001"},
                       serializable(uuid.UUID(int=1)))
     assertJsonLdEqual(self, [{
         "UUID": "00000000-0000-0000-0000-000000000001"
     }, {
         "UUID": "00000000-0000-0000-0000-000000000002"
     }], serializable([uuid.UUID(int=1), uuid.UUID(int=2)]))
     assertJsonLdEqual(self, {"ENTITY": "city.Citizen"},
                       serializable(city.Citizen))
     assertJsonLdEqual(self, [{
         "ENTITY": "city.Citizen"
     }, {
         "ENTITY": "city.City"
     }], serializable([city.Citizen, city.City]))
     assertJsonLdEqual(self, [1, 1.2, "hallo"],
                       serializable([1, 1.2, "hallo"]))
Esempio n. 3
0
def serialize(cuds_object,
              rel=cuba.activeRelationship,
              max_depth=float("inf"),
              json_dumps=True):
    """Serialize a cuds objects and all of its contents recursively.

    Args:
        cuds_object (Cuds): The cuds object to serialize
        rel (OntologyRelationship, optional): The relationships to follow when
            serializing recursively. Defaults to cuba.activeRelationship.
        max_depth (int, optional): The maximum recursion depth.
            Defaults to float("inf").
        json_dumps (bool, optional): Whether to dump it to the registry.
            Defaults to True.

    Returns:
        Union[str, List]: The serialized cuds object.
    """
    from osp.core.session.transport.transport_utils import serializable
    from osp.core.utils import find_cuds_object
    cuds_objects = find_cuds_object(criterion=lambda x: True,
                                    root=cuds_object,
                                    rel=rel,
                                    find_all=True,
                                    max_depth=max_depth)
    result = serializable(cuds_objects)
    if json_dumps:
        return json.dumps(result)
    return result
Esempio n. 4
0
def post(url, cuds_object, max_depth=float("inf")):
    """Will send the given CUDS object to the given URL.

    Will also send the CUDS object in the container recursively.

    Args:
        url (string): The URL to send the CUDS object to
        cuds_object (Cuds): The CUDS to send
        max_depth (int, optional): The maximum depth to send CUDS objects
            recursively. Defaults to float("inf").

    Returns:
        [type]: [description]
    """
    from osp.core.utils import find_cuds_object
    from osp.core.session.transport.transport_utils import serializable
    cuds_objects = find_cuds_object(criterion=lambda x: True,
                                    root=cuds_object,
                                    rel=cuba.activeRelationship,
                                    find_all=True,
                                    max_depth=max_depth)
    serialized = json.dumps(serializable(cuds_objects))
    return requests.post(url=url,
                         data=serialized,
                         headers={"content_type": "application/json"})
Esempio n. 5
0
    def test_post(self):
        """Test sending a cuds object to the server."""
        def request_callback(request):
            headers = {'request-id': '728d329e-0e86-11e4-a748-0c84dc037c13'}
            return (200, headers, request.body)

        responses.add_callback(
            responses.POST,
            'http://dsms.com',
            callback=request_callback,
            content_type='application/json',
        )

        c, p1, p2, p3, n1, n2, s1 = get_test_city()
        response = post('http://dsms.com', c)

        serialized = serializable([c, p1, p2, p3, n1, n2, s1])
        assertJsonLdEqual(self, serialized, response.json())

        response = post('http://dsms.com', c, max_depth=1)
        serialized = serializable([c, p1, p2, p3, n1, n2])
        assertJsonLdEqual(self, serialized, response.json())
 def _store(self, cuds_object):
     # Initialize the server, when the first cuds_object is stored.
     if self.root is None:
         data = {
             "args": self.args,
             "kwargs": self.kwargs,
             "root": serializable(cuds_object),
             "hashes": get_hash_dir(self._file_destination),
             "auth": self.auth
         }
         super()._store(cuds_object)
         self._engine.send(INITIALIZE_COMMAND,
                           json.dumps(data))
         logger.debug("Remove %s from added buffer in context %s of session"
                      " %s" % (cuds_object, self._current_context, self))
         del self._buffers[self._current_context][
             BufferType.ADDED][cuds_object.uid]
         return
     super()._store(cuds_object)
Esempio n. 7
0
def _serialize_session_json(session, json_dumps=True):
    """Serialize a session in application/ld+json format.

    Args:
        session (Session): The session to serialize.
        json_dumps (bool, optional): Whether to dump it to the registry.
            Defaults to True.

    Returns:
        Union[str, List]: The serialized session.
    """
    from osp.core.session.transport.transport_utils import serializable

    cuds_objects = list(cuds for cuds in session._registry.values()
                        if not cuds.is_a(cuba.Wrapper))
    result = serializable(cuds_objects, partition_cuds=False, mark_first=False)
    if json_dumps:
        return json.dumps(result)
    return result