Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started
I am trying to generate a client in C# from the Swagger resful API on Power BI Report Server. In Visual Studio 2017 there is a feature where you can generate a client proxy from the Solution explorer by right-clicking the project, Add and REST API Client. In the dialog it asks for Swagger url or an existing metadata file. I have tried using http://my-server/reports/api/v2.0/, http://my-server/reports/api/v2.0/swagger.json, https://app.swaggerhub.com/apis/microsoft-rs/PBIRS/2.0/ and https://app.swaggerhub.com/apis/microsoft-rs/PBIRS/2.0/swagger.json which all generates the total useless error message "Failed to download metadata file for Microsoft Azure API App : ConsoleApp1Client".
API documentation: https://app.swaggerhub.com/apis/microsoft-rs/PBIRS/2.0/
Hopefully a good guide to the rest of the work: https://www.red-gate.com/simple-talk/dotnet/net-development/visual-studio-2017-swagger-building-docu...
Has anyone successfylly generated a C# client for PBIRS? If so please share!
Solved! Go to Solution.
If you just want the JSON file you can get it from SwaggerHub here:
https://app.swaggerhub.com/apis/microsoft-rs/PBIRS/2.0
In the top right you'll see an arrow pointing down, click that and you should see and option to download the JSON. I just noticed that there were some codegen customisations that we missed for PBIRS but added for SSRS which may be why your getting compilation errors, I just added them in I'll try it on my end to make sure it works. However if you still want to try the editor let me know if you see any issues.
Matt M.
Visual Studio uses something called AutoRest to generate there REST stubs, you can see the GitHub link here:
https://github.com/Azure/AutoRest
Unfortuntely the version that ships in Visual Studio has some issues and GitHub is updated more frequently than VS. If your looking to generate client proxies there are some other options you can try, for most of our testing we used either the opensource Swagger editor or the best option is to use the swagger codegen CLI.
1. Swagger Codegen - https://github.com/swagger-api/swagger-codegen It's a little more work to get setup but has the most support and can be cusotmized quite a bit.
2. Swagger Editor - Got to http://editor.swagger.io of you copy and past the Swagger json into the editor you can export to many differant languages in the UI C# being one of them
3. Swagger Hub - this is where we have the docs hosted and like the editor above it has export options as well but they're code gen is a few steps behind the open source one
Let me know how it goes or is you have any other questions!
Thanks!
Matt M.
Thanks Matt!
I found the little arrow (download client) button just by chance! It would help, and save countless hours of searching, if you could mention in some documentation how to actually use the REST API. All I have come across is just mentioning that there is an API and a bit what you can do with it. Sorry for the rant, but I have spent all day on this getting nowhere.
As I said, I downloaded the generated client then opened it in VS 2017. It did not compile. Read the readme and tried running the build.bat, same thing, did not compile. I choose csharpe, and I allso tried CsharpDotNet2 but that did not compile either. The compile errors I got from csharp is lots of these:
src\IO.Swagger\Model\ReportParameterDefinition.cs(65,325): error CS1750: A value of type '<null>' cannot be used as a default parameter because there are no standard conversions to type 'IO.Swagger.Model.ReportParameterState'
A quick look at Swagger Codegen tells me Im going to spent one more day just getting the environment ready to output a client!?
The Swagger Editor needs the json which I asked for in my original question. Where is it? How do I get that file?
Sorry again and thank you for your help!
If you just want the JSON file you can get it from SwaggerHub here:
https://app.swaggerhub.com/apis/microsoft-rs/PBIRS/2.0
In the top right you'll see an arrow pointing down, click that and you should see and option to download the JSON. I just noticed that there were some codegen customisations that we missed for PBIRS but added for SSRS which may be why your getting compilation errors, I just added them in I'll try it on my end to make sure it works. However if you still want to try the editor let me know if you see any issues.
Matt M.
OK, downloaded the JSON(resolved) and imported it into swagger editor online. All is fine. The generated client csharp. Opened it ind cmd and ran build.bat. The window is flooded by compile errors!
src\IO.Swagger\Model\Resource.cs(158,23): warning CS0108: 'IO.Swagger.Model.Resource.ContentType' hides inherited member 'IO.Swagger.Model.CatalogItem.ContentType'. Use the new keyword if hiding was intended. src\IO.Swagger\Model\CatalogItem.cs(158,23): (Location of symbol related to previous warning) src\IO.Swagger\Model\Resource.cs(165,23): warning CS0108: 'IO.Swagger.Model.Resource.Content' hides inherited member 'IO.Swagger.Model.CatalogItem.Content'. Use the new keyword if hiding was intended. src\IO.Swagger\Model\CatalogItem.cs(165,23): (Location of symbol related to previous warning) src\IO.Swagger\Model\Resource.cs(172,22): warning CS0108: 'IO.Swagger.Model.Resource.IsFavorite' hides inherited member 'IO.Swagger.Model.CatalogItem.IsFavorite'. Use the new keyword if hiding was intended. src\IO.Swagger\Model\CatalogItem.cs(172,22): (Location of symbol related to previous warning) src\IO.Swagger\Model\ResourceItem.cs(55,22): warning CS0108: 'IO.Swagger.Model.ResourceItem.Id' hides inherited member 'IO.Swagger.Model.ManifestItem.Id'. Use the new keyword if hiding was intended. src\IO.Swagger\Model\ManifestItem.cs(53,22): (Location of symbol related to previous warning) src\IO.Swagger\Model\ResourceItem.cs(62,23): warning CS0108: 'IO.Swagger.Model.ResourceItem.Path' hides inherited member 'IO.Swagger.Model.ManifestItem.Path'. Use the new keyword if hiding was intended. src\IO.Swagger\Model\ManifestItem.cs(60,23): (Location of symbol related to previous warning) src\IO.Swagger\Model\ResourceItem.cs(69,23): warning CS0108: 'IO.Swagger.Model.ResourceItem.Name' hides inherited member 'IO.Swagger.Model.ManifestItem.Name'. Use the new keyword if hiding was intended. src\IO.Swagger\Model\ManifestItem.cs(67,23): (Location of symbol related to previous warning) src\IO.Swagger\Model\ResourceItem.cs(76,23): warning CS0108: 'IO.Swagger.Model.ResourceItem.Hash' hides inherited member 'IO.Swagger.Model.ManifestItem.Hash'. Use the new keyword if hiding was intended. src\IO.Swagger\Model\ManifestItem.cs(74,23): (Location of symbol related to previous warning) src\IO.Swagger\Model\ThumbnailItem.cs(55,22): warning CS0108: 'IO.Swagger.Model.ThumbnailItem.Id' hides inherited member 'IO.Swagger.Model.ManifestItem.Id'. Use the new keyword if hiding was intended.
Am I using the wrong .Net version? I have installed Visual Studio 2017 so I guess I have the latest stable version of C#!?
I just tried it myself via the editor and everything compiled fine, however there were a lot of Warnings mostly because of the syntax that the generated code is using. The list that you show only contains Warnings, were you getting errors as well? Also it looks like the code that SwaggerHub generates doesn't compile most likely because they haven't aligned with the latest codegen tools I'll file a ticket with them and hopefully get it resolved.
Youre right, it compiles in VS 2017. And running build.bat outputs the bin directory with some dlls. I guess I can use them even if there are lots of warnings. Do I need to use all of the dlls or just IO.Swagger.dll?
Hi Martin
Could you successful use the generated API Client from Swagger HUB? Did you find source code examples for C# how to use the client?
I read the readme.md, could compile the "csharp-client-generated" Project (had to fixe some things there) but now I have no idea how to use the IO.Swagger and connect my PBI Report Server.
Greetings, Roli
OK, here my experiences with the Swagger on 2018-02-03 (can vary if you do it after that date because of updates)
Create IO.Swagger Assembly
ManifestItem.ToJson()
public virtual string ToJson()
Use IO.Swagger Assembly in your Application
using IO.Swagger.Api; using IO.Swagger.Client; using IO.Swagger.Model; using RestSharp;
var clientCI = new CatalogItemsApi(apiString); clientCI.Configuration.ApiClient.RestClient.Authenticator = new NtlmAuthenticator(CredentialCache.DefaultCredentials); ODataCatalogItems cItems = clientCI.GetCatalogItems();
Happy coding!
Roli
hey i guess the generated code has changed and there is one more problem with it.
i got the C# client coe and this one part does not compile and i amtrying to see what the right whay is to make it work.
in apiclient.cs at about line number 138 is a bit of code like this:
request.AddFile( param.Value.Name, param.Value.Writer, param.Value.FileName, param.Value.ContentType );
now the item param.Value.Writer is of type Action<T> where T is a Stream
the parameter on AddFile ( ... ) wants an array of byte
so i need to get the stream and then the bytes if this was not an Action<T> id have no problem.
but I am not familiar with that.
any ideas on what should be done here ??
hey i guess the generated code has changed and there is one more problem with it.
i got the C# client coe and this one part does not compile and i amtrying to see what the right whay is to make it work.
in apiclient.cs at about line number 138 is a bit of code like this:
request.AddFile( param.Value.Name, param.Value.Writer, param.Value.FileName, param.Value.ContentType );
now the item param.Value.Writer is of type Action<T> where T is a Stream
the parameter on AddFile ( ... ) wants an array of byte
so i need to get the stream and then the bytes if this was not an Action<T> id have no problem.
but I am not familiar with that.
any ideas on what should be done here ??
so i found that this is related to a new build of RestSharp, i had to go to an older version to make it build.
so i gess the swagger hub generated code and RestSharp are not playing well together....
The choice of Microsoft to use a Rest Api / Swagger looks very trendy, but from the point of view of a Visual Studio developer, the good old SOAP Web Service was more stable and easy to use.
In fact, no other development tool except Visual Studio could generate a web service client from a SOAP WSDL definition. All other dev tools were overwhelmed ... Microsoft had to react to this fact and take a "more open" path, so that developers can use other tools to use their web service interfaces too.
Now thats the result: Swagger IO, oh my god... Totally out of control 😞
Check out the September 2024 Power BI update to learn about new features.
Learn from experts, get hands-on experience, and win awesome prizes.
User | Count |
---|---|
8 | |
5 | |
2 | |
2 | |
2 |