diff --git a/Samples/7.WebAPI/Program.cs b/Samples/7.WebAPI/Program.cs new file mode 100644 index 00000000..a0bd4be5 --- /dev/null +++ b/Samples/7.WebAPI/Program.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using SteamKit2; +using System.Net; + +// +// Sample 7: WebAPI +// +// this sample will give an example of how the WebAPI utilities can be used to +// interact with the Steam Web APIs +// +// the Steam Web APIs are structured as a set of "interfaces" with methods, +// similar to classes in OO languages. +// as such, the API for interacting with the WebAPI follows a similar methodology + + +namespace Sample7_WebAPI +{ + class Program + { + static void Main( string[] args ) + { + // in order to interact with the Web APIs, you must first acquire an interface + // for a certain API + using ( dynamic steamNews = WebAPI.GetInterface( "ISteamNews" ) ) + { + // note the usage of c#'s dynamic feature, which can be used + // to make the api a breeze to use + + // the ISteamNews WebAPI has only 1 function: GetNewsForApp, + // so we'll be using that + + // when making use of dynamic, we call the interface function directly + // and pass any parameters as named arguments + KeyValue kvNews = steamNews.GetNewsForApp( appid: 440 ); // get news for tf2 + + // the return of every WebAPI call is a KeyValue class that contains the result data + + // for this example we'll iterate the results and display the title + foreach ( KeyValue news in kvNews[ "newsitems" ][ "newsitem" ].Children ) + { + Console.WriteLine( "News: {0}", news[ "title" ].AsString() ); + } + + // for functions with multiple versions, the version can be specified by + // adding a number after the function name when calling the API + + kvNews = steamNews.GetNewsForApp2( appid: 570 ); + + // if a number is not specified, version 1 is assumed by default + + // notice that the output of this version differs from the first version + foreach ( KeyValue news in kvNews[ "newsitems" ].Children ) + { + Console.WriteLine( "News: {0}", news[ "title" ].AsString() ); + } + + // note that the interface functions can throw WebExceptions when the API + // is otherwise inaccessible (networking issues, server downtime, etc) + // and these should be handled appropriately + try + { + kvNews = steamNews.GetNewsForApp002( appid: 730, maxlength: 100, count: 5 ); + } + catch ( WebException ex ) + { + Console.WriteLine( "Unable to make API request: {0}", ex.Message ); + } + } + + // for WebAPIs that require an API key, the key can be specified in the GetInterface function + using ( dynamic steamUserAuth = WebAPI.GetInterface( "ISteamUserAuth", "APIKEYGOESHERE" ) ) + { + // as the interface functions are synchronous, it may be beneficial to specify a timeout for calls + steamUserAuth.Timeout = ( int )TimeSpan.FromSeconds( 5 ).TotalMilliseconds; + + // additionally, if the API you are using requires you to POST or use an SSL connection, you may specify + // these settings with the "method" and "secure" reserved parameters + steamUserAuth.AuthenticateUser( someParam: "someValue", method: WebRequestMethods.Http.Post, secure: true ); + } + + // if you are using a language that does not have dynamic object support, or you otherwise don't wish to use it + // you can call interface functions through a Call method + using ( WebAPI.Interface steamNews = WebAPI.GetInterface( "ISteamNews" ) ) + { + Dictionary newsArgs = new Dictionary(); + newsArgs[ "appid" ] = "440"; + + KeyValue results = steamNews.Call( "GetNewsForApp", /* version */ 1, newsArgs ); + } + } + } +} diff --git a/Samples/7.WebAPI/Properties/AssemblyInfo.cs b/Samples/7.WebAPI/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..fdf03875 --- /dev/null +++ b/Samples/7.WebAPI/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle( "SteamKit Sample 7: WebAPI" )] +[assembly: AssemblyDescription( "SteamKit Sample 7: WebAPI" )] +[assembly: AssemblyConfiguration( "" )] +[assembly: AssemblyCompany( "" )] +[assembly: AssemblyProduct( "Sample7_WebAPI" )] +[assembly: AssemblyCopyright( "Copyright © Ryan Stecker 2013" )] +[assembly: AssemblyTrademark( "" )] +[assembly: AssemblyCulture( "" )] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible( false )] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid( "f0cc9aee-ab08-4206-b0f8-62dcd148d0d4" )] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion( "1.0.0.0" )] +[assembly: AssemblyFileVersion( "1.0.0.0" )] diff --git a/Samples/7.WebAPI/Sample7_WebAPI.csproj b/Samples/7.WebAPI/Sample7_WebAPI.csproj new file mode 100644 index 00000000..eb3524ef --- /dev/null +++ b/Samples/7.WebAPI/Sample7_WebAPI.csproj @@ -0,0 +1,63 @@ + + + + Debug + x86 + 8.0.30703 + 2.0 + {B93A6DDC-0648-4004-86DD-4B09DE1268E7} + Exe + Properties + Sample7_WebAPI + Sample7_WebAPI + v4.0 + Client + 512 + + + x86 + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + x86 + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + {BEB5BF07-BB56-402A-97A3-A41C6444C6A5} + SteamKit2 %28SteamKit2\SteamKit2%29 + + + + + \ No newline at end of file diff --git a/Samples/Samples.sln b/Samples/Samples.sln index be5ec3fd..82559a99 100644 --- a/Samples/Samples.sln +++ b/Samples/Samples.sln @@ -24,6 +24,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{0CC783 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample6_SteamGuard", "6.SteamGuard\Sample6_SteamGuard.csproj", "{2D681A01-7F28-43BB-B038-653AD3022895}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample7_WebAPI", "7.WebAPI\Sample7_WebAPI.csproj", "{B93A6DDC-0648-4004-86DD-4B09DE1268E7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -104,6 +106,16 @@ Global {2D681A01-7F28-43BB-B038-653AD3022895}.Release|Mixed Platforms.Build.0 = Release|x86 {2D681A01-7F28-43BB-B038-653AD3022895}.Release|x86.ActiveCfg = Release|x86 {2D681A01-7F28-43BB-B038-653AD3022895}.Release|x86.Build.0 = Release|x86 + {B93A6DDC-0648-4004-86DD-4B09DE1268E7}.Debug|Any CPU.ActiveCfg = Debug|x86 + {B93A6DDC-0648-4004-86DD-4B09DE1268E7}.Debug|Mixed Platforms.ActiveCfg = Debug|x86 + {B93A6DDC-0648-4004-86DD-4B09DE1268E7}.Debug|Mixed Platforms.Build.0 = Debug|x86 + {B93A6DDC-0648-4004-86DD-4B09DE1268E7}.Debug|x86.ActiveCfg = Debug|x86 + {B93A6DDC-0648-4004-86DD-4B09DE1268E7}.Debug|x86.Build.0 = Debug|x86 + {B93A6DDC-0648-4004-86DD-4B09DE1268E7}.Release|Any CPU.ActiveCfg = Release|x86 + {B93A6DDC-0648-4004-86DD-4B09DE1268E7}.Release|Mixed Platforms.ActiveCfg = Release|x86 + {B93A6DDC-0648-4004-86DD-4B09DE1268E7}.Release|Mixed Platforms.Build.0 = Release|x86 + {B93A6DDC-0648-4004-86DD-4B09DE1268E7}.Release|x86.ActiveCfg = Release|x86 + {B93A6DDC-0648-4004-86DD-4B09DE1268E7}.Release|x86.Build.0 = Release|x86 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE