Skip to content

Commit

Permalink
Merge branch 'release/v0.22.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ja573 committed Nov 20, 2023
2 parents d89bf63 + 08fe0bc commit 2f49f3a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion 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 <javi@openbookpublishers.com>"
__copyright__ = "Copyright (c) 2020 Open Book Publishers"
__license__ = "Apache 2.0"
Expand Down
8 changes: 6 additions & 2 deletions thothlibrary/client.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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():
"""
Expand Down
40 changes: 29 additions & 11 deletions thothlibrary/mutation.py
Expand Up @@ -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.
Expand All @@ -304,27 +310,39 @@ 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,
"data": self.data_str,
"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):
Expand Down
7 changes: 5 additions & 2 deletions thothlibrary/thoth-0_9_0/fixtures/QUERIES
Expand Up @@ -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"
]
},
Expand Down

0 comments on commit 2f49f3a

Please sign in to comment.