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
Nested Classes -
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
ConstructorsModifierConstructorDescriptionBasicApiConnection(String apiBaseUrl) Creates an object to manage a connection to the Web API of a Wikibase site.protectedBasicApiConnection(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 TypeMethodDescriptionvoidClears the set of cookies.voidclientLogin(String username, String password) Logs in using the main user credentials.protected voidconfirmClientLogin(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 voidconfirmLogin(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.BuilderSubclasses can customize their ownOkHttpClient.Builderinstances.Returns the map of cookies currently used in this connection.static BasicApiConnectionCreates an API connection to test.wikidata.org.static BasicApiConnectionCreates an API connection to wikidata.org.static BasicApiConnectionCreates an API connection to commons.wikimedia.org.voidLogs in using the specified user credentials.protected voidlogin(String username, String password, org.wikidata.wdtk.wikibaseapi.BasicApiConnection.ILogin loginFunction) Login function that contains token logic and a function as parametervoidlogout()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:ApiConnectionSubclasses can customize their ownOkHttpClient.Builderinstances. An example:return new OkHttpClient.Builder() .connectTimeout(5, TimeUnit.MILLISECONDS) .readTimeout(5, TimeUnit.MILLISECONDS) .cookieJar(...);- Specified by:
getClientBuilderin 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:
IOExceptionLoginFailedExceptionMediaWikiApiErrorException
-
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:
IOExceptionLoginFailedExceptionMediaWikiApiErrorException
-
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:
logoutin classApiConnection- Throws:
IOExceptionMediaWikiApiErrorException
-