Struct mediawiki::api::Api

source ·
pub struct Api { /* private fields */ }
Expand description

Api is the main class to interact with a MediaWiki API

Implementations§

source§

impl Api

source

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();
source

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.

source

pub fn api_url(&self) -> &str

Returns the API url

source

pub fn set_oauth(&mut self, oauth: Option<OAuthParams>)

Sets the OAuth parameters

source

pub fn set_oauth2(&mut self, oauth2: &str)

Set an OAuth 2 access token

source

pub fn oauth(&self) -> &Option<OAuthParams>

Returns a reference to the current OAuth parameters

source

pub fn client(&self) -> &Client

Returns a reference to the reqwest client

source

pub fn client_mut(&mut self) -> &mut Client

Returns a mutable reference to the reqwest client

source

pub fn user(&self) -> &User

Returns a reference to the current user object

source

pub fn user_mut(&mut self) -> &mut User

Returns a mutable reference to the current user object

source

pub async fn load_current_user_info(&mut self) -> Result<(), MediaWikiError>

Loads the current user info; returns Ok(()) is successful

source

pub fn max_retry_attempts(&self) -> u64

Returns the maximum number of retry attempts

source

pub fn set_max_retry_attempts(&mut self, max_retry_attempts: u64)

Sets the maximum number of retry attempts

source

pub fn get_site_info(&self) -> &Value

Returns a reference to the serde_json Value containing the site info

source

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.

source

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]

source

pub fn get_namespace_info(&self, namespace_id: NamespaceID) -> &Value

Returns the raw data for the namespace, matching ["query"]["namespaces"][namespace_id]

source

pub fn get_canonical_namespace_name( &self, namespace_id: NamespaceID, ) -> Option<&str>

Returns the canonical namespace name for a namespace ID, if defined

source

pub fn get_local_namespace_name( &self, namespace_id: NamespaceID, ) -> Option<&str>

Returns the local namespace name for a namespace ID, if defined

source

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

source

pub fn no_params(&self) -> HashMap<String, String>

Returns an empty parameter HashMap

source

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)

source

pub async fn get_edit_token(&mut self) -> Result<String, MediaWikiError>

Calls get_token() to return an edit token

source

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

source

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

source

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.

source

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.

source

pub fn edit_delay(&self) -> &Option<u64>

Returns the delay time after edits, in milliseconds, if set

source

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

source

pub fn maxlag(&self) -> &Option<u64>

Returns the maxlag, in seconds, if set

source

pub fn set_maxlag(&mut self, maxlag_seconds: Option<u64>)

Sets the maxlag in seconds (or None)

source

pub async fn get_query_api_json( &self, params: &HashMap<String, String>, ) -> Result<Value, MediaWikiError>

GET wrapper for query_api_json

source

pub async fn post_query_api_json( &self, params: &HashMap<String, String>, ) -> Result<Value, MediaWikiError>

POST wrapper for query_api_json

source

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

source

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

source

pub fn get_api_request_builder( &self, params: &HashMap<String, String>, method: &str, ) -> Result<RequestBuilder, MediaWikiError>

Generates a RequestBuilder for the API URL

source

pub fn user_agent(&self) -> &str

Returns the user agent name

source

pub fn set_user_agent<S: Into<String>>(&mut self, agent: S)

Sets the user agent name

source

pub fn user_agent_full(&self) -> String

Returns the user agent string, as it is passed to the API through a HTTP header

source

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

source

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

source

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.

source

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

source

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

source

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

source

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

source

pub async fn load_user_info( &self, user: &mut User, ) -> Result<(), MediaWikiError>

Loads the user info from the API into the user structure

Trait Implementations§

source§

impl Clone for Api

source§

fn clone(&self) -> Api

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Api

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Api

§

impl !RefUnwindSafe for Api

§

impl Send for Api

§

impl Sync for Api

§

impl Unpin for Api

§

impl !UnwindSafe for Api

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> ErasedDestructor for T
where T: 'static,

source§

impl<T> MaybeSendSync for T