-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphql_service.py
133 lines (118 loc) · 3.51 KB
/
graphql_service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os
import requests
import exceptions
def mutate(mutation, variables):
url=os.getenv('HYGRAPH_URL')
token = os.getenv("HYGRAPH_TOKEN")
headers = {"Authorization": f"Bearer {token}"}
try:
r = requests.post(url, json={"query": mutation, "variables": variables}, headers=headers)
json_data = r.json()
print("MUTATioN", mutation)
print("vas: ---------", variables)
print(json_data)
r.raise_for_status()
return json_data
except requests.exceptions.HTTPError as error:
raise exceptions.CustomException("HTTPError error " + str(error))
except requests.exceptions.ConnectionError as error:
raise exceptions.CustomException("ConnectionError" + str(error))
except requests.exceptions.Timeout as error:
raise exceptions.CustomException("Timeout error" + str(error))
except requests.exceptions.TooManyRedirects as error:
raise exceptions.CustomException("TooManyRedirects error" + str(error))
except requests.exceptions.RequestException as error:
raise exceptions.CustomException("RequestException(catastrophic) error" + str(error))
def create_episode(audio_id, program_slug, release_date, audio_url, transcript = ""):
mutation = """
mutation upsertEpisode(
$transcript: String,
$programSlug: String,
$releaseDate: Date,
$audioUrl: String,
$audioId: String
) {
upsertEpisode(
where:{ audioId: $audioId },
upsert: {
create: {
transcript: $transcript
program: {
connect: {
slug: $programSlug
}
}
releaseDate: $releaseDate,
audioUrl: $audioUrl,
audioId: $audioId
},
update: {
transcript: $transcript
program: {
connect: {
slug: $programSlug
}
}
releaseDate: $releaseDate,
audioUrl: $audioUrl,
audioId: $audioId
}
}) {
id
program {
id
}
}
}
"""
variables = {
"transcript": transcript,
"programSlug": program_slug,
"releaseDate": release_date,
"audioUrl": audio_url,
"audioId": audio_id
}
return mutate(mutation, variables)
def publish_episode(audio_id):
mutation = """
mutation publishEpisode($audioId: String) {
publishEpisode(where: { audioId: $audioId }, to: PUBLISHED) {
audioId
}
}
"""
variables = {
"audioId": audio_id
}
return mutate(mutation, variables)
def update_transcript(audio_id, transcript):
mutation = """
mutation UpdateEpisodeTranscript($audioId: String, $transcript: String){
updateEpisode(
where: { audioId: $audioId },
data: {
transcript: $transcript
}) {
id
}
}
"""
variables = {
"audioId": audio_id,
"transcript": transcript
}
return mutate(mutation, variables)
def gql_create_episode_and_publish(audio_id, program_slug, release_date, audio_url, transcript = ""):
try:
create_episode(audio_id, program_slug, release_date, audio_url, transcript)
publish_episode(audio_id)
except Exception as error:
raise exceptions.CustomException(str(error))
def gql_update_episode_transcript_and_publish(audio_id, transcript):
try:
update_transcript(audio_id, transcript)
publish_episode(audio_id)
print('gql_update_episode_transcript_and_publish')
except Exception as error:
print(error)
raise exceptions.CustomException(str(error))