@@ -502,11 +502,19 @@ compileTypeDeclaration src (ServiceDeclaration name (Service methods) _ _) = do
502
502
methodMetadata <- mapM compileMethodMetadata methods'
503
503
let methodMetadata' = commaNl methodMetadata
504
504
dummyMethods <- mapM compileMethod methods'
505
+ clientMethods <- mapM compileClientMethod methods'
505
506
let dummyMethods' = T. intercalate " \n\n " dummyMethods
506
- withThirdPartyImports [ (" nirum.constructs" , [" name_dict_type" ])
507
- , (" nirum.rpc" , [" service_type" ])
508
- ] $
509
- return [qq |
507
+ clientMethods' = T. intercalate " \n\n " clientMethods
508
+ withStandardImport " urllib.request" $
509
+ withStandardImport " json" $
510
+ withThirdPartyImports [ (" nirum.constructs" , [" name_dict_type" ])
511
+ , (" nirum.deserialize" , [" deserialize_meta" ])
512
+ , (" nirum.serialize" , [" serialize_meta" ])
513
+ , (" nirum.rpc" , [ " service_type"
514
+ , " client_type"
515
+ ])
516
+ ] $
517
+ return [qq |
510
518
class $className(service_type):
511
519
512
520
__nirum_service_methods__ = \{
@@ -517,6 +525,13 @@ class $className(service_type):
517
525
])
518
526
519
527
{dummyMethods'}
528
+
529
+
530
+ # FIXME client MUST be generated & saved on diffrent module
531
+ # where service isn't included.
532
+ class {className}_Client(client_type, $className):
533
+ {clientMethods'}
534
+ pass
520
535
|]
521
536
where
522
537
className :: T. Text
@@ -561,6 +576,30 @@ class $className(service_type):
561
576
paramNameMap :: [Parameter ] -> T. Text
562
577
paramNameMap params = toIndentedCodes
563
578
toNamePair [pName | Parameter pName _ _ <- params] " ,\n "
579
+ compileClientPayload :: Parameter -> CodeGen Code
580
+ compileClientPayload (Parameter pName _ _) = do
581
+ let pName' = toAttributeName' pName
582
+ return [qq |meta['_names']['{pName'}']: serialize_meta({pName'})|]
583
+ compileClientMethod :: Method -> CodeGen Code
584
+ compileClientMethod (Method mName params rtype _ _ _) = do
585
+ let clientMethodName' = toAttributeName' mName
586
+ params' <- mapM compileParameter $ toList params
587
+ rtypeExpr <- compileTypeExpression src rtype
588
+ payloadArguments <- mapM compileClientPayload $ toList params
589
+ return [qq |
590
+ def {clientMethodName'}(self, {commaNl params'}) -> $rtypeExpr:
591
+ meta = self.__nirum_service_methods__['{clientMethodName'}']
592
+ return deserialize_meta(
593
+ meta['_return'],
594
+ json.loads(
595
+ self.remote_call(
596
+ self.__nirum_method_names__['{clientMethodName'}'],
597
+ payload=\{{commaNl payloadArguments}\}
598
+ )
599
+ )
600
+ )
601
+ |]
602
+
564
603
compileTypeDeclaration _ (Import _ _) =
565
604
return " " -- Nothing to compile
566
605
0 commit comments