Пример #1
0
    def create_deck(self, deck):
        """Create a new Deck for the currently logged in user.

        Args:
            deck (Deck): The Deck object to create.

        Returns:
            Deck: The created Deck object if creation was successful.

        """
        form_boundary = self._generate_form_boundary()

        # Clone headers to not modify the global variable.
        headers = dict(DEFAULT_HEADERS)
        # Explicitly set Content-Type to multipart/form-data.
        headers['Content-Type'] = ('multipart/form-data; boundary=%s'
                                   % form_boundary)

        request_payload = json_converter.deck_to_json(deck)
        request_payload = self._to_multipart_form(request_payload,
                                                  form_boundary)
        r = self.session.post(url=API_URL + 'decks',
                              headers=headers,
                              data=request_payload)

        json_data = r.json()
        created_deck = json_converter.json_to_deck(json_data)

        return created_deck
Пример #2
0
    def get_decks(self, user_id):
        """Get all Decks for the currently logged in user.

        Returns:
            list: The list of retrieved decks.

        """
        request_url = API_URL + 'decks?userId=' + str(user_id)
        r = self.session.get(url=request_url)

        if r.status_code != 200:
            raise ValueError(r.text)

        json_response = r.json()
        decks = []
        for d in json_response['decks']:
            current_deck = json_converter.json_to_deck(d)
            decks.append(current_deck)

        return decks
Пример #3
0
    def update_deck(self, deck):
        """Update an existing deck.

        Args:
            deck (Deck): The Deck object to update.

        Returns:
            Deck: The updated Deck object if update was successful.

        """
        headers = DEFAULT_HEADERS
        request_payload = json_converter.deck_to_json(deck)

        r = self.session.patch(url=API_URL + 'decks/' + deck.id,
                               headers=headers,
                               json=request_payload)

        json_data = r.json()
        updated_deck = json_converter.json_to_deck(json_data)

        return updated_deck
Пример #4
0
    def delete_deck(self, deck_id):
        """Delete an existing deck.

        Args:
            deck_id (str): The ID of the Deck to delete.

        Returns:
            Deck: The deleted Deck object if deletion was successful.

        """
        if type(deck_id) is not str:
            raise ValueError("'deck_id' parameter must be of type str")

        headers = DEFAULT_HEADERS

        r = self.session.delete(url=API_URL + 'decks/' + deck_id,
                                headers=headers)

        json_data = r.json()
        deleted_deck = json_converter.json_to_deck(json_data)

        return deleted_deck
Пример #5
0
    def get_deck(self, deck_id, user_id, include_cards=True):
        """Get the Deck with the given ID.

        Args:
            deck_id (str): The ID of the deck to retrieve.
            include_cards (bool): Only include the cards of the deck when set
                to True (as by default). Otherwise cards will be an empty list.

        Returns:
            Deck: The retrieved deck.

        """
        request_url = API_URL + 'decks/' + deck_id
        if include_cards:
            request_url += '?expand=true'
        r = self.session.get(url=request_url)
        json_response = r.json()

        deck = json_converter.json_to_deck(json_response)
        # Set additional properties.
        deck.id = deck_id
        deck.user_id = user_id

        return deck