public WrappedData DataRequest(cleverRequestType requestType, cleverRequestSubType requestSubType, List <KeyValuePair <String, String> > kvpList) { string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name + "_3"; /////////////////////////////////////////////////////////////////////////////////////////////////////// //// Example for "https://api.clever.com/v1.1/schools/530e595026403103360ff9fd/students?limit=120" //// //// Example for "https://api.clever.com/v1.1/schools/530e595026403103360ff9fd/students" //// /////////////////////////////////////////////////////////////////////////////////////////////////////// //string requestSubTypeStr = requestSubType.ToString().ToLower(); string msg = string.Format("reqType: {0} :: reqSubType: {1} :: List<kvp> count: {2}", requestType, requestSubType, kvpList.Count); mLogger.Log(methodName, msg, 1); WrappedData wrappedData = new WrappedData(); string pluralItems = "s"; string url = string.Empty; Tuple <string, string, bool> kvpStrTuple = mHelper.ConvertKvpToStringAndValidate(mLogger, kvpList); string kvpStr = kvpStrTuple.Item1; string id = kvpStrTuple.Item2; bool stringifySuccess = kvpStrTuple.Item3; // // Final Validation // if (!stringifySuccess) { mLogger.Log(methodName, "Error !! The list argument (Clever parameter items) contains at least one invalid value.", 1); throw new System.ArgumentException(string.Format("The list argument (Clever parameter items) contains at least one invalid value. For more details, see log at: {0}", mLogger.mLogFile)); } if (kvpList.Count < 1) { mLogger.Log(methodName, "Error !! The list argument (Clever parameter items) contains NO items.", 1); throw new System.ArgumentException(string.Format("The list argument (Clever parameter items) contains NO items.")); } if (kvpStr.Contains("include=")) { mLogger.Log(methodName, "Error !! The Clever 'include' item (second-level endpoint) is NOT valid when using request SubTypes.", 1); throw new System.ArgumentException("The Clever 'include' item (second-level endpoint) is NOT valid when using request SubTypes."); } // The assumption is that if we have an argument for cleverRequestSubType, then there WILL be an 'id' as part of the kvp's if (string.IsNullOrEmpty(id)) { mLogger.Log(methodName, "Error !! The list argument (Clever parameter items) does NOT contain 'id'. The 'id' must be included when using cleverRequestSubType.", 1); throw new System.ArgumentException(string.Format("The list argument (Clever parameter items) does NOT contain 'id'. The 'id' must be included when using cleverRequestSubType.")); } mLogger.Log(methodName, string.Format("Successfully validated list of KeyValuePairs and converted to a string: {0}", kvpStr), 1); // need to look at reqSubkey and determine what conditions where we don't need to append an "s" if (requestSubType == cleverRequestSubType.district || (requestType == cleverRequestType.teacher && requestSubType == cleverRequestSubType.school) || (requestType == cleverRequestType.student && requestSubType == cleverRequestSubType.school) || (requestType == cleverRequestType.section && requestSubType == cleverRequestSubType.school) || (requestType == cleverRequestType.section && requestSubType == cleverRequestSubType.teacher) || (requestType == cleverRequestType.district && requestSubType == cleverRequestSubType.status)) { pluralItems = ""; } // need to identify what type of object we will look for in parser string targetObjectType = requestSubType.ToString().ToLower() + "s"; if (requestSubType.ToString().ToLower() == "status") { targetObjectType = requestSubType.ToString().ToLower(); } if (string.IsNullOrEmpty(kvpStr)) { url = string.Format(@"https://api.clever.com/v1.1/{0}s/{1}/{2}{3}", requestType, id, requestSubType.ToString().ToLower(), pluralItems); } else { url = string.Format(@"https://api.clever.com/v1.1/{0}s/{1}/{2}{3}?{4}", requestType, id, requestSubType.ToString().ToLower(), pluralItems, kvpStr); } mLogger.Log(methodName, "url: " + url, 2); // Send built URl to Clever and hopefully, we get a message containing the expected data string rawData = GetRawDataFromClever(url); if (string.IsNullOrEmpty(rawData)) { string errMsg = "The message from Clever appears to be empty or was not properly extracted from the stream. It may also indicate a network connection error."; mLogger.Log(methodName, "Unexpected Error !! " + errMsg, 1); throw new System.ApplicationException(errMsg); } // The parser will translate the JSON msg and generate the Clever objects (Students, Teachers, ect) all contained in the parent object - wrappedData Parser parser = new Parser(mLogger); wrappedData = parser.ParseJsonMsg(rawData, targetObjectType); return(wrappedData); }
public WrappedData DataRequest(cleverRequestType requestType, cleverRequestSubType requestSubType, List<KeyValuePair<String, String>> kvpList) { string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name + "_3"; /////////////////////////////////////////////////////////////////////////////////////////////////////// //// Example for "https://api.clever.com/v1.1/schools/530e595026403103360ff9fd/students?limit=120" //// //// Example for "https://api.clever.com/v1.1/schools/530e595026403103360ff9fd/students" //// /////////////////////////////////////////////////////////////////////////////////////////////////////// //string requestSubTypeStr = requestSubType.ToString().ToLower(); string msg = string.Format("reqType: {0} :: reqSubType: {1} :: List<kvp> count: {2}", requestType, requestSubType, kvpList.Count); mLogger.Log(methodName, msg, 1); WrappedData wrappedData = new WrappedData(); string pluralItems = "s"; string url = string.Empty; Tuple<string, string, bool> kvpStrTuple = mHelper.ConvertKvpToStringAndValidate(mLogger, kvpList); string kvpStr = kvpStrTuple.Item1; string id = kvpStrTuple.Item2; bool stringifySuccess = kvpStrTuple.Item3; // // Final Validation // if (!stringifySuccess) { mLogger.Log(methodName, "Error !! The list argument (Clever parameter items) contains at least one invalid value.", 1); throw new System.ArgumentException(string.Format("The list argument (Clever parameter items) contains at least one invalid value. For more details, see log at: {0}", mLogger.mLogFile)); } if (kvpList.Count < 1) { mLogger.Log(methodName, "Error !! The list argument (Clever parameter items) contains NO items.", 1); throw new System.ArgumentException(string.Format("The list argument (Clever parameter items) contains NO items.")); } if (kvpStr.Contains("include=")) { mLogger.Log(methodName, "Error !! The Clever 'include' item (second-level endpoint) is NOT valid when using request SubTypes.", 1); throw new System.ArgumentException("The Clever 'include' item (second-level endpoint) is NOT valid when using request SubTypes."); } // The assumption is that if we have an argument for cleverRequestSubType, then there WILL be an 'id' as part of the kvp's if (string.IsNullOrEmpty(id)) { mLogger.Log(methodName, "Error !! The list argument (Clever parameter items) does NOT contain 'id'. The 'id' must be included when using cleverRequestSubType.", 1); throw new System.ArgumentException(string.Format("The list argument (Clever parameter items) does NOT contain 'id'. The 'id' must be included when using cleverRequestSubType.")); } mLogger.Log(methodName, string.Format("Successfully validated list of KeyValuePairs and converted to a string: {0}", kvpStr), 1); // need to look at reqSubkey and determine what conditions where we don't need to append an "s" if (requestSubType == cleverRequestSubType.district || (requestType == cleverRequestType.teacher && requestSubType == cleverRequestSubType.school) || (requestType == cleverRequestType.student && requestSubType == cleverRequestSubType.school) || (requestType == cleverRequestType.section && requestSubType == cleverRequestSubType.school) || (requestType == cleverRequestType.section && requestSubType == cleverRequestSubType.teacher) || (requestType == cleverRequestType.district && requestSubType == cleverRequestSubType.status)) { pluralItems = ""; } // need to identify what type of object we will look for in parser string targetObjectType = requestSubType.ToString().ToLower() + "s"; if (requestSubType.ToString().ToLower() == "status") targetObjectType = requestSubType.ToString().ToLower(); if (string.IsNullOrEmpty(kvpStr)) { url = string.Format(@"https://api.clever.com/v1.1/{0}s/{1}/{2}{3}", requestType, id, requestSubType.ToString().ToLower(), pluralItems); } else { url = string.Format(@"https://api.clever.com/v1.1/{0}s/{1}/{2}{3}?{4}", requestType, id, requestSubType.ToString().ToLower(), pluralItems, kvpStr); } mLogger.Log(methodName, "url: " + url, 2); // Send built URl to Clever and hopefully, we get a message containing the expected data string rawData = GetRawDataFromClever(url); if (string.IsNullOrEmpty(rawData)) { string errMsg = "The message from Clever appears to be empty or was not properly extracted from the stream. It may also indicate a network connection error."; mLogger.Log(methodName, "Unexpected Error !! " + errMsg, 1); throw new System.ApplicationException(errMsg); } // The parser will translate the JSON msg and generate the Clever objects (Students, Teachers, ect) all contained in the parent object - wrappedData Parser parser = new Parser(mLogger); wrappedData = parser.ParseJsonMsg(rawData, targetObjectType); return wrappedData; }