diff --git a/README.md b/README.md index d9f441d..51233e6 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Install is either via pip or cloning the repository. From pip: ```sh -python3 -m pip install thothlibrary==0.21.0 +python3 -m pip install thothlibrary==0.22.0 ``` Or from the repo: diff --git a/thothlibrary/__init__.py b/thothlibrary/__init__.py index 19d846c..3046b53 100644 --- a/thothlibrary/__init__.py +++ b/thothlibrary/__init__.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 """GraphQL client for Thoth""" -__version__ = "0.21.0" +__version__ = "0.22.0" __author__ = "Javier Arias " __copyright__ = "Copyright (c) 2020 Open Book Publishers" __license__ = "Apache 2.0" diff --git a/thothlibrary/client.py b/thothlibrary/client.py index 09ff4d4..679767f 100644 --- a/thothlibrary/client.py +++ b/thothlibrary/client.py @@ -54,9 +54,9 @@ def login(self, email, password): bearer = "Bearer {}".format(auth.get_token()) self.client.inject_token(bearer) - def mutation(self, mutation_name, data): + def mutation(self, mutation_name, data, nested=True): """Instantiate a thoth mutation and execute it""" - mutation = ThothMutation(mutation_name, data) + mutation = ThothMutation(mutation_name, data, nested) return mutation.run(self.client) def query(self, query_name, parameters, raw=False): @@ -140,6 +140,10 @@ def update_institution(self, institution): """Construct and trigger a mutation to update an institution object""" return self.mutation("updateInstitution", institution) + def delete_location(self, location): + """Construct and trigger a mutation to delete a location object""" + return self.mutation("deleteLocation", location, nested=False) + @staticmethod def supported_versions(): """ diff --git a/thothlibrary/mutation.py b/thothlibrary/mutation.py index c0faa2e..a32c51c 100644 --- a/thothlibrary/mutation.py +++ b/thothlibrary/mutation.py @@ -290,10 +290,16 @@ class ThothMutation(): ("countryCode", False) ], "return_value": "institutionId" + }, + "deleteLocation": { + "fields": [ + ("locationId", True), + ], + "return_value": "locationId" } } - def __init__(self, mutation_name, mutation_data): + def __init__(self, mutation_name, mutation_data, nested): """Returns new ThothMutation object with specified mutation data mutation_name: Must match one of the keys found in MUTATIONS. @@ -304,9 +310,9 @@ def __init__(self, mutation_name, mutation_data): self.return_value = self.MUTATIONS[mutation_name]["return_value"] self.mutation_data = mutation_data self.data_str = self.generate_values() - self.request = self.prepare_request() + self.request = self.prepare_request(nested) - def prepare_request(self): + def prepare_request(self, nested): """Format the mutation request string""" values = { "mutation_name": self.mutation_name, @@ -314,17 +320,29 @@ def prepare_request(self): "return_value": self.return_value } - payload = """ - mutation { - %(mutation_name)s( - data: { + if nested: + payload = """ + mutation { + %(mutation_name)s( + data: { + %(data)s + } + ) { + %(return_value)s + } + } + """ + else: + payload = """ + mutation { + %(mutation_name)s( %(data)s + ) { + %(return_value)s } - ) { - %(return_value)s } - } - """ + """ + return payload % values def run(self, client): diff --git a/thothlibrary/thoth-0_9_0/fixtures/QUERIES b/thothlibrary/thoth-0_9_0/fixtures/QUERIES index e1a81fc..6df6c08 100644 --- a/thothlibrary/thoth-0_9_0/fixtures/QUERIES +++ b/thothlibrary/thoth-0_9_0/fixtures/QUERIES @@ -410,10 +410,13 @@ "pageInterval", "issues { issueOrdinal series { seriesName issnPrint issnDigital } }", "languages { languageCode }", - "publications { isbn publicationType locations { canonical fullTextUrl } __typename }", - "contributions { fullName contributionType mainContribution affiliations { affiliationId institution { institutionName institutionId ror fundings { institutionId program projectName projectShortname grantNumber jurisdiction } } } contributor { contributorId orcid firstName lastName } contributionId contributionOrdinal __typename }", + "publications { isbn publicationType publicationId locations { locationId canonical landingPage fullTextUrl locationPlatform } __typename }", + "contributions { fullName firstName lastName contributionType mainContribution affiliations { affiliationId affiliationOrdinal institution { institutionName institutionId ror fundings { institutionId program projectName projectShortname grantNumber jurisdiction } } } contributor { contributorId orcid firstName lastName } contributionId contributionOrdinal __typename }", "imprint { __typename publisher { publisherName publisherId __typename } }", "subjects { subjectId, subjectType, subjectCode, subjectOrdinal, __typename }", + "relations { relationOrdinal relationType relatedWork { doi __typename } __typename }", + "references { doi unstructuredCitation __typename }", + "fundings { grantNumber institution { institutionName institutionDoi ror __typename } __typename }", "__typename" ] },