public WrappedData DataRequest(cleverRequestType requestType) { string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name + "_1"; ////////////////////////////////////////////////////////////// //// Example for "https://api.clever.com/v1.1/students" //// ////////////////////////////////////////////////////////////// string msg = string.Format("reqType: {0}", requestType); mLogger.Log(methodName, msg, 1); string cleverObjType = requestType.ToString().ToLower() + "s"; // toLower() used to handle Events WrappedData wrappedData = new WrappedData(); string url = string.Format(@"https://api.clever.com/v1.1/{0}s", requestType.ToString().ToLower()); // toLower() used to handle Events 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)) { mLogger.Log(methodName, "Unexpected Error !! The message from Clever appears to be empty or was not properly extracted from the stream.", 1); throw new System.ApplicationException("The message from Clever appears to be empty or was not properly extracted from the stream."); } // 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, cleverObjType); return wrappedData; }
public WrappedData DataRequest(cleverRequestType requestType, List<KeyValuePair<String, String>> kvpList) { /////////////////////////////////////////////////////////////////////////////////////////////////////////////// //// Example for "https://api.clever.com/v1.1/districts/4fd43cc56d11340000000005?include=schools,teachers" //// //// Example for "https://api.clever.com/v1.1/students?limit=120" //// //// Example for "https://api.clever.com/v1.1/districts/4fd43cc56d11340000000005" //// /////////////////////////////////////////////////////////////////////////////////////////////////////////////// string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name + "_2"; string cleverObjType = string.Empty; string url = string.Empty; string msg = string.Format("reqType: {0} :: List<string> count: {1}", requestType, kvpList.Count); mLogger.Log(methodName, msg, 1); WrappedData wrappedData = new WrappedData(); 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 (string.IsNullOrEmpty(id)) { if (kvpStr.Contains("include=")) { mLogger.Log(methodName, "Error !! The 'id' value is empty and user attempted to use Clever include item (second-level endpoint). There must be an 'id' when using 'include'.", 1); throw new System.ArgumentException("The 'id' value is empty and user attempted to use Clever include item (second-level endpoint). There must be an 'id' when using 'include'."); } } if (!kvpStr.Contains("include=")) { // We know the return object should be reqType, so pass that along in case it is needed cleverObjType = requestType.ToString().ToLower() + "s"; } mLogger.Log(methodName, string.Format("Successfully validated list of KeyValuePairs and converted to a string: {0}", kvpStr), 1); if (string.IsNullOrEmpty(id)) { url = string.Format(@"https://api.clever.com/v1.1/{0}s?{1}", requestType.ToString().ToLower(), kvpStr); } else { if (string.IsNullOrEmpty(kvpStr)) { url = string.Format(@"https://api.clever.com/v1.1/{0}s/{1}", requestType.ToString().ToLower(), id); } else { url = string.Format(@"https://api.clever.com/v1.1/{0}s/{1}?{2}", requestType.ToString().ToLower(), id, 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)) { mLogger.Log(methodName, "Unexpected Error !! The message from Clever appears to be empty or was not properly extracted from the stream.", 1); throw new System.ApplicationException("The message from Clever appears to be empty or was not properly extracted from the stream."); } // 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, cleverObjType); 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; }