Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
**/*.rs.bk
Cargo.lock
.idea

#vim files
*swp
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "africastalking_gateway"
version = "0.1.0"
authors = ["Matt Gathu <mattgathu@gmail.com>"]
version = "0.1.1"
authors = ["Matt Gathu <mattgathu@gmail.com>", "Brian Orwe <brian.orwe@gmail.com>"]

description = "A Rust library for communicating with the Africa's Talking REST API."
documentation = "https://docs.rs/africastalking_gateway"
Expand All @@ -16,8 +16,7 @@ license = "MIT"
travis-ci = { repository = "rust-nairobi/africastalking-rust"}

[dependencies]
reqwest = "0.8.1"
hyper = "0.11.6"
reqwest = { version = "0.11", features = ["json","blocking"] }
serde = "1.0.37"
serde_derive = "1.0.37"
serde_json = "1.0"
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@

A Rust library for communicating with the Africa's Talking REST API.

### Requirements

OpenSSL.
*Ubuntu:*

```
sudo apt-get install libssl-dev
```

*Fedora:*

```
sudo dnf install openssl-devel
```




### installation

Expand Down
38 changes: 17 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,20 @@

#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate hyper;
extern crate reqwest;
extern crate serde;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate serde_json as json;

use reqwest::header::{HeaderMap,ACCEPT};

use std::collections::HashMap;
use std::io::Read;

use hyper::header::{Accept, Headers};
use serde::ser::Serialize;

header! { (Apikey, "apikey") => [String] }

#[allow(unused_variables)]
trait HttpAccessMethods {
fn send_request(&self, url: &str, data: Option<HashMap<&str, &str>>) {}
Expand All @@ -40,7 +37,6 @@ error_chain! {
} }

}

/// SMS Message Struct
#[derive(Serialize, Deserialize, Debug, Default)]
#[allow(non_snake_case)]
Expand Down Expand Up @@ -277,11 +273,11 @@ impl AfricasTalkingGateway {
&self,
url: &str,
data: Option<HashMap<&str, &str>>,
) -> Result<reqwest::Response> {
let mut headers = Headers::new();
headers.set(Accept::json());
headers.set(Apikey(self.api_key.clone()));
let client = reqwest::Client::new();
) -> Result<reqwest::blocking::Response> {
let mut headers = HeaderMap::new();
headers.insert(ACCEPT,"application/json".parse().unwrap());
headers.insert("Apikey",self.api_key.clone().parse().unwrap());
let client = reqwest::blocking::Client::new();
let resp = match data {
Some(map) => client.post(url).json(&map).send()?,
None => client.get(url).headers(headers).send()?,
Expand All @@ -290,21 +286,21 @@ impl AfricasTalkingGateway {
Ok(resp)
}

fn send_form_data<T: Serialize>(&self, url: &str, data: T) -> Result<reqwest::Response> {
let mut headers = Headers::new();
headers.set(Accept::json());
headers.set(Apikey(self.api_key.clone()));
let client = reqwest::Client::new();
fn send_form_data<T: Serialize>(&self, url: &str, data: T) -> Result<reqwest::blocking::Response> {
let mut headers = HeaderMap::new();
headers.insert(ACCEPT, "application/json".parse().unwrap());
headers.insert("Apikey",self.api_key.clone().parse().unwrap());
let client = reqwest::blocking::Client::new();
let resp = client.post(url).form(&data).headers(headers).send()?;

Ok(resp)
}

fn send_json_request<T: Serialize>(&self, url: &str, data: T) -> Result<reqwest::Response> {
let mut headers = Headers::new();
headers.set(Accept::json());
headers.set(Apikey(self.api_key.clone()));
let client = reqwest::Client::new();
fn send_json_request<T: Serialize>(&self, url: &str, data: T) -> Result<reqwest::blocking::Response> {
let mut headers = HeaderMap::new();
headers.insert(ACCEPT, "application/json".parse().unwrap());
headers.insert("Apikey",self.api_key.clone().parse().unwrap());
let client = reqwest::blocking::Client::new();
let resp = client.post(url).json(&data).headers(headers).send()?;

Ok(resp)
Expand Down