简体   繁体   中英

How to work with Visual Studio 2017 generated restful api from swagger docs

So I am working with Visual Studio 2017 Enterprise, and I noticed the other day that you can right click and do file add restful api client. So I went out and generated the .json file for my restful service. My Visual Studio generated a bunch of classes and files. However, I don't understand how to work with these classes?

What is an example code of creating an object to call my restful backend methods?

I thought perhaps this was the object I need to work with, but then what are the ServiceClientCredentials if they cannot be null?

public AngularDemoComplete(ServiceClientCredentials credentials, params DelegatingHandler[] handlers) : this(handlers)
    {
        if (credentials == null)
        {
            throw new ArgumentNullException("credentials");
        }
        this.Credentials = credentials;
        if (this.Credentials != null)
        {
            this.Credentials.InitializeServiceClient(this);
        }
    }

I am still trying to work out a complete solution, but I did manage to get this working by passing in these parameters:

(new TokenCredentials("<bearer token>"), null)

Unfortunately I don't really understand what this means at the moment. I found this in a post about working with autorest clients, which seems to generate similar code: https://dzimchuk.net/generating-clients-for-your-apis-with-autorest/

I am continuing to work this out and will update this as I learn more.

Any updates from you would be appreciated.

Cheers

Additionally, I got it working by commenting out the null check on the credentials parameter. But that probably is not advisable. It is just interesting to note that it can work without the credentials if there is no authentication required.

UPDATE: I have found there are 2 other types of credentials available that appear to be a bit more self explanatory:

  • BasicAuthenticationCredentials
  • CertificateCredentials

Apparently with the TokenCredentials you would need to obtain the token from the server prior to this step. This might be needed if using OAuth. As I don't need authentication initially, I am just going to use the BasicAuthenticationCredentials without setting the UserName and Password properties.

I was surprised there were not more examples of how to use the generated code, created by https://github.com/Azure/AutoRest so I thought it worthwhile to add something here.

+10 for @Keiran for referring to BasicAuthenticationCredentials, which as it turns out, extends BasicAuthenticationCredentials and therefore, can be used as follows:

BasicAuthenticationCredentials credentials = new BasicAuthenticationCredentials();
credentials.UserName = "joe@bloggs.com.au";
credentials.Password = "passwordvalue";
System.Uri baseUri = new System.Uri("https://api.something.com.au/v0.1/");
APIClassName client = new APIClassName(baseUri, credentials); 

APIClassNameSpace.Models.SomeResponse someResponse = client.GetSomething(requestHeaderParameter1, pathParameter1);

Visual Studio 2019 currently produces a Models folder containing all of the classes referenced by the swagger file. It also produces three other classes, eg IAPIClassName, APIClassName, APIClassNameExtensions. Use APIClassName to instantiate the client - the operations specified in the swagger file will be available with auto-completion. I didn't dig too deeply but the class prefixed with I looked like an interface definition and the class suffixed with extensions looked lie the implementation of the interface. My example illustrated the use of a request header parameter and a path parameter although the implementation of that handling is generated using the details in the swagger file.

I must mention How to handle both a single item and an array for the same property using JSON.net as part of this discussion. Anyone dealing with the JSON and generated code for should know about this!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM