/// <summary>
        /// Share data to LinkedIn.
        /// </summary>
        /// <typeparam name="T">Schema of data to share.</typeparam>
        /// <typeparam name="U">Type of response object.</typeparam>
        /// <param name="dataToShare">Share request content.</param>
        /// <returns>Boolean indicating success or failure.</returns>
        public async Task <U> ShareDataAsync <T, U>(T dataToShare)
        {
            var shareRequest = dataToShare as LinkedInShareRequest;

            if (shareRequest != null)
            {
                LinkedInVisibility.ParseVisibilityStringToEnum(shareRequest.Visibility.Code);

                var requestParser = new LinkedInParser <LinkedInShareRequest>();

                var url = $"{_baseUrl}/people/~/shares?oauth2_access_token={Tokens.AccessToken}&format=json";

                using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(url)))
                {
                    request.Headers.Add("x-li-format", "json");
                    var stringContent = requestParser.Parse(shareRequest);
                    request.Content = new StringContent(stringContent, Encoding.UTF8, "application/json");

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

                        var responseParser = new LinkedInParser <U>();

                        var listResults = responseParser.Parse(data) as List <U>;
                        return(listResults[0]);
                    }
                }
            }

            return(default(U));
        }
        /// <summary>
        /// Share data to LinkedIn.
        /// </summary>
        /// <typeparam name="T">Schema of data to share.</typeparam>
        /// <typeparam name="U">Type of response object.</typeparam>
        /// <param name="dataToShare">Share request content.</param>
        /// <returns>Boolean indicating success or failure.</returns>
        public async Task <U> ShareDataAsync <T, U>(T dataToShare)
        {
            var shareRequest = dataToShare as LinkedInShareRequest;

            if (shareRequest != null)
            {
                LinkedInVisibility.ParseVisibilityStringToEnum(shareRequest.Visibility.Code);

                var requestParser = new LinkedInParser <LinkedInShareRequest>();

                var url = $"{_baseUrl}/people/~/shares?oauth2_access_token={Tokens.AccessToken}&format=json";

                using (HttpHelperRequest request = new HttpHelperRequest(new Uri(url), HttpMethod.Post))
                {
                    request.Headers["x-li-format"] = "json";

                    var stringContent = requestParser.Parse(shareRequest);
                    request.Content = new HttpStringContent(stringContent, Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");

                    using (var response = await HttpHelper.Instance.SendRequestAsync(request).ConfigureAwait(false))
                    {
                        var data = await response.GetTextResultAsync().ConfigureAwait(false);

                        var responseParser = new LinkedInParser <U>();

                        var listResults = responseParser.Parse(data) as List <U>;
                        return(listResults[0]);
                    }
                }
            }

            return(default(U));
        }
Esempio n. 3
0
        /// <summary>
        /// Share data to LinkedIn.
        /// </summary>
        /// <typeparam name="T">Schema of data to share.</typeparam>
        /// <typeparam name="U">Type of response object.</typeparam>
        /// <param name="dataToShare">Share request content.</param>
        /// <returns>Boolean indicating success or failure.</returns>
        public async Task <U> ShareDataAsync <T, U>(T dataToShare)
        {
            var shareRequest = dataToShare as LinkedInShareRequest;

            if (shareRequest != null)
            {
                LinkedInVisibility.ParseVisibilityStringToEnum(shareRequest.Visibility.Code);

                var requestParser = new LinkedInParser <LinkedInShareRequest>();

                var url = $"{_baseUrl}/people/~/shares?oauth2_access_token={this.Tokens.AccessToken}&format=json";

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

                    httpRequestMessage.Headers.Add("x-li-format", "json");

                    var stringContent = requestParser.Parse(shareRequest);
                    httpRequestMessage.Content = new HttpStringContent(stringContent, Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");

                    var response = await httpClient.SendRequestAsync(httpRequestMessage);

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

                    var responseParser = new LinkedInParser <U>();

                    var listResults = responseParser.Parse(jsonString.ToString()) as List <U>;
                    return(listResults[0]);
                }
            }

            return(default(U));
        }
        /// <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);
                }
            }
        }
Esempio n. 5
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));
            }
        }