11from logging import getLogger
2- from typing import AnyStr , Callable , Dict , List , Optional , Tuple , Union
2+ from typing import Any , AnyStr , Callable , Dict , List , Optional , Text , Tuple , Union
33
44from .api_resources .mutations import *
55from .api_resources .payload import (
66 CreateEtchPacketPayload ,
77 FillPDFPayload ,
8+ ForgeSubmitPayload ,
89 GeneratePDFPayload ,
910)
1011from .api_resources .requests import GraphqlRequest , PlainRequest , RestRequest
@@ -37,7 +38,12 @@ class Anvil:
3738 def __init__ (self , api_key = None , environment = 'dev' ):
3839 self .client = HTTPClient (api_key = api_key , environment = environment )
3940
40- def query (self , query : str , variables : Optional [str ] = None , ** kwargs ):
41+ def query (
42+ self ,
43+ query : str ,
44+ variables : Union [Optional [Text ], Dict [Text , Any ]] = None ,
45+ ** kwargs ,
46+ ):
4147 gql = GraphqlRequest (client = self .client )
4248 return gql .post (query , variables = variables , ** kwargs )
4349
@@ -187,6 +193,10 @@ def get_welds(self, **kwargs) -> Union[List, Tuple[List, Dict]]:
187193 eid
188194 slug
189195 title
196+ forges {
197+ eid
198+ name
199+ }
190200 }
191201 }
192202 }
@@ -200,6 +210,38 @@ def get_data(r):
200210
201211 return _get_return (res , get_data = get_data )
202212
213+ def get_weld (self , eid : Text , ** kwargs ):
214+ res = self .query (
215+ """
216+ query WeldQuery(
217+ #$organizationSlug: String!,
218+ #$slug: String!
219+ $eid: String!
220+ ) {
221+ weld(
222+ #organizationSlug: $organizationSlug,
223+ #slug: $slug
224+ eid: $eid
225+ ) {
226+ eid
227+ slug
228+ name
229+ forges {
230+ eid
231+ name
232+ slug
233+ }
234+ }
235+ }""" ,
236+ variables = dict (eid = eid ),
237+ ** kwargs ,
238+ )
239+
240+ def get_data (r ):
241+ return r ["data" ]["weld" ]
242+
243+ return _get_return (res , get_data = get_data )
244+
203245 def create_etch_packet (
204246 self ,
205247 payload : Optional [
@@ -253,3 +295,33 @@ def download_documents(self, document_group_eid: str, **kwargs):
253295 """Retrieve all completed documents in zip form."""
254296 api = PlainRequest (client = self .client )
255297 return api .get (f"document-group/{ document_group_eid } .zip" , ** kwargs )
298+
299+ def forge_submit (
300+ self ,
301+ payload : Optional [Union [Dict [Text , Any ], ForgeSubmitPayload ]] = None ,
302+ json = None ,
303+ ** kwargs ,
304+ ):
305+ """Create a Webform (forge) submission via a graphql mutation."""
306+ if not any ([json , payload ]):
307+ raise TypeError ('One of arguments `json` or `payload` are required' )
308+
309+ if json :
310+ payload = ForgeSubmitPayload .parse_raw (
311+ json , content_type = "application/json"
312+ )
313+
314+ if isinstance (payload , dict ):
315+ mutation = ForgeSubmit .create_from_dict (payload )
316+ elif isinstance (payload , ForgeSubmitPayload ):
317+ mutation = ForgeSubmit (payload = payload )
318+ else :
319+ raise ValueError (
320+ "`payload` must be a valid ForgeSubmitPayload instance or dict"
321+ )
322+
323+ return self .mutate (
324+ mutation ,
325+ variables = mutation .create_payload ().dict (by_alias = True , exclude_none = True ),
326+ ** kwargs ,
327+ )
0 commit comments