Пример #1
0
        public List<JobPost> GetJobs(FilterBag filterbag, int page, int resultsPerPage)
        {
            // Short circuit if there are no filters specified
            if (  filterbag.IsEmpty()  ) {  return new List<JobPost>();   }

            // Will try and build an Indeed API request from the given set of filters. Catches and logs any problems
            string request = "";
            try
            {
                // Build the request based on filters
                request = builder.buildQuery(filterbag, page, resultsPerPage);

                // If the request comes back empty, something bad happened
                if (String.IsNullOrEmpty(request)) { throw new ArgumentException(); }

            }

            // The built request is, for some reason, empty or null. Return empty results list
            catch (ArgumentException argex)
            {
                Logging.JobSaltLogger.Instance.log("(Indeed) Error in IndeedQueryBuilder caused API request string to be empty or null.");
                Logging.JobSaltLogger.Instance.log(filterbag.ToString() + "\n Page=" + page + "\n resultsPerPage=" + resultsPerPage);
                return new List<JobPost>();
            }

            // An unknown exception was thrown when trying to build the request. The request string
            // is empty now, so there's no point trying to continue (since it will return an empty list anyway)
            catch (Exception e)
            {
                Logging.JobSaltLogger.Instance.log("(Indeed) Exception caught while building Indeed Query: " + e.Message);
                Logging.JobSaltLogger.Instance.log(filterbag.ToString() + "\n Page=" + page + "\n resultsPerPage=" + resultsPerPage);
                return new List<JobPost>();
            }

            IndeedResult iResult;   // Raw Indeed results
            using (var client = new WebClient())
            {
                string json = client.DownloadString(request);   // Issues a Get to the Indeed API with the request string

                try
                {

                    var serializer = new JavaScriptSerializer();
                    iResult = serializer.Deserialize<IndeedResult>(json);   // Parses JSON result into C# Indeed data object

                    // The raw Indeed results are null... something very bad happened!
                    if (null == iResult) { throw new ArgumentException();}

                    // This is used to make sure that if a higher page is requested than Indeed has,
                    // then the last several posts don't keep getting returned (fixed bug)
                    int startpost = resultsPerPage * page;

                    return IndeedResultToJobPosts(iResult, startpost); // Parses C# Indeed data object into a list of JobPosts

                }

                // iResult is null, which means something went very wrong. Return an empty job list
                catch (ArgumentException argex)
                {
                    Logging.JobSaltLogger.Instance.log("(Indeed) An error occured when parsing Indeed JSON into iResult, resulting in iResult being null: " + argex.Message);
                    Logging.JobSaltLogger.Instance.log("(Indeed) JSON: \n" + json);
                    return new List<JobPost>();
                }

                // An unknown exception occured
                catch (Exception e)
                {
                    Logging.JobSaltLogger.Instance.log("(Indeed) An exception occured when parsing Indeed JSON into iResult: " + e.Message);
                    Logging.JobSaltLogger.Instance.log("(Indeed) JSON: \n" + json);
                    return new List<JobPost>();
                }
            }
        }