autorest icon indicating copy to clipboard operation
autorest copied to clipboard

C# IFooBarAPI should derive from IBaseAPI

Open koolraap opened this issue 11 years ago • 4 comments

I'd like to generically access the Credentials (when generated) and BaseUri properties of all generated APIs. I can do this via Reflection, but I'd rather do it the easy way. To that end, I suggest changing the generated interfaces from this:

    public partial interface IFooBarAPI
    {
        // these are common to all generated APIs
        Uri BaseUri { get; set; }
        JsonSerializerSettings SerializationSettings { get; }
        JsonSerializerSettings DeserializationSettings { get; }
        ServiceClientCredentials Credentials { get; set; }

        // these are unique per API
        IFooBars FooBars { get; }
  }

To something like this, where properties common to all generated API classes are in their own interface:

   public interface IBaseAPI
   {
        Uri BaseUri { get; set; }
        JsonSerializerSettings SerializationSettings { get; }
        JsonSerializerSettings DeserializationSettings { get; }
        ServiceClientCredentials Credentials { get; set; }
   }

    public partial interface IFooBarAPI : IBaseAPI
    {
        IFooBars FooBars { get; }
  }

koolraap avatar Nov 04 '15 23:11 koolraap

If I implement this would you prefer one base interface or two. Also API vs Api?

   public interface IBaseApi
   {
        Uri BaseUri { get; set; }
        JsonSerializerSettings SerializationSettings { get; }
        JsonSerializerSettings DeserializationSettings { get; }
   }

   public interface ICredentialApi : IBaseApi
   {
        ServiceClientCredentials Credentials { get; set; }
   }

    public partial interface IFooBarApi : IBaseApi // will be ICredentialApi if specified on command-line
    {
        IFooBars FooBars { get; }
    }

OR

   public interface IBaseApi
   {
        Uri BaseUri { get; set; }
        JsonSerializerSettings SerializationSettings { get; }
        JsonSerializerSettings DeserializationSettings { get; }
   }

   public interface ICredentialApi
   {
        ServiceClientCredentials Credentials { get; set; }
   }

    public partial interface IFooBarApi : IBaseApi, ICredentialApi
    {
        IFooBars FooBars { get; }
    }

koolraap avatar Dec 08 '15 05:12 koolraap

Regarding naming, the generated Azure clients are called IFooBarServiceClient, not IFooBarApi. So maybe IServiceClient instead of IBaseApi? If the credentials interface inherits from the base (option 1 above), maybe call it IServiceClientWithCredentials?

Also, it would be really useful if the credentials type were a parameter:

    public interface IServiceClientWithCredentials<T> : IServiceClient where T : ServiceClientCredentials
    {
        T Credentials { get; set; }
    }

Some APIs use their own authentication scheme, and parameterizing the credentials type would make it easier to integrate with the generated code.

bitsforthought avatar Dec 08 '15 18:12 bitsforthought

Thanks brjohns, that's a good idea. I should have some time next week to do this one.

BTW 0.9.7 generated code with an API suffix rather than Client :-) The thing that inspired this issue (and the other one I raised) is turning legacy code into an API. Due to conflicting dependencies with the Newtonsoft package we couldn't take a later version of AutoRest

koolraap avatar Dec 09 '15 06:12 koolraap

Howdy!

In our planning for driving towards a stable '1.0' release, I'm marking this issue as 'deferred' :zzz: and we're going to review it during the post-1.0 planning cycle.

It's not to say that we're not going to work on it, or that this isn't not important, but at the moment, we're picking and choosing the stuff we must do before 1.0. :horse_racing: :horse_racing: :horse_racing:

We'll make sure we pick this back up at that point. :tada:

fearthecowboy avatar Jun 15 '16 16:06 fearthecowboy