public abstract class ApiConnection extends Object
Modifier and Type | Field and Description |
---|---|
protected String |
apiBaseUrl
URL to access the Wikibase API.
|
protected static String |
ASSERT_PARAMETER
MediaWiki assert parameter to ensure we are editing while logged in.
|
protected int |
connectTimeout
Maximum time to wait for when establishing a connection, in milliseconds.
|
protected boolean |
loggedIn
True after successful login.
|
protected static String |
PARAM_ACTION
Name of the HTTP parameter to submit an action to the API.
|
protected static String |
PARAM_FORMAT
Name of the HTTP parameter to submit the requested result format to the
API.
|
protected int |
readTimeout
Maximum time to wait for a server response once the connection was established.
|
protected Map<String,String> |
tokens
Map of requested tokens.
|
static String |
URL_TEST_WIKIDATA_API
URL of the API of test.wikidata.org.
|
static String |
URL_WIKIDATA_API
URL of the API of wikidata.org.
|
static String |
URL_WIKIMEDIA_COMMONS_API
URL of the API of commons.wikimedia.org.
|
protected static okhttp3.MediaType |
URLENCODED_MEDIA_TYPE |
protected String |
username
User name used to log in.
|
Constructor and Description |
---|
ApiConnection(String apiBaseUrl)
Creates an object to manage a connection to the Web API of a Wikibase
site.
|
ApiConnection(String apiBaseUrl,
Map<String,String> tokens)
Creates an object to manage a connection to the Web API of a Wikibase
site.
|
Modifier and Type | Method and Description |
---|---|
void |
checkCredentials()
Checks that the credentials are still valid for the
user currently logged in.
|
protected void |
checkErrors(com.fasterxml.jackson.databind.JsonNode root)
Checks if an API response contains an error and throws a suitable
exception in this case.
|
String |
getApiBaseUrl()
Getter for the apiBaseUrl.
|
protected abstract okhttp3.OkHttpClient.Builder |
getClientBuilder()
Subclasses can customize their own
OkHttpClient.Builder instances. |
int |
getConnectTimeout()
Maximum time to wait for when establishing a connection, in milliseconds.
|
String |
getCurrentUser()
Returns the username of the user who is currently logged in.
|
int |
getReadTimeout()
Maximum time to wait for a server response once the connection was established.
|
Map<String,String> |
getTokens()
Returns the map of tokens (such as csrf token and login token) currently used in this connection.
|
static String |
implodeObjects(Iterable<?> objects)
Builds a string that serializes a list of objects separated by the pipe
character.
|
boolean |
isLoggedIn()
Returns true if a user is logged in.
|
abstract void |
logout()
Logs the current user out.
|
protected void |
logWarnings(com.fasterxml.jackson.databind.JsonNode root)
Extracts and logs any warnings that are returned in an API response.
|
com.fasterxml.jackson.databind.JsonNode |
sendJsonRequest(String requestMethod,
Map<String,String> parameters)
Sends a request to the API with the given parameters and the given
request method and returns the result JSON tree.
|
com.fasterxml.jackson.databind.JsonNode |
sendJsonRequest(String requestMethod,
Map<String,String> parameters,
Map<String,org.apache.commons.lang3.tuple.ImmutablePair<String,File>> files)
Sends a request to the API with the given parameters and the given
request method and returns the result JSON tree.
|
InputStream |
sendRequest(String requestMethod,
Map<String,String> parameters,
Map<String,org.apache.commons.lang3.tuple.ImmutablePair<String,File>> files)
Sends a request to the API with the given parameters and the given
request method and returns the result string.
|
void |
setConnectTimeout(int timeout)
Sets the maximum time to wait for when establishing a connection, in milliseconds.
|
void |
setReadTimeout(int timeout)
Sets the maximum time to wait for a server response once the connection was established, in milliseconds.
|
public static final String URL_WIKIDATA_API
public static final String URL_TEST_WIKIDATA_API
public static final String URL_WIKIMEDIA_COMMONS_API
protected static final String PARAM_ACTION
protected static final String PARAM_FORMAT
protected static final String ASSERT_PARAMETER
protected static final okhttp3.MediaType URLENCODED_MEDIA_TYPE
protected final String apiBaseUrl
protected boolean loggedIn
protected String username
protected int connectTimeout
protected int readTimeout
public ApiConnection(String apiBaseUrl)
apiBaseUrl
- base URI to the API, e.g.,
"https://www.wikidata.org/w/api.php/"public ApiConnection(String apiBaseUrl, Map<String,String> tokens)
apiBaseUrl
- base URI to the API, e.g.,
"https://www.wikidata.org/w/api.php/"tokens
- CSRF tokens already acquired by the connectionprotected abstract okhttp3.OkHttpClient.Builder getClientBuilder()
OkHttpClient.Builder
instances.
An example:
return new OkHttpClient.Builder() .connectTimeout(5, TimeUnit.MILLISECONDS) .readTimeout(5, TimeUnit.MILLISECONDS) .cookieJar(...);
public String getApiBaseUrl()
public boolean isLoggedIn()
checkCredentials()
.public void checkCredentials() throws IOException, MediaWikiApiErrorException
AssertUserFailedException
if the check failed. This does not update the state of the connection
object.public String getCurrentUser()
public Map<String,String> getTokens()
public void setConnectTimeout(int timeout)
URLConnection.setConnectTimeout(int)
public void setReadTimeout(int timeout)
URLConnection.setReadTimeout(int)
public int getConnectTimeout()
URLConnection.getConnectTimeout()
public int getReadTimeout()
URLConnection.getReadTimeout()
public abstract void logout() throws IOException, MediaWikiApiErrorException
public com.fasterxml.jackson.databind.JsonNode sendJsonRequest(String requestMethod, Map<String,String> parameters) throws IOException, MediaWikiApiErrorException
requestMethod
- either POST or GETparameters
- Maps parameter keys to values. Out of this map the function
will create a query string for the request.IOException
MediaWikiApiErrorException
- if the API returns an errorpublic com.fasterxml.jackson.databind.JsonNode sendJsonRequest(String requestMethod, Map<String,String> parameters, Map<String,org.apache.commons.lang3.tuple.ImmutablePair<String,File>> files) throws IOException, MediaWikiApiErrorException
requestMethod
- either POST or GETparameters
- Maps parameter keys to values. Out of this map the function
will create a query string for the request.files
- If GET, this should be null. If POST, this can contain
a list of files to upload, indexed by the parameter to pass them with.
The first component of the pair is the filename exposed to the server,
and the second component is the path to the local file to upload.
Set to null or empty map to avoid uploading any file.IOException
MediaWikiApiErrorException
- if the API returns an errorpublic InputStream sendRequest(String requestMethod, Map<String,String> parameters, Map<String,org.apache.commons.lang3.tuple.ImmutablePair<String,File>> files) throws IOException
requestMethod
- either POST or GETparameters
- Maps parameter keys to values. Out of this map the function
will create a query string for the request.files
- If GET, this should be null. If POST, this can contain
a list of files to upload, indexed by the parameter to pass them with.
The first component of the pair is the filename exposed to the server,
and the second component is the path to the local file to upload.
Set to null or empty map to avoid uploading any file.IOException
protected void checkErrors(com.fasterxml.jackson.databind.JsonNode root) throws MediaWikiApiErrorException
root
- root node of the JSON resultMediaWikiApiErrorException
protected void logWarnings(com.fasterxml.jackson.databind.JsonNode root)
root
- root node of the JSON resultpublic static String implodeObjects(Iterable<?> objects)
objects
- the objects to implodeCopyright © 2014–2024 Wikidata Toolkit Developers. Generated from source code published under the Apache License 2.0. For more information, see the Wikidata Toolkit homepage