├── .gitignore ├── LICENSE.md ├── README.md ├── sample ├── Sample.cs └── Sample.csproj └── sdk ├── Glitch.cs ├── GlitchRequest.cs ├── IGlitchRequester.cs └── SDK.csproj /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | sdk/obj/x86/Release/glitch-csharp-sdk.csproj.FileListAbsolute.txt 3 | 4 | sdk/obj/x86/Release/SDK.csproj.FileListAbsolute.txt 5 | 6 | sdk/obj/x86/Release/GlitchSDK.pdb 7 | 8 | sdk/obj/x86/Release/GlitchSDK.dll 9 | 10 | sdk/obj/x86/Release/DesignTimeResolveAssemblyReferencesInput.cache 11 | 12 | sdk/obj/x86/Debug/glitch-csharp-sdk.pdb 13 | 14 | sdk/obj/x86/Debug/glitch-csharp-sdk.exe 15 | 16 | sdk/obj/x86/Debug/glitch-csharp-sdk.csproj.FileListAbsolute.txt 17 | 18 | sdk/obj/x86/Debug/SDK.csproj.FileListAbsolute.txt 19 | 20 | sdk/obj/x86/Debug/GlitchSDK.pdb 21 | 22 | sdk/obj/x86/Debug/GlitchSDK.dll 23 | 24 | sdk/obj/x86/Debug/DesignTimeResolveAssemblyReferencesInput.cache 25 | 26 | sdk/bin/Release/glitch-csharp-sdk.pdb 27 | 28 | sdk/bin/Release/glitch-csharp-sdk.exe.config 29 | 30 | sdk/bin/Release/glitch-csharp-sdk.exe 31 | 32 | sdk/bin/Release/GlitchSDK.pdb 33 | 34 | sdk/bin/Release/GlitchSDK.dll 35 | 36 | sdk/bin/Debug/glitch-csharp-sdk.vshost.exe.manifest 37 | 38 | sdk/bin/Debug/glitch-csharp-sdk.vshost.exe.config 39 | 40 | sdk/bin/Debug/glitch-csharp-sdk.vshost.exe 41 | 42 | sdk/bin/Debug/glitch-csharp-sdk.pdb 43 | 44 | sdk/bin/Debug/glitch-csharp-sdk.exe.config 45 | 46 | sdk/bin/Debug/glitch-csharp-sdk.exe 47 | 48 | sdk/bin/Debug/GlitchSDK.pdb 49 | 50 | sdk/bin/Debug/GlitchSDK.dll 51 | 52 | sdk/SDK.csproj.user 53 | 54 | sample/obj/x86/Release/Sample.pdb 55 | 56 | sample/obj/x86/Release/Sample.exe 57 | 58 | sample/obj/x86/Release/Sample.csproj.FileListAbsolute.txt 59 | 60 | sample/obj/x86/Release/ResolveAssemblyReference.cache 61 | 62 | sample/obj/x86/Release/DesignTimeResolveAssemblyReferencesInput.cache 63 | 64 | sample/obj/x86/Debug/Sample.pdb 65 | 66 | sample/obj/x86/Debug/Sample.exe 67 | 68 | sample/obj/x86/Debug/Sample.csproj.FileListAbsolute.txt 69 | 70 | sample/obj/x86/Debug/ResolveAssemblyReference.cache 71 | 72 | sample/obj/x86/Debug/DesignTimeResolveAssemblyReferencesInput.cache 73 | 74 | sample/bin/Release/Sample.vshost.exe.manifest 75 | 76 | sample/bin/Release/Sample.vshost.exe.config 77 | 78 | sample/bin/Release/Sample.vshost.exe 79 | 80 | sample/bin/Release/Sample.pdb 81 | 82 | sample/bin/Release/Sample.exe 83 | 84 | sample/bin/Release/GlitchSDK.pdb 85 | 86 | sample/bin/Release/GlitchSDK.dll 87 | 88 | sample/bin/Debug/Sample.vshost.exe.manifest 89 | 90 | sample/bin/Debug/Sample.vshost.exe 91 | 92 | sample/bin/Debug/Sample.pdb 93 | 94 | sample/bin/Debug/Sample.exe 95 | 96 | sample/bin/Debug/GlitchSDK.pdb 97 | 98 | sample/bin/Debug/GlitchSDK.dll 99 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright 2011 Tiny Speck, Inc. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Glitch C# / Unity SDK 2 | =============== 3 | 4 | This SDK is intended to help you interact with the [Glitch API](http://api.glitch.com/). The SDK includes functionality for calling API methods and getting a parsed response. 5 | 6 | If you have any feedback or find bugs, please let us know on the [Glitch Developer Forum](http://developer.glitch.com/forum/). We want to hear from you! -------------------------------------------------------------------------------- /sample/Sample.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using GlitchSDK; 6 | using System.Threading; 7 | 8 | namespace GlitchSample 9 | { 10 | class Sample : IGlitchRequester // Implement IGlitchRequester, which will receive callback events from the async request 11 | { 12 | static ManualResetEvent resetEvent = new ManualResetEvent(false); 13 | 14 | static void Main(string[] args) 15 | { 16 | // Create new instance of class 17 | Sample sample = new Sample(); 18 | 19 | // Set our access token 20 | // It is up to you to get this yourself 21 | // To get assistance with retrieving an access token, please follow the authentication tutorial 22 | // at http://developer.glitch.com/docs/auth/ 23 | 24 | string accessToken = ""; // SET ACCESS TOKEN HERE 25 | 26 | // Instantiate our Glitch class, from which you will interact with the APIs 27 | Glitch glitch = new Glitch(accessToken); 28 | 29 | // Get a request with a specific method 30 | GlitchRequest request = glitch.GetRequest("players.fullInfo", new Dictionary 31 | { 32 | // Parameters go here 33 | {"player_tsid", "PIF12K4LV4D1FCG"} 34 | }); 35 | 36 | // Execute the request! 37 | request.Execute(sample); 38 | 39 | resetEvent.WaitOne(); // Hold the program until the async call completes 40 | } 41 | 42 | 43 | #region IGlitchRequester Interface Methods 44 | 45 | // Request completed 46 | public void RequestFinished(GlitchRequest request) 47 | { 48 | Console.WriteLine("Request Finished!"); 49 | 50 | // Null checks and type check 51 | if (request != null && request.response != null && 52 | request.response.GetType().Equals(typeof(Dictionary))) 53 | { 54 | // Cast to dictionary 55 | Dictionary response = request.response as Dictionary; 56 | 57 | // Declare playerName to be set later 58 | object playerName; 59 | 60 | // Try to get player_name from the dictionary 61 | if (response.TryGetValue("player_name", out playerName)) 62 | { 63 | // Check if it is a string before writing it to the console 64 | if (playerName.GetType().Equals(typeof(String))) 65 | { 66 | Console.WriteLine("Hello " + playerName as String); 67 | } 68 | } 69 | } 70 | 71 | resetEvent.Set(); // Allow the program to exit 72 | } 73 | 74 | // Request failed with associated exception 75 | public void RequestFailed(GlitchRequest request, Exception exception) 76 | { 77 | resetEvent.Set(); // Allow the program to exit 78 | 79 | if (exception != null) 80 | { 81 | throw exception; // Throw exception, handle this how you want 82 | } 83 | } 84 | 85 | #endregion 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /sample/Sample.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | x86 6 | 8.0.30703 7 | 2.0 8 | {9562B617-F0BE-459E-92B8-E48015B594EC} 9 | Exe 10 | Properties 11 | Sample 12 | Sample 13 | v4.0 14 | 15 | 16 | 512 17 | 18 | 19 | x86 20 | true 21 | full 22 | false 23 | bin\Debug\ 24 | DEBUG;TRACE 25 | prompt 26 | 4 27 | 28 | 29 | x86 30 | pdbonly 31 | true 32 | bin\Release\ 33 | TRACE 34 | prompt 35 | 4 36 | 37 | 38 | 39 | 40 | 41 | 42 | {15CF29DB-8CF3-498E-A9A2-7CCD73202B89} 43 | SDK 44 | 45 | 46 | 47 | 54 | -------------------------------------------------------------------------------- /sdk/Glitch.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace GlitchSDK 7 | { 8 | public class Glitch 9 | { 10 | #region Constants 11 | 12 | public const String BASE_URL = "http://api.glitch.com"; // Base service URL 13 | 14 | #endregion 15 | 16 | 17 | #region Public Properties 18 | 19 | public String accessToken; // Access token for the currently logged in user 20 | 21 | #endregion 22 | 23 | 24 | #region Constructors 25 | 26 | // Constructor for Glitch object 27 | // Access token should be obtained by the developer before instantiating this 28 | // To get assistance with retrieving an access token, please follow the authentication tutorial 29 | // at http://developer.glitch.com/docs/auth/ 30 | public Glitch(String accessToken) 31 | { 32 | if (String.IsNullOrEmpty(accessToken)) 33 | { 34 | throw new ArgumentException("Please specify your access token when initializing a Glitch object"); 35 | } 36 | 37 | this.accessToken = accessToken; 38 | } 39 | 40 | #endregion 41 | 42 | 43 | #region Interacting with the API 44 | 45 | public GlitchRequest GetRequest(String method) 46 | { 47 | return GetRequest(method, null); 48 | } 49 | 50 | public GlitchRequest GetRequest(String method, Dictionary parameters) 51 | { 52 | return new GlitchRequest(method, parameters, this); 53 | } 54 | 55 | #endregion 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /sdk/GlitchRequest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Net; 6 | using System.Web.Script.Serialization; 7 | using System.IO; 8 | 9 | namespace GlitchSDK 10 | { 11 | public class GlitchRequest 12 | { 13 | #region Constants 14 | 15 | public const String API_URL = Glitch.BASE_URL + "/simple/"; 16 | 17 | #endregion 18 | 19 | 20 | #region Public Properties 21 | 22 | public String url; // Full url for request, e.g. "http://api.glitch.com/simple/players.info" 23 | public String method; // Specific method without 'simple', e.g. "players.info" 24 | public Dictionary parameters; // Dictionary of parameters passed in the request 25 | public IGlitchRequester requester; // Requester that will be called when events occur before, during, and after the request 26 | public object response; // JSON response object 27 | 28 | #endregion 29 | 30 | 31 | #region Private Properties 32 | 33 | private HttpWebRequest request; // Async request that interacts with API 34 | private Glitch glitch; // Glitch parent instance 35 | 36 | #endregion 37 | 38 | 39 | #region Constructors 40 | 41 | public GlitchRequest(String startMethod, Dictionary startParams, Glitch startGlitch) 42 | { 43 | method = startMethod; 44 | parameters = startParams; 45 | glitch = startGlitch; 46 | } 47 | 48 | public GlitchRequest(String startMethod, Glitch startGlitch) 49 | { 50 | method = startMethod; 51 | parameters = null; 52 | glitch = startGlitch; 53 | } 54 | 55 | #endregion 56 | 57 | 58 | #region Interacting with the API 59 | 60 | // Call this to execute your request 61 | // Pass in the delegate which will be called when events occur that are related to this object 62 | public void Execute(IGlitchRequester glitchRequester) 63 | { 64 | // Set local IGlitchRequester for callbacks 65 | requester = glitchRequester; 66 | 67 | // Concatenate the full url 68 | String fullUrl = API_URL + method; 69 | 70 | // If we don't have parameters, create the dictionary 71 | if (parameters == null) 72 | { 73 | parameters = new Dictionary(); 74 | } 75 | 76 | // Add access token to dictionary 77 | if (glitch.accessToken != null) 78 | { 79 | parameters.Add("oauth_token", glitch.accessToken); 80 | } 81 | 82 | // Serialize our full url with our parameters 83 | fullUrl = SerializeURL(fullUrl, parameters); 84 | 85 | // Create the request and set its method 86 | request = HttpWebRequest.Create(fullUrl) as HttpWebRequest; 87 | request.Method = WebRequestMethods.Http.Get; 88 | 89 | // Begin the response with the associated callback 90 | request.BeginGetResponse(r => 91 | { 92 | // Callback 93 | // r is the IAsyncResult 94 | 95 | // Get the associated Glitch Request object 96 | GlitchRequest glitchRequest = r.AsyncState as GlitchRequest; 97 | 98 | // Attempt to parse the response to check for ok 99 | try 100 | { 101 | // Get the associated response object 102 | HttpWebResponse httpWebResponse = request.EndGetResponse(r) as HttpWebResponse; 103 | 104 | // Read the response into a string 105 | StreamReader reader = new StreamReader(httpWebResponse.GetResponseStream()); 106 | String result = reader.ReadToEnd(); 107 | reader.Dispose(); 108 | 109 | // Serialize the response string into an object 110 | JavaScriptSerializer serializer = new JavaScriptSerializer(); 111 | object response = serializer.DeserializeObject(result); 112 | 113 | // Check the type before checking for ok 114 | if (response.GetType().Equals(typeof(Dictionary))) 115 | { 116 | // Cast to a dictionary 117 | Dictionary responseDict = response as Dictionary; 118 | 119 | // Declare ok object 120 | object ok; 121 | 122 | // Attempt to get the ok value 123 | if (responseDict.TryGetValue("ok", out ok)) 124 | { 125 | // If ok is an int 126 | if (ok.GetType().Equals(typeof(int))) 127 | { 128 | // Check if it is 1, which means the request was ok 129 | if ((int)ok == 1) 130 | { 131 | // Set the response for the request 132 | glitchRequest.response = response; 133 | 134 | // Call the requester with the request 135 | requester.RequestFinished(glitchRequest); 136 | 137 | return; 138 | } 139 | } 140 | 141 | // If the service did not return ok or did not return ok = 1 142 | requester.RequestFailed(glitchRequest, new Exception("Service did not return OK")); 143 | } 144 | } 145 | } 146 | catch (Exception ex) 147 | { 148 | // If there was an exception somewhere in the parsing process, send it back to the requester 149 | requester.RequestFailed(glitchRequest, ex); 150 | } 151 | 152 | // If we get to this point, we didn't encounter an exception but we didn't get a valid parsed response 153 | requester.RequestFailed(glitchRequest, new Exception("An unknown error occurred with the request!")); 154 | }, this); 155 | } 156 | 157 | // Cancel the pending request 158 | public void Cancel() 159 | { 160 | // Abort the request 161 | if (request != null) 162 | { 163 | request.Abort(); 164 | } 165 | } 166 | 167 | #endregion 168 | 169 | 170 | #region URL Helper Methods 171 | 172 | public static String SerializeURL(String url, Dictionary parameters) 173 | { 174 | url = url + "?"; 175 | 176 | url = url + SerializeParams(parameters); 177 | 178 | return url; 179 | } 180 | 181 | public static String SerializeParams(Dictionary parameters) 182 | { 183 | String serializedParameters = ""; 184 | 185 | if (parameters != null && parameters.Count > 0) 186 | { 187 | foreach (String key in parameters.Keys) 188 | { 189 | serializedParameters = serializedParameters + "&" + key + "=" + parameters[key]; 190 | } 191 | 192 | // Remove extra ampersand from front 193 | serializedParameters = serializedParameters.Substring(1); 194 | } 195 | 196 | return serializedParameters; 197 | } 198 | 199 | #endregion 200 | } 201 | } 202 | -------------------------------------------------------------------------------- /sdk/IGlitchRequester.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace GlitchSDK 7 | { 8 | public interface IGlitchRequester 9 | { 10 | // Called when a request is completed 11 | // Check the method via request.method and response via request.response 12 | void RequestFinished(GlitchRequest request); 13 | 14 | // Called when a request fails 15 | void RequestFailed(GlitchRequest request, Exception exception); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /sdk/SDK.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | x86 6 | 8.0.30703 7 | 2.0 8 | {15CF29DB-8CF3-498E-A9A2-7CCD73202B89} 9 | Library 10 | Properties 11 | GlitchSDK 12 | GlitchSDK 13 | v4.0 14 | 15 | 16 | 512 17 | publish\ 18 | true 19 | Disk 20 | false 21 | Foreground 22 | 7 23 | Days 24 | false 25 | false 26 | true 27 | 0 28 | 1.0.0.%2a 29 | false 30 | false 31 | true 32 | 33 | 34 | x86 35 | true 36 | full 37 | false 38 | bin\Debug\ 39 | DEBUG;TRACE 40 | prompt 41 | 4 42 | 43 | 44 | x86 45 | pdbonly 46 | true 47 | bin\Release\ 48 | TRACE 49 | prompt 50 | 4 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | False 66 | Microsoft .NET Framework 4 %28x86 and x64%29 67 | true 68 | 69 | 70 | False 71 | .NET Framework 3.5 SP1 Client Profile 72 | false 73 | 74 | 75 | False 76 | .NET Framework 3.5 SP1 77 | false 78 | 79 | 80 | False 81 | Windows Installer 3.1 82 | true 83 | 84 | 85 | 86 | 93 | --------------------------------------------------------------------------------