elastic / elasticsearch-rs

Official Elasticsearch Rust Client
https://www.elastic.co/guide/en/elasticsearch/client/rust-api/current/index.html
Apache License 2.0
705 stars 72 forks source link

[ENHANCEMENT] Non-generic API functions when API accepts GET and POST #9

Closed russcam closed 4 years ago

russcam commented 4 years ago

Some APIs allow sending a request either as a GET request or POST request. For those that allow sending as a GET request, a request body may not always need to be provided.

With the current implementation, an API that can send a POST request will model the inputs with a generic builder struct, where the generic type parameter is the body type. Take for example, search

#[doc = "http://www.elastic.co/guide/en/elasticsearch/reference/master/search-search.html"]
pub fn search<B>(&self) -> Search<B>
where
    B: Serialize,
{
    Search::new(self.clone())
}

With the current implementation, the generic type parameter B forms part of the search() function that returns the builder to build the request, meaning a user has to provide some value like () (unit), even if they may not be sending a request body, such as in the case of a GET request. This implementation can be changed to make search non-generic, and body on Search<B> generic


// 1. Make search() non-generic and return Search<()> by default

pub fn search(&self) -> Search<()>
where
    B: Serialize,
{
    Search::new(self.clone())
}

// 2. Make body() generic and return new Search<B> when body set

#[doc = "The body for the API call"]
pub fn body<B>(mut self, body: Option<B>) -> Search<B> {
    Search {
        client: self.client,
        _source: self._source,
        _source_excludes: self._source_excludes,
        //... assign all fields
        body: body,
    }
}

This can be generated from the REST API spec