/// <summary>
 /// Check validity of configuration.
 /// </summary>
 /// <param name="config">Query configuration.</param>
 protected void ValidateConfig(LinkedInDataConfig config)
 {
     if (config?.Query == null)
     {
         throw new ConfigParameterNullException(nameof(config.Query));
     }
 }
Exemple #2
0
        /// <summary>
        /// Request list data from service provider based upon a given config / query.
        /// </summary>
        /// <typeparam name="T">Strong type of model.</typeparam>
        /// <param name="config">LinkedInDataConfig instance.</param>
        /// <param name="maxRecords">Upper limit of records to return.</param>
        /// <param name="startRecord">Index of paged results.</param>
        /// <param name="fields">A comma seperated string of required fields, which will have strongly typed representation in the model passed in.</param>
        /// <returns>Strongly typed list of data returned from the service.</returns>
        public async Task <List <T> > RequestAsync <T>(LinkedInDataConfig config, int maxRecords = 20, int startRecord = 0, string fields = "id")
        {
            List <T> queryResults = new List <T>();

            var results = await Provider.GetDataAsync <T>(config, maxRecords, startRecord, fields);

            foreach (var result in results)
            {
                queryResults.Add(result);
            }

            return(queryResults);
        }
        /// <summary>
        /// Wrapper around REST API for making data request.
        /// </summary>
        /// <typeparam name="TSchema">Schema to use</typeparam>
        /// <param name="config">Query configuration.</param>
        /// <param name="maxRecords">Upper limit for records returned.</param>
        /// <param name="startRecord">Index of paged results.</param>
        /// <param name="fields">A comma seperated string of required fields, which will have strongly typed representation in the model passed in.</param>
        /// <returns>Strongly typed list of results.</returns>
        public async Task <IEnumerable <TSchema> > GetDataAsync <TSchema>(LinkedInDataConfig config, int maxRecords, int startRecord = 0, string fields = "id")
        {
            var parser = new LinkedInParser <TSchema>();

            var url = $"{_baseUrl}{config.Query}/~:({fields})?oauth2_access_token={Tokens.AccessToken}&format=json&count={maxRecords}&start={startRecord}";

            using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, new Uri(url)))
            {
                request.Headers.Connection.TryParseAdd("Keep-Alive");

                using (var response = await client.SendAsync(request).ConfigureAwait(false))
                {
                    var data = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                    if (response.IsSuccessStatusCode && !string.IsNullOrEmpty(data))
                    {
                        return(parser.Parse(data));
                    }

                    throw new RequestFailedException((System.Net.HttpStatusCode)response.StatusCode, data);
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// Wrapper around REST API for making data request.
        /// </summary>
        /// <typeparam name="TSchema">Schema to use</typeparam>
        /// <param name="config">Query configuration.</param>
        /// <param name="maxRecords">Upper limit for records returned.</param>
        /// <param name="startRecord">Index of paged results.</param>
        /// <param name="fields">A comma seperated string of required fields, which will have strongly typed representation in the model passed in.</param>
        /// <returns>Strongly typed list of results.</returns>
        public async Task <IEnumerable <TSchema> > GetDataAsync <TSchema>(LinkedInDataConfig config, int maxRecords, int startRecord = 0, string fields = "id")
        {
            var parser = new LinkedInParser <TSchema>();

            var url = $"{_baseUrl}{config.Query}/~:({fields})?oauth2_access_token={this.Tokens.AccessToken}&format=json&count={maxRecords}&start={startRecord}";

            using (var httpClient = new HttpClient())
            {
                var httpRequestMessage = new HttpRequestMessage
                {
                    Method     = HttpMethod.Get,
                    RequestUri = new Uri(url)
                };

                httpRequestMessage.Headers.Add("Connection", "Keep-Alive");

                var response = await httpClient.SendRequestAsync(httpRequestMessage);

                var jsonString = await response.Content.ReadAsStringAsync();

                return(parser.Parse(jsonString));
            }
        }