pub struct Api { /* private fields */ }
Expand description
Api
is the main class to interact with a MediaWiki API
Implementations§
source§impl Api
impl Api
sourcepub async fn new(api_url: &str) -> Result<Api, MediaWikiError>
pub async fn new(api_url: &str) -> Result<Api, MediaWikiError>
Returns a new Api
element, and loads the MediaWiki site info from the api_url
site.
This is done both to get basic information about the site, and to test the API.
§Examples
let api = mediawiki::api::Api::new("https://en.wikipedia.org/w/api.php").await.unwrap();
sourcepub async fn new_from_builder(
api_url: &str,
builder: ClientBuilder,
) -> Result<Api, MediaWikiError>
pub async fn new_from_builder( api_url: &str, builder: ClientBuilder, ) -> Result<Api, MediaWikiError>
Returns a new Api
element, and loads the MediaWiki site info from the api_url
site.
This is done both to get basic information about the site, and to test the API.
Uses a bespoke reqwest::ClientBuilder.
sourcepub fn set_oauth(&mut self, oauth: Option<OAuthParams>)
pub fn set_oauth(&mut self, oauth: Option<OAuthParams>)
Sets the OAuth parameters
sourcepub fn set_oauth2(&mut self, oauth2: &str)
pub fn set_oauth2(&mut self, oauth2: &str)
Set an OAuth 2 access token
sourcepub fn oauth(&self) -> &Option<OAuthParams>
pub fn oauth(&self) -> &Option<OAuthParams>
Returns a reference to the current OAuth parameters
sourcepub fn client_mut(&mut self) -> &mut Client
pub fn client_mut(&mut self) -> &mut Client
Returns a mutable reference to the reqwest client
sourcepub async fn load_current_user_info(&mut self) -> Result<(), MediaWikiError>
pub async fn load_current_user_info(&mut self) -> Result<(), MediaWikiError>
Loads the current user info; returns Ok(()) is successful
sourcepub fn max_retry_attempts(&self) -> u64
pub fn max_retry_attempts(&self) -> u64
Returns the maximum number of retry attempts
sourcepub fn set_max_retry_attempts(&mut self, max_retry_attempts: u64)
pub fn set_max_retry_attempts(&mut self, max_retry_attempts: u64)
Sets the maximum number of retry attempts
sourcepub fn get_site_info(&self) -> &Value
pub fn get_site_info(&self) -> &Value
Returns a reference to the serde_json Value containing the site info
sourcepub fn get_site_info_value<'a>(&'a self, k1: &str, k2: &str) -> &'a Value
pub fn get_site_info_value<'a>(&'a self, k1: &str, k2: &str) -> &'a Value
Returns a serde_json Value in site info, within the ["query"]
object.
sourcepub fn get_site_info_string<'a>(
&'a self,
k1: &str,
k2: &str,
) -> Result<&'a str, MediaWikiError>
pub fn get_site_info_string<'a>( &'a self, k1: &str, k2: &str, ) -> Result<&'a str, MediaWikiError>
Returns a String from the site info, matching ["query"][k1][k2]
sourcepub fn get_namespace_info(&self, namespace_id: NamespaceID) -> &Value
pub fn get_namespace_info(&self, namespace_id: NamespaceID) -> &Value
Returns the raw data for the namespace, matching ["query"]["namespaces"][namespace_id]
sourcepub fn get_canonical_namespace_name(
&self,
namespace_id: NamespaceID,
) -> Option<&str>
pub fn get_canonical_namespace_name( &self, namespace_id: NamespaceID, ) -> Option<&str>
Returns the canonical namespace name for a namespace ID, if defined
sourcepub fn get_local_namespace_name(
&self,
namespace_id: NamespaceID,
) -> Option<&str>
pub fn get_local_namespace_name( &self, namespace_id: NamespaceID, ) -> Option<&str>
Returns the local namespace name for a namespace ID, if defined
sourcepub fn params_into(&self, params: &[(&str, &str)]) -> HashMap<String, String>
pub fn params_into(&self, params: &[(&str, &str)]) -> HashMap<String, String>
Turns a Vec of str tuples into a Hashmap of String, to be used in API calls
sourcepub async fn get_token(
&mut self,
token_type: &str,
) -> Result<String, MediaWikiError>
pub async fn get_token( &mut self, token_type: &str, ) -> Result<String, MediaWikiError>
Returns a token of a token_type
, such as login
or csrf
(for editing)
sourcepub async fn get_edit_token(&mut self) -> Result<String, MediaWikiError>
pub async fn get_edit_token(&mut self) -> Result<String, MediaWikiError>
Calls get_token()
to return an edit token
sourcepub async fn get_query_api_json_all(
&self,
params: &HashMap<String, String>,
) -> Result<Value, MediaWikiError>
pub async fn get_query_api_json_all( &self, params: &HashMap<String, String>, ) -> Result<Value, MediaWikiError>
Same as get_query_api_json
but automatically loads all results via the continue
parameter
sourcepub async fn get_query_api_json_limit(
&self,
params: &HashMap<String, String>,
max: Option<usize>,
) -> Result<Value, MediaWikiError>
pub async fn get_query_api_json_limit( &self, params: &HashMap<String, String>, max: Option<usize>, ) -> Result<Value, MediaWikiError>
Same as get_query_api_json
but automatically loads more results via the continue
parameter
sourcepub async fn get_query_api_json_limit_iter<'a>(
&'a self,
params: &HashMap<String, String>,
max: Option<usize>,
) -> impl Stream<Item = Result<Value, MediaWikiError>> + 'a
pub async fn get_query_api_json_limit_iter<'a>( &'a self, params: &HashMap<String, String>, max: Option<usize>, ) -> impl Stream<Item = Result<Value, MediaWikiError>> + 'a
Same as get_query_api_json
but automatically loads more results via the continue
parameter.
Returns a stream; each item is a “page” of results.
sourcepub async fn query_api_json(
&self,
params: &HashMap<String, String>,
method: &str,
) -> Result<Value, MediaWikiError>
pub async fn query_api_json( &self, params: &HashMap<String, String>, method: &str, ) -> Result<Value, MediaWikiError>
Runs a query against the MediaWiki API, using method
GET or POST.
Parameters are a hashmap; format=json
is enforced.
sourcepub fn edit_delay(&self) -> &Option<u64>
pub fn edit_delay(&self) -> &Option<u64>
Returns the delay time after edits, in milliseconds, if set
sourcepub fn set_edit_delay(&mut self, edit_delay_ms: Option<u64>)
pub fn set_edit_delay(&mut self, edit_delay_ms: Option<u64>)
Sets the delay time after edits in milliseconds (or None
).
This is independent of, and additional to, MAXLAG
sourcepub fn set_maxlag(&mut self, maxlag_seconds: Option<u64>)
pub fn set_maxlag(&mut self, maxlag_seconds: Option<u64>)
Sets the maxlag in seconds (or None
)
sourcepub async fn get_query_api_json(
&self,
params: &HashMap<String, String>,
) -> Result<Value, MediaWikiError>
pub async fn get_query_api_json( &self, params: &HashMap<String, String>, ) -> Result<Value, MediaWikiError>
GET wrapper for query_api_json
sourcepub async fn post_query_api_json(
&self,
params: &HashMap<String, String>,
) -> Result<Value, MediaWikiError>
pub async fn post_query_api_json( &self, params: &HashMap<String, String>, ) -> Result<Value, MediaWikiError>
POST wrapper for query_api_json
sourcepub async fn post_query_api_json_mut(
&mut self,
params: &HashMap<String, String>,
) -> Result<Value, MediaWikiError>
pub async fn post_query_api_json_mut( &mut self, params: &HashMap<String, String>, ) -> Result<Value, MediaWikiError>
POST wrapper for query_api_json
.
Requires &mut self
, for session cookie storage
sourcepub async fn query_api_raw(
&self,
params: &HashMap<String, String>,
method: &str,
) -> Result<String, MediaWikiError>
pub async fn query_api_raw( &self, params: &HashMap<String, String>, method: &str, ) -> Result<String, MediaWikiError>
Runs a query against the MediaWiki API, and returns a text.
Uses query_raw
sourcepub fn get_api_request_builder(
&self,
params: &HashMap<String, String>,
method: &str,
) -> Result<RequestBuilder, MediaWikiError>
pub fn get_api_request_builder( &self, params: &HashMap<String, String>, method: &str, ) -> Result<RequestBuilder, MediaWikiError>
Generates a RequestBuilder
for the API URL
sourcepub fn user_agent(&self) -> &str
pub fn user_agent(&self) -> &str
Returns the user agent name
sourcepub fn set_user_agent<S: Into<String>>(&mut self, agent: S)
pub fn set_user_agent<S: Into<String>>(&mut self, agent: S)
Sets the user agent name
sourcepub fn user_agent_full(&self) -> String
pub fn user_agent_full(&self) -> String
Returns the user agent string, as it is passed to the API through a HTTP header
sourcepub async fn query_raw(
&self,
api_url: &str,
params: &HashMap<String, String>,
method: &str,
) -> Result<String, MediaWikiError>
pub async fn query_raw( &self, api_url: &str, params: &HashMap<String, String>, method: &str, ) -> Result<String, MediaWikiError>
Runs a query against a generic URL, and returns a text.
Does not store cookies, but also does not require &self
to be mutable.
Used for simple queries
sourcepub async fn login<S: Into<String>>(
&mut self,
lgname: S,
lgpassword: S,
) -> Result<(), MediaWikiError>
pub async fn login<S: Into<String>>( &mut self, lgname: S, lgpassword: S, ) -> Result<(), MediaWikiError>
Performs a login against the MediaWiki API.
If successful, user information is stored in User
, and in the cookie jar
sourcepub fn result_array_to_titles(data: &Value) -> Vec<Title>
pub fn result_array_to_titles(data: &Value) -> Vec<Title>
From an API result that has a list of entries with “title” and “ns” (e.g. search), returns a vector of Title
objects.
sourcepub async fn sparql_query(&self, query: &str) -> Result<Value, MediaWikiError>
pub async fn sparql_query(&self, query: &str) -> Result<Value, MediaWikiError>
Performs a SPARQL query against a wikibase installation. Tries to get the SPARQL endpoint URL from the site info
sourcepub async fn sparql_query_endpoint(
&self,
query: &str,
query_api_url: &str,
) -> Result<Value, MediaWikiError>
pub async fn sparql_query_endpoint( &self, query: &str, query_api_url: &str, ) -> Result<Value, MediaWikiError>
Performs a SPARQL query against a wikibase installation. Uses the given sparql endpoint
sourcepub fn extract_entity_from_uri(
&self,
uri: &str,
) -> Result<String, MediaWikiError>
pub fn extract_entity_from_uri( &self, uri: &str, ) -> Result<String, MediaWikiError>
Given a uri
(usually, an URL) that points to a Wikibase entity on this MediaWiki installation, returns the item ID
sourcepub fn entities_from_sparql_result(
&self,
sparql_result: &Value,
variable_name: &str,
) -> Vec<String>
pub fn entities_from_sparql_result( &self, sparql_result: &Value, variable_name: &str, ) -> Vec<String>
Returns a vector of entity IDs (as String) from a SPARQL result, given a variable name
sourcepub async fn load_user_info(
&self,
user: &mut User,
) -> Result<(), MediaWikiError>
pub async fn load_user_info( &self, user: &mut User, ) -> Result<(), MediaWikiError>
Loads the user info from the API into the user structure