Class BasicApiConnection

java.lang.Object
org.wikidata.wdtk.wikibaseapi.ApiConnection
org.wikidata.wdtk.wikibaseapi.BasicApiConnection

public class BasicApiConnection extends ApiConnection
A connection to the MediaWiki API established via standard login with username and password.
Author:
Antonin Delpeuch
  • Constructor Details

    • BasicApiConnection

      public BasicApiConnection(String apiBaseUrl)
      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 session
      username - name of the current user
      loggedIn - true if login succeeded.
      tokens - map of tokens used for this session
      connectTimeout - the maximum time to wait for when establishing a connection, in milliseconds
      readTimeout - 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 own OkHttpClient.Builder instances. An example:
                  return new OkHttpClient.Builder()
                              .connectTimeout(5, TimeUnit.MILLISECONDS)
                              .readTimeout(5, TimeUnit.MILLISECONDS)
                              .cookieJar(...);
       
      Specified by:
      getClientBuilder in class ApiConnection
    • getTestWikidataApiConnection

      public static BasicApiConnection getTestWikidataApiConnection()
      Creates an API connection to test.wikidata.org.
      Returns:
      BasicApiConnection
    • getWikidataApiConnection

      public static BasicApiConnection getWikidataApiConnection()
      Creates an API connection to wikidata.org.
      Returns:
      BasicApiConnection
    • getWikimediaCommonsApiConnection

      public static BasicApiConnection getWikimediaCommonsApiConnection()
      Creates an API connection to commons.wikimedia.org.
      Returns:
      BasicApiConnection
    • login

      public void login(String username, String password) throws LoginFailedException
      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 in
      password - the password of the user
      Throws:
      LoginFailedException - if the login failed for some reason
    • clientLogin

      public void clientLogin(String username, String password) throws LoginFailedException
      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 in
      password - 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 in
      password - the password of the user
      loginFunction - 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 string
      username - the name of the user that was logged in
      password - 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 string
      username - the name of the main user that was logged in
      password - the password used to log in
      Throws:
      IOException
      LoginFailedException
      MediaWikiApiErrorException
    • getCookies

      public List<HttpCookie> getCookies()
      Returns the map of cookies currently used in this connection.
    • clearCookies

      public void clearCookies() throws IOException, MediaWikiApiErrorException
      Clears the set of cookies. This will cause a logout.
      Throws:
      IOException
      MediaWikiApiErrorException
    • logout

      public void logout() throws IOException, MediaWikiApiErrorException
      Logs the current user out.
      Specified by:
      logout in class ApiConnection
      Throws:
      IOException
      MediaWikiApiErrorException