コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GoodreadsClient"/> class.
        /// Use this constructor if you already have OAuth permissions for the user.
        /// </summary>
        /// <param name="apiKey">Your Goodreads API key.</param>
        /// <param name="apiSecret">Your Goodreads API secret.</param>
        /// <param name="accessToken">The user's OAuth access token.</param>
        /// <param name="accessSecret">The user's OAuth access secret.</param>
        public GoodreadsClient(string apiKey, string apiSecret, string accessToken, string accessSecret)
        {
            var client = new RestClient(new Uri(GoodreadsUrl))
            {
                UserAgent = "goodreads-dotnet"
            };

            client.AddDefaultParameter("key", apiKey, ParameterType.QueryString);
            client.AddDefaultParameter("format", "xml", ParameterType.QueryString);

            var apiCredentials = new ApiCredentials(client, apiKey, apiSecret, accessToken, accessSecret);

            // Setup the OAuth authenticator if they have passed on the appropriate tokens
            if (!string.IsNullOrWhiteSpace(accessToken) &&
                !string.IsNullOrWhiteSpace(accessSecret))
            {
                client.Authenticator = OAuth1Authenticator.ForProtectedResource(
                    apiKey, apiSecret, accessToken, accessSecret);
            }

            Connection = new Connection(client, apiCredentials);
            Authors = new AuthorsClient(Connection);
            Books = new BooksClient(Connection);
            Shelves = new ShelvesClient(Connection);
            Users = new UsersClient(Connection);
            Reviews = new ReviewsClient(Connection);
            Series = new SeriesClient(Connection);
        }
コード例 #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Connection"/> class.
 /// </summary>
 /// <param name="client">A RestSharp client to use for this connection.</param>
 /// <param name="credentials">Credentials for use with the Goodreads API.</param>
 public Connection(IRestClient client, ApiCredentials credentials)
 {
     Client          = client;
     Credentials     = credentials;
     IsAuthenticated = credentials != null &&
                       !string.IsNullOrWhiteSpace(credentials.OauthToken) &&
                       !string.IsNullOrWhiteSpace(credentials.OauthTokenSecret);
 }
コード例 #3
0
        private static IRestClient CreateClient(ApiCredentials credentials)
        {
            var client = new RestClient(new Uri(GoodreadsUrl))
            {
                UserAgent = GoodreadsUserAgent
            };

            client.AddDefaultParameter("key", credentials.ApiKey, ParameterType.QueryString);
            client.AddDefaultParameter("format", "xml", ParameterType.QueryString);

            if (!string.IsNullOrEmpty(credentials.OAuthToken) && !string.IsNullOrEmpty(credentials.OAuthTokenSecret))
            {
                client.Authenticator =
                    OAuth1Authenticator.ForProtectedResource(
                        credentials.ApiKey,
                        credentials.ApiSecret,
                        credentials.OAuthToken,
                        credentials.OAuthTokenSecret);
            }

            return(client);
        }
コード例 #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Connection"/> class.
 /// </summary>
 /// <param name="client">A RestSharp client to use for this connection.</param>
 /// <param name="credentials">Credentials for use with the Goodreads API.</param>
 public Connection(ApiCredentials credentials)
 {
     _client     = CreateClient(credentials);
     Credentials = credentials;
 }