Package org.wikidata.wdtk.wikibaseapi
Class BasicApiConnection
java.lang.Object
org.wikidata.wdtk.wikibaseapi.ApiConnection
org.wikidata.wdtk.wikibaseapi.BasicApiConnection
A connection to the MediaWiki API established via
standard login with username and password.
- Author:
- Antonin Delpeuch
-
Nested Class Summary
-
Field Summary
Fields inherited from class org.wikidata.wdtk.wikibaseapi.ApiConnection
apiBaseUrl, ASSERT_PARAMETER, connectTimeout, loggedIn, PARAM_ACTION, PARAM_FORMAT, readTimeout, tokens, URL_TEST_WIKIDATA_API, URL_WIKIDATA_API, URL_WIKIMEDIA_COMMONS_API, URLENCODED_MEDIA_TYPE, username
-
Constructor Summary
ModifierConstructorDescriptionBasicApiConnection
(String apiBaseUrl) Creates an object to manage a connection to the Web API of a Wikibase site.protected
BasicApiConnection
(String apiBaseUrl, List<BasicApiConnection.HttpCookieWrapper> cookies, String username, boolean loggedIn, Map<String, String> tokens, int connectTimeout, int readTimeout) Deserializes an existing BasicApiConnection from JSON. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Clears the set of cookies.void
clientLogin
(String username, String password) Logs in using the main user credentials.protected void
confirmClientLogin
(String token, String username, String password) Issues a Web API query to confirm that the previous client login attempt was successful, and sets the internal state of the API connection accordingly in this case.protected void
confirmLogin
(String token, String username, String password) Issues a Web API query to confirm that the previous login attempt was successful, and sets the internal state of the API connection accordingly in this case.protected okhttp3.OkHttpClient.Builder
Subclasses can customize their ownOkHttpClient.Builder
instances.Returns the map of cookies currently used in this connection.static BasicApiConnection
Creates an API connection to test.wikidata.org.static BasicApiConnection
Creates an API connection to wikidata.org.static BasicApiConnection
Creates an API connection to commons.wikimedia.org.void
Logs in using the specified user credentials.protected void
login
(String username, String password, org.wikidata.wdtk.wikibaseapi.BasicApiConnection.ILogin loginFunction) Login function that contains token logic and a function as parametervoid
logout()
Logs the current user out.Methods inherited from class org.wikidata.wdtk.wikibaseapi.ApiConnection
checkCredentials, checkErrors, getApiBaseUrl, getConnectTimeout, getCurrentUser, getReadTimeout, getTokens, implodeObjects, isLoggedIn, logWarnings, sendJsonRequest, sendJsonRequest, sendRequest, setConnectTimeout, setReadTimeout
-
Constructor Details
-
BasicApiConnection
Creates an object to manage a connection to the Web API of a Wikibase site.- Parameters:
apiBaseUrl
- base URI to the API, e.g., "https://www.wikidata.org/w/api.php/"
-
BasicApiConnection
protected BasicApiConnection(String apiBaseUrl, List<BasicApiConnection.HttpCookieWrapper> cookies, String username, boolean loggedIn, Map<String, String> tokens, int connectTimeout, int readTimeout) Deserializes an existing BasicApiConnection from JSON.- Parameters:
apiBaseUrl
- base URL of the API to use, e.g. "https://www.wikidata.org/w/api.php/"cookies
- map of cookies used for this sessionusername
- name of the current userloggedIn
- true if login succeeded.tokens
- map of tokens used for this sessionconnectTimeout
- the maximum time to wait for when establishing a connection, in millisecondsreadTimeout
- the maximum time to wait for a server response once the connection was established, in milliseconds
-
-
Method Details
-
getClientBuilder
protected okhttp3.OkHttpClient.Builder getClientBuilder()Description copied from class:ApiConnection
Subclasses can customize their ownOkHttpClient.Builder
instances. An example:return new OkHttpClient.Builder() .connectTimeout(5, TimeUnit.MILLISECONDS) .readTimeout(5, TimeUnit.MILLISECONDS) .cookieJar(...);
- Specified by:
getClientBuilder
in classApiConnection
-
getTestWikidataApiConnection
Creates an API connection to test.wikidata.org.- Returns:
BasicApiConnection
-
getWikidataApiConnection
Creates an API connection to wikidata.org.- Returns:
BasicApiConnection
-
getWikimediaCommonsApiConnection
Creates an API connection to commons.wikimedia.org.- Returns:
BasicApiConnection
-
login
Logs in using the specified user credentials. After successful login, the API connection remains in a logged in state, and future actions will be run as a logged in user.- Parameters:
username
- the name of the user to log inpassword
- the password of the user- Throws:
LoginFailedException
- if the login failed for some reason
-
clientLogin
Logs in using the main user credentials. After successful login, the API connection remains in a logged in state, and future actions will be run as a logged in user.- Parameters:
username
- the name of the main user to log inpassword
- the password of the main user- Throws:
LoginFailedException
- if the login failed for some reason
-
login
protected void login(String username, String password, org.wikidata.wdtk.wikibaseapi.BasicApiConnection.ILogin loginFunction) throws LoginFailedException Login function that contains token logic and a function as parameter- Parameters:
username
- the name of the user to log inpassword
- the password of the userloginFunction
- the functional interface to log in with- Throws:
LoginFailedException
- if the login failed for some reason
-
confirmLogin
protected void confirmLogin(String token, String username, String password) throws IOException, LoginFailedException, MediaWikiApiErrorException Issues a Web API query to confirm that the previous login attempt was successful, and sets the internal state of the API connection accordingly in this case.- Parameters:
token
- the login token stringusername
- the name of the user that was logged inpassword
- the password used to log in- Throws:
IOException
LoginFailedException
MediaWikiApiErrorException
-
confirmClientLogin
protected void confirmClientLogin(String token, String username, String password) throws IOException, LoginFailedException, MediaWikiApiErrorException Issues a Web API query to confirm that the previous client login attempt was successful, and sets the internal state of the API connection accordingly in this case.- Parameters:
token
- the login token stringusername
- the name of the main user that was logged inpassword
- the password used to log in- Throws:
IOException
LoginFailedException
MediaWikiApiErrorException
-
getCookies
Returns the map of cookies currently used in this connection. -
clearCookies
Clears the set of cookies. This will cause a logout. -
logout
Logs the current user out.- Specified by:
logout
in classApiConnection
- Throws:
IOException
MediaWikiApiErrorException
-