Skip to content

Commit

Permalink
Merge branch 'next'
Browse files Browse the repository at this point in the history
  • Loading branch information
Juici committed Jan 4, 2020
2 parents 8da58de + 28780f4 commit dfaf65d
Show file tree
Hide file tree
Showing 32 changed files with 2,248 additions and 437 deletions.
9 changes: 3 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,11 @@ jobs:

msrv:
name: MSRV
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Install Rust
run: rustup update 1.39.0 --no-self-update && rustup default 1.39.0
run: rustup update 1.40.0 --no-self-update && rustup default 1.40.0
shell: bash
- run: cargo build

Expand All @@ -74,7 +71,7 @@ jobs:
- name: Publish documentation
run: |
cd target/doc
echo '<meta http-equiv=refresh content=0;url=strife/index.html>' > index.html
echo '<meta http-equiv="refresh" content="0;url=strife/index.html">' > index.html
git init
git add .
git -c user.name='ci' -c user.email='ci' commit -m init
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ edition = "2018"
async-std = "1.3"
bitflags = "1.2"
bytes = "0.5"
chrono = "0.4"
chrono = { version = "0.4", features = ["serde"] }
futures-timer = "2.0"
hyper = "0.13"
hyper-tls = "0.4"
int-enum = "0.2"
int-enum = { version = "0.3", features = ["serialize", "convert"] }
log = "0.4"
remain = "0.2"
serde = { version = "1.0", features = ["derive"] }
Expand Down
62 changes: 29 additions & 33 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! A collection of constants used by the library.

use int_enum::*;

/// The gateway version used by the library, URI is retrieved via the REST API.
pub const GATEWAY_VERSION: usize = 6;

Expand All @@ -13,37 +11,35 @@ pub const MESSAGE_MAX_LENGTH: usize = 2000;
/// The UserAgent header sent with every request.
pub const USER_AGENT: &str = concat!("DiscordBot (", pkg_repo!(), ", ", pkg_version!(), ")");

int_enum! {
/// Gateway opcodes.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum OpCode: u8 {
/// Dispatches an event.
Event = 0,
/// Used for ping checking.
Heartbeat = 1,
/// Used for client handshake.
Identify = 2,
/// Used to update the client status.
StatusUpdate = 3,
/// Used to join/move/leave voice channels.
VoiceStateUpdate = 4,
/// Used for voice ping checking.
VoiceServerPing = 5,
/// Used to resume a closed connection.
Resume = 6,
/// Used to tell clients to reconnect to the gateway.
Reconnect = 7,
/// Used to request guild members.
GetGuildMembers = 8,
/// Used to notify clients that they have an invalid session Id.
InvalidSession = 9,
/// Sent immediately after connection, contains heartbeat + server info.
Hello = 10,
/// Sent immediately following a client heartbeat that was received.
HeartbeatAck = 11,
#[doc(hidden)]
__Nonexhaustive,
}
/// Gateway opcodes.
#[int_enum::int_enum(u8)]
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum OpCode {
/// Dispatches an event.
Event = 0,
/// Used for ping checking.
Heartbeat = 1,
/// Used for client handshake.
Identify = 2,
/// Used to update the client status.
StatusUpdate = 3,
/// Used to join/move/leave voice channels.
VoiceStateUpdate = 4,
/// Used for voice ping checking.
VoiceServerPing = 5,
/// Used to resume a closed connection.
Resume = 6,
/// Used to tell clients to reconnect to the gateway.
Reconnect = 7,
/// Used to request guild members.
GetGuildMembers = 8,
/// Used to notify clients that they have an invalid session Id.
InvalidSession = 9,
/// Sent immediately after connection, contains heartbeat + server info.
Hello = 10,
/// Sent immediately following a client heartbeat that was received.
HeartbeatAck = 11,
}

/// Gateway close event codes.
Expand Down
11 changes: 7 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
use std::{fmt, result};
use std::fmt::Error as FmtError;
use std::result::Result as StdResult;

use serde_json::Error as JsonError;
use thiserror::Error;

use crate::http::HttpError;

/// The common result type returned by library functions.
pub type Result<T> = result::Result<T, Error>;
pub type Result<T> = StdResult<T, Error>;

/// The common error enum returned by library functions within a [`Result`].
///
/// [`Result`]: type.Result.html
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum Error {
/// An error while formatting a message.
#[error(transparent)]
Format(#[from] fmt::Error),
Format(#[from] FmtError),
/// An HTTP error.
#[error(transparent)]
HttpError(#[from] HttpError),
/// A JSON error.
#[error(transparent)]
JsonError(#[from] serde_json::Error),
JsonError(#[from] JsonError),
}
8 changes: 6 additions & 2 deletions src/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ impl Http {
let client = Arc::new(client);

Http {
ratelimiter: RateLimiter::new(client, token.to_string()),
ratelimiter: RateLimiter::new(client, token),
}
}

/// Performs a request with rate limiting if necessary.
async fn request<T: DeserializeOwned>(&self, req: Request<'_>) -> Result<T> {
///
/// # Stability
///
/// This is not part of the stable API and may change at any time.
pub async fn request<T: DeserializeOwned>(&self, req: Request<'_>) -> Result<T> {
json_body(&mut self.inner_request(req).await?).await
}

Expand Down
5 changes: 2 additions & 3 deletions src/http/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
use thiserror::Error;

/// An HTTP error.
#[remain::sorted]
#[non_exhaustive]
#[derive(Debug, Error)]
pub enum Error {
/// An Hyper error.
Expand Down Expand Up @@ -43,12 +43,11 @@ pub struct ErrorResponse {
/// A [Discord JSON error].
///
/// [Discord JSON error]: https://discordapp.com/developers/docs/topics/opcodes-and-status-codes#json
#[non_exhaustive]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct DiscordJsonError {
/// The error code.
pub code: u32,
/// A user friendly error message.
pub message: String,
#[serde(skip)]
non_exhaustive: (),
}
13 changes: 13 additions & 0 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,16 @@ mod routing;

pub use self::client::Http;
pub use self::error::Error as HttpError;

/// Unstable HTTP API.
///
/// # Stability
///
/// This is not part of the stable API and may change at any time. For a stable
/// API use the functions on the [`Http`] client.
///
/// [`Http`]: struct.Http.html
pub mod unstable {
pub use super::request::Request;
pub use super::routing::Route;
}
13 changes: 7 additions & 6 deletions src/http/ratelimit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::time::Duration;
use std::{i64, str};

use async_std::sync::{Arc, Mutex, RwLock};

use bytes::Bytes;
use futures_timer::Delay;
use hyper::{HeaderMap, StatusCode};
Expand All @@ -16,16 +15,17 @@ use super::prelude::*;
const RATELIMIT_GLOBAL: &str = "x-ratelimit-global";
const RATELIMIT_LIMIT: &str = "x-ratelimit-limit";
const RATELIMIT_REMAINING: &str = "x-ratelimit-remaining";

#[cfg(any(test, feature = "systime_ratelimits"))]
const RATELIMIT_RESET: &str = "x-ratelimit-reset";
#[cfg(any(test, not(feature = "systime_ratelimits")))]
const RATELIMIT_RESET_AFTER: &str = "x-ratelimit-reset-after";

const RETRY_AFTER: &str = "Retry-After";
const RETRY_AFTER: &str = "retry-after";

/// Ratelimiter for requests the the Discord REST API.
pub struct RateLimiter {
token: String,
token: Bytes,
client: Arc<HyperClient>,
global: Arc<Mutex<()>>,
routes: Arc<RwLock<HashMap<Bucket, Arc<Mutex<RateLimit>>>>>,
Expand All @@ -35,13 +35,14 @@ impl RateLimiter {
/// Creates a new rate limit manager.
pub fn new<S: AsRef<str>>(client: Arc<HyperClient>, token: S) -> RateLimiter {
RateLimiter {
token: token.as_ref().to_string(),
token: Bytes::copy_from_slice(token.as_ref().as_bytes()),
client,
global: Default::default(),
routes: Default::default(),
}
}

/// Performs a ratelimited request.
pub async fn perform(&self, request: &Request<'_>) -> Result<HttpResponse> {
let bucket = request.route.bucket();

Expand All @@ -50,7 +51,7 @@ impl RateLimiter {
// Drop instantly to prevent blocking other threads.
drop(self.global.lock().await);

let req = request.build(&self.token)?;
let req = request.build(self.token.clone())?;

// No rate limits apply.
if bucket == Bucket::None {}
Expand Down Expand Up @@ -108,7 +109,7 @@ impl RateLimiter {
}
}

pub struct RateLimit {
struct RateLimit {
limit: i64,
remaining: i64,
#[cfg(feature = "systime_ratelimits")]
Expand Down
Loading

0 comments on commit dfaf65d

Please sign in to comment.