/// <summary> /// Returns an OAuth token + refresh pair in an array. If no redirect /// is set, the redirect in the Dwolla Application Settings will be used /// </summary> /// <param name="code">Code from redirect response</param> /// <param name="redirect">Redirect destination</param> /// <returns>OAuthResponse object</returns> public OAuthResponse Get(string code, string redirect = null) { var data = new Dictionary <string, string> { { "client_id", C.dwolla_key }, { "client_secret", C.dwolla_secret }, { "grant_type", "authorization_code" }, { "code", code } }; if (redirect != null) { data["redirect_uri"] = redirect; } var response = Post("/token", data, "/oauth/v2"); var oar = Jss.Deserialize <OAuthResponse>(response); if (oar.access_token != null) { return(oar); } throw new ApiException(Jss.Deserialize <OAuthError>(response).error_description); }
/// <summary> /// Fetch data from disk /// </summary> /// <param name="key">key</param> /// <returns></returns> public T Fetch <T>(string key) { try { string jsonText = File.ReadAllText(StoreDirectory + "//" + key + ".json"); return(!string.IsNullOrEmpty(jsonText) ? Jss.Deserialize <T>(jsonText) : default(T)); } catch (Exception) { return(default(T)); } }
/// <summary> /// Returns a "catalog" of endpoints that are available for use with the current/passed OAuth token. /// </summary> /// <param name="altToken">Alternate OAuth token</param> /// <returns>OAuthCatalog object</returns> public OAuthCatalog Catalog(string altToken = null) { var response = Get("/catalog", new Dictionary <string, string> { { "oauth_token", altToken ?? C.dwolla_access_token } }); var cat = Jss.Deserialize <OAuthCatalog>(response); if (cat.Success) { return(cat); } throw new OAuthException(cat.Message); }
/// <summary> /// Store data to disk /// </summary> /// <param name="key">key The search key (becomes the filename .. so keep it alphanumeric)</param> /// <param name="data">data Data to persist (will be persisted in json format)</param> /// <returns> true on success, false otherwise</returns> public bool store(string key, Dictionary <string, dynamic> data) { string jsonstr = Jss.Serialize(data); try { File.WriteAllText(StoreDirectory + "//" + key + ".json", jsonstr); } catch (Exception) { return(false); } return(true); }
/// <summary> /// Returns a newly refreshed access token and refresh token pair. /// </summary> /// <param name="refreshToken">Refresh token from initial OAuth handshake.</param> /// <returns>OAuthResponse object</returns> public OAuthResponse Refresh(string refreshToken) { var response = Post("/token", new Dictionary <string, string> { { "client_id", C.dwolla_key }, { "client_secret", C.dwolla_secret }, { "grant_type", "refresh_token" }, { "refresh_token", refreshToken } }, "/oauth/v2"); var oar = Jss.Deserialize <OAuthResponse>(response); if (oar.access_token != null) { return(oar); } throw new OAuthException(Jss.Deserialize <OAuthError>(response).error_description); }
/// <summary> /// Device Detect /// </summary> /// <param name="data">Data for device detection : HTTP Headers usually</param> /// <returns>true on success, false otherwise. Use getReply to inspect results on success.</returns> public bool DeviceDetect(Dictionary <string, dynamic> data = null) { int id = 0; if (data == null || !data.Any() || !data.ContainsKey("id")) { id = Convert.ToInt32(Config["site_id"]); } else { id = Convert.ToInt32(data["id"]); } Dictionary <string, dynamic> requestBody = new Dictionary <string, dynamic>(); foreach (KeyValuePair <string, dynamic> item in data) { if (requestBody.ContainsKey(item.Key.ToLower())) { requestBody[item.Key.ToLower()] = item.Value; } else { requestBody.Add(item.Key.ToLower(), item.Value); } } string fastKey = ""; // If caching enabled then check cache if (Cacherequests) { IOrderedEnumerable <dynamic> headersKeys = requestBody.Values.Select(c => c).OrderBy(c => c); fastKey = Jss.Serialize(headersKeys).Replace(" ", ""); Dictionary <string, dynamic> objReply = _cache.Read <Dictionary <string, dynamic> >(fastKey); if (objReply != null && objReply.Count > 0) { Reply = objReply; SetRawReply(); return(SetError(0, "OK")); } } try { bool result = false; if (UseLocal) { result = _device.LocalDetect(requestBody); // Log unknown headers if enabled SetError(_device.GetStatus(), _device.GetMessage()); } else { result = Remote(string.Format("/device/detect/{0}", id), requestBody); } if (Cacherequests) { _cache.Write(fastKey, GetReply()); } return(result); } catch (Exception ex) { SetError(299, "Exception : " + ex.Message + " " + ex.StackTrace); return(false); } }
/// <summary> /// Returns all devices inside one giant array /// Used by localDevice* functions to iterate over all devies /// </summary> /// <returns>array All devices in one giant assoc array</returns> public Dictionary <string, dynamic> FetchDevices() { Dictionary <string, dynamic> data = new Dictionary <string, dynamic>(); List <Dictionary <string, dynamic> > dicList = new List <Dictionary <string, dynamic> >(); try { string[] filePaths = System.IO.Directory.GetFiles(StoreDirectory, "Device*.json"); dicList.AddRange(from item in filePaths select File.ReadAllText(item) into jsonText where !string.IsNullOrEmpty(jsonText) select Jss.Deserialize <Dictionary <string, dynamic> >(jsonText)); data["devices"] = dicList; return(data); } catch (Exception ex) { Reply = new Dictionary <string, dynamic>(); SetError(1, "Exception : " + ex.Message + " " + ex.StackTrace); } return(null); }