Ejemplo n.º 1
0
        public PackageModel(RestDescription discoveryDoc)
        {
            _discoveryDoc = discoveryDoc;
            ApiName       = discoveryDoc.Name;

            // Note that spaces are removed first, because the Python code doesn't Pascal-case them. For example,
            // the "Cloud Memorystore for Memcached" API has a package name of "CloudMemorystoreforMemcached".
            // It's awful, but that's the way it works...
            ClassName        = (discoveryDoc.CanonicalName ?? discoveryDoc.Name).Replace(" ", "").ToMemberName();
            ServiceClassName = $"{ClassName}Service";
            ApiVersion       = discoveryDoc.Version;
            VersionNoDots    = discoveryDoc.Version.Replace('.', '_');
            PackageName      = $"Google.Apis.{ClassName}.{VersionNoDots}";
            DataModels       = discoveryDoc.Schemas.ToReadOnlyList(pair => new DataModel(this, parent: null, name: pair.Key, schema: pair.Value));

            // Populate the data model dictionary early, as methods and resources refer to the data model types.
            foreach (var dm in DataModels)
            {
                _dataModels[dm.Id] = dm;
            }
            Resources   = discoveryDoc.Resources.ToReadOnlyList(pair => new ResourceModel(this, parent: null, pair.Key, pair.Value));
            ApiFeatures = discoveryDoc.Features.ToReadOnlyList();
            // TODO: Ordering?
            AuthScopes = (discoveryDoc.Auth?.Oauth2?.Scopes).ToReadOnlyList(pair => new AuthScope(pair.Key, pair.Value.Description));
            ServiceTyp = Typ.Manual(PackageName, ServiceClassName);
            GenericBaseRequestTypDef = Typ.Manual(PackageName, $"{ClassName}BaseServiceRequest");
            BaseRequestTyp           = Typ.Generic(GenericBaseRequestTypDef, Typ.GenericParam("TResponse"));
            BaseUri   = discoveryDoc.RootUrl + discoveryDoc.ServicePath;
            BasePath  = discoveryDoc.ServicePath;
            BatchUri  = discoveryDoc.RootUrl + discoveryDoc.BatchPath;
            BatchPath = discoveryDoc.BatchPath;
            Title     = discoveryDoc.Title;
            Methods   = discoveryDoc.Methods.ToReadOnlyList(pair => new MethodModel(this, null, pair.Key, pair.Value));
        }
Ejemplo n.º 2
0
        public ClassDeclarationSyntax GenerateBaseRequestClass(SourceFileContext ctx)
        {
            var cls = Class(
                Modifier.Public | Modifier.Abstract,
                BaseRequestTyp,
                ctx.Type(Typ.Generic(typeof(ClientServiceRequest <>), Typ.GenericParam("TResponse"))))
                      .WithXmlDoc(XmlDoc.Summary($"A base abstract class for {ClassName} requests."));

            using (ctx.InClass(BaseRequestTyp))
            {
                var serviceParam = Parameter(ctx.Type <IClientService>(), "service");
                var ctor         = Ctor(Modifier.Protected, cls, BaseInitializer(serviceParam))(serviceParam)
                                   .WithBody()
                                   .WithXmlDoc(XmlDoc.Summary($"Constructs a new {ClassName}BaseServiceRequest instance."));

                var parameters = CreateParameterList(BaseRequestTyp);

                cls = cls.AddMembers(ctor);
                cls = cls.AddMembers(parameters.SelectMany(p => p.GenerateDeclarations(ctx)).ToArray());

                var initParameters = Method(Modifier.Protected | Modifier.Override, VoidType, "InitParameters")()
                                     .WithBlockBody(
                    BaseExpression().Call("InitParameters")(),
                    parameters.Select(p => p.GenerateInitializer(ctx)).ToArray())
                                     .WithXmlDoc(XmlDoc.Summary($"Initializes {ClassName} parameter list."));

                cls = cls.AddMembers(initParameters);
            }
            return(cls);
        }
Ejemplo n.º 3
0
        public ClassDeclarationSyntax GenerateBaseRequestClass(SourceFileContext ctx)
        {
            var cls = Class(
                Modifier.Public,
                Typ.Generic(Typ.Manual(PackageName, $"{ClassName}BaseServiceRequest"), Typ.GenericParam("TResponse")),
                ctx.Type(Typ.Generic(typeof(ClientServiceRequest <>), Typ.GenericParam("TResponse"))))
                      .WithXmlDoc(XmlDoc.Summary($"A base abstract class for {ClassName} requests."));

            var serviceParam = Parameter(ctx.Type <IClientService>(), "service");
            var ctor         = Ctor(Modifier.Protected, cls, BaseInitializer(serviceParam))(serviceParam)
                               .WithBody()
                               .WithXmlDoc(XmlDoc.Summary($"Constructs a new {ClassName}BaseServiceRequest instance."));

            cls = cls.AddMembers(ctor);

            var parameters = _discoveryDoc.Parameters.Select(p => new ParameterModel(p.Key, p.Value))
                             .OrderBy(p => p.PropertyName)
                             .ToList();

            cls = cls.AddMembers(parameters.Select(p => p.GenerateProperty(ctx)).ToArray());

            var initParameters = Method(Modifier.Protected | Modifier.Override, VoidType, "InitParameters")()
                                 .WithBlockBody(
                BaseExpression().Call("InitParameters")(),
                parameters.Select(p => p.GenerateInitializer(ctx)).ToArray())
                                 .WithXmlDoc(XmlDoc.Summary($"Initializes {ClassName} parameter list."));

            cls = cls.AddMembers(initParameters);
            return(cls);
        }
Ejemplo n.º 4
0
 public Paginated(ServiceDetails svc, MethodDescriptor desc,
                  FieldDescriptor responseResourceField, int pageSizeFieldNumber, int pageTokenFieldNumber) : base(svc, desc)
 {
     ResourceTyp = ProtoTyp.Of(responseResourceField, forceRepeated: false);
     // For map fields, ResourceTyp is a constructed KeyValuePair<,> type: we need the open type in a cref.
     ResourceTypForCref = responseResourceField.IsMap
         ? Typ.Generic(typeof(KeyValuePair <,>), Typ.GenericParam("TKey"), Typ.GenericParam("TValue"))
         : ResourceTyp;
     ApiCallTyp           = Typ.Generic(typeof(ApiCall <,>), RequestTyp, ResponseTyp);
     SyncReturnTyp        = Typ.Generic(typeof(PagedEnumerable <,>), ResponseTyp, ResourceTyp);
     AsyncReturnTyp       = Typ.Generic(typeof(PagedAsyncEnumerable <,>), ResponseTyp, ResourceTyp);
     SyncGrpcType         = Typ.Generic(typeof(GrpcPagedEnumerable <, ,>), RequestTyp, ResponseTyp, ResourceTyp);
     AsyncGrpcType        = Typ.Generic(typeof(GrpcPagedAsyncEnumerable <, ,>), RequestTyp, ResponseTyp, ResourceTyp);
     ResourcesFieldName   = responseResourceField.CSharpPropertyName();
     PageSizeFieldNumber  = pageSizeFieldNumber;
     PageTokenFieldNumber = pageTokenFieldNumber;
 }