private ProfileResponse GetProfileDataInternal(bool force) { //We don't want to allow hammering of the API, so we cache response for 60 seconds. var profileResponse = new ProfileResponse(); profileResponse.LoginStatus = LoginStatus.Ok; string cachedResponse = _Cache.Get(Constants.CACHE_PROFILEJSON) as string; if (!String.IsNullOrEmpty(cachedResponse) && !force) { profileResponse.Cached = true; profileResponse.HttpStatusCode = HttpStatusCode.OK; profileResponse.Json = cachedResponse; return(profileResponse); } if (!_CurrentProfile.LoggedIn) { var loginResponse = LoginInternal(); if (loginResponse.Status != LoginStatus.Ok) { profileResponse.LoginStatus = loginResponse.Status; profileResponse.HttpStatusCode = loginResponse.HttpStatusCode; profileResponse.Cached = false; return(profileResponse); } } using (var response = _Http.Get(Constants.URL_PROFILE, null)) { profileResponse.Cached = false; profileResponse.HttpStatusCode = response.StatusCode; if (response.StatusCode == HttpStatusCode.OK) { using (StreamReader sr = new StreamReader(response.GetResponseStream())) { profileResponse.Json = sr.ReadToEnd(); if (profileResponse.Json.ToLower().Equals("profile unavailable")) { profileResponse.LoginStatus = LoginStatus.UnknownError; profileResponse.Json = "{}"; } } } else { profileResponse.LoginStatus = LoginStatus.NotAccessible; profileResponse.Json = "{}"; } } _Cache.Set(Constants.CACHE_PROFILEJSON, profileResponse.Json, DateTimeOffset.Now.AddSeconds(Constants.CACHE_PROFILE_SECONDS)); _sWatch.Restart(); return(profileResponse); }
private ProfileResponse GetProfileDataInternal(bool force = false) { //We don't want to allow hammering of the API, so we cache response for 60 seconds. var profileResponse = new ProfileResponse(); profileResponse.LoginStatus = LoginStatus.Ok; string cachedResponse = _Cache.Get(Constants.CACHE_PROFILEJSON) as string; if (!String.IsNullOrEmpty(cachedResponse) && !force) { profileResponse.Cached = true; profileResponse.HttpStatusCode = HttpStatusCode.OK; profileResponse.Json = cachedResponse; return(profileResponse); } if (!_CurrentProfile.LoggedIn) { var loginResponse = LoginInternal(); if (loginResponse.Status != LoginStatus.Ok) { profileResponse.LoginStatus = loginResponse.Status; profileResponse.HttpStatusCode = loginResponse.HttpStatusCode; profileResponse.Cached = false; return(profileResponse); } } StringBuilder fullJson = new StringBuilder(Constants.RESPONSE_PATTERN); using (var response = _Http.Get(Constants.URL_BASE + Constants.URL_ADD_PROFILE, null)) { string tempData; profileResponse.Cached = false; profileResponse.HttpStatusCode = response.StatusCode; if (response.StatusCode == HttpStatusCode.OK) { using (StreamReader sr = new StreamReader(response.GetResponseStream())) { tempData = sr.ReadToEnd(); if (tempData.ToLower().Equals("profile unavailable")) { profileResponse.LoginStatus = LoginStatus.UnknownError; profileResponse.PlainData = tempData; tempData = "{}"; } } } else { profileResponse.LoginStatus = LoginStatus.NotAccessible; tempData = "{}"; } fullJson.Replace("**PH1**", tempData); if (profileResponse.HttpStatusCode == HttpStatusCode.OK) { JObject profileJson = null; // load more data ? try { profileJson = JsonConvert.DeserializeObject <JObject>(tempData); } catch (Exception) { profileResponse.PlainData = tempData; } if (profileJson != null) { if (profileJson["commander"].Value <Boolean>("docked")) { if (profileJson["lastStarport"]["services"].SelectToken("commodities") != null) { using (var response_md = _Http.Get(Constants.URL_BASE + Constants.URL_ADD_MARKET, null)) { string tempData_md; profileResponse.Cached = false; profileResponse.HttpStatusCode = response_md.StatusCode; if (response_md.StatusCode == HttpStatusCode.OK) { using (StreamReader sr = new StreamReader(response_md.GetResponseStream())) { tempData_md = sr.ReadToEnd(); } } else { // error happened profileResponse.LoginStatus = LoginStatus.NotAccessible; tempData_md = "{}"; } fullJson.Replace("**PH2**", tempData_md); } } else { // station has no market fullJson.Replace("**PH2**", "{}"); } if ((profileJson["lastStarport"]["services"].SelectToken("outfitting") != null) || (profileJson["lastStarport"]["services"].SelectToken("shipyard") != null)) { using (var response_sy = _Http.Get(Constants.URL_BASE + Constants.URL_ADD_SHIPYARD, null)) { string tempData_sy; profileResponse.Cached = false; profileResponse.HttpStatusCode = response_sy.StatusCode; if (response_sy.StatusCode == HttpStatusCode.OK) { using (StreamReader sr = new StreamReader(response_sy.GetResponseStream())) { tempData_sy = sr.ReadToEnd(); } } else { // error happened profileResponse.LoginStatus = LoginStatus.NotAccessible; tempData_sy = "{}"; } fullJson.Replace("**PH3**", tempData_sy); } } else { // station has no shipyard and no outfitting fullJson.Replace("**PH3**", "{}"); } } else { // not docked fullJson.Replace("**PH2**", "{}"); fullJson.Replace("**PH3**", "{}"); } } else { // error - profile not parsable profileResponse.LoginStatus = LoginStatus.DataError; fullJson.Clear(); fullJson.Append(Constants.RESPONSE_EMPTY); } } else { fullJson.Replace("**PH2**", "{}"); fullJson.Replace("**PH3**", "{}"); } } profileResponse.Json = fullJson.ToString(); _Cache.Set(Constants.CACHE_PROFILEJSON, profileResponse.Json, DateTimeOffset.Now.AddSeconds(Constants.CACHE_PROFILE_SECONDS)); _sWatch.Restart(); return(profileResponse); }