conjure_http/private/client/
mod.rs

1// Copyright 2019 Palantir Technologies, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14pub use crate::private::client::uri_builder::UriBuilder;
15use bytes::Bytes;
16use conjure_error::Error;
17use conjure_object::{BearerToken, Plain, ToPlain};
18use http::header::{HeaderName, HeaderValue, AUTHORIZATION, COOKIE};
19use http::Request;
20
21mod uri_builder;
22
23pub fn encode_header<B>(
24    request: &mut Request<B>,
25    header: &'static str,
26    value: &dyn Plain,
27) -> Result<(), Error> {
28    let header = HeaderName::from_static(header);
29    let value = HeaderValue::from_maybe_shared(Bytes::from(value.to_plain()))
30        .map_err(Error::internal_safe)?;
31    request.headers_mut().insert(header, value);
32
33    Ok(())
34}
35
36pub fn encode_cookie_auth<B>(request: &mut Request<B>, prefix: &str, value: &BearerToken) {
37    encode_auth(request, COOKIE, prefix, value)
38}
39
40pub fn encode_header_auth<B>(request: &mut Request<B>, value: &BearerToken) {
41    encode_auth(request, AUTHORIZATION, "Bearer ", value);
42}
43
44fn encode_auth<B>(request: &mut Request<B>, header: HeaderName, prefix: &str, value: &BearerToken) {
45    let value = format!("{}{}", prefix, value.as_str());
46    let value = HeaderValue::from_maybe_shared(Bytes::from(value))
47        .expect("bearer tokens are valid headers");
48    request.headers_mut().insert(header, value);
49}