1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
use crate::error::{Error, Result};
use crate::notification::Notification;
use crate::response::{ErrorResponse, NotificationResponse, RateLimitInfo};
use reqwest::StatusCode;
use serde_json::json;
use std::sync::{Arc, RwLock};
use std::time::Duration;
/// Pincho API client
///
/// The client is thread-safe and reuses HTTP connections via connection pooling.
/// It's recommended to create a single client instance and reuse it across your application.
///
/// # Examples
///
/// ```no_run
/// use pincho::Client;
///
/// #[tokio::main]
/// async fn main() -> Result<(), pincho::Error> {
/// let client = Client::new("abc12345")?;
/// let response = client.send("Hello", "World!").await?;
/// println!("Status: {}", response.status);
/// Ok(())
/// }
/// ```
#[derive(Debug, Clone)]
pub struct Client {
http_client: reqwest::Client,
token: String,
base_url: String,
max_retries: u32,
last_rate_limit: Arc<RwLock<Option<RateLimitInfo>>>,
}
impl Client {
/// Default API base URL
const DEFAULT_BASE_URL: &'static str = "https://api.pincho.app";
/// Default timeout in seconds
const DEFAULT_TIMEOUT: u64 = 30;
/// Default max retries
const DEFAULT_MAX_RETRIES: u32 = 3;
/// Maximum backoff delay in seconds
const MAX_BACKOFF_SECS: u64 = 30;
/// Library version
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
/// Creates a new Pincho client
///
/// # Arguments
///
/// * `token` - Your Pincho API token (Bearer token)
///
/// # Environment Variables
///
/// - `PINCHO_TOKEN` - Default token if not provided
/// - `PINCHO_TIMEOUT` - Request timeout in seconds (default: 30)
/// - `PINCHO_MAX_RETRIES` - Max retry attempts (default: 3)
///
/// # Errors
///
/// Returns an error if:
/// - Token is empty
/// - HTTP client cannot be created
///
/// # Examples
///
/// ```no_run
/// use pincho::Client;
///
/// # fn main() -> Result<(), pincho::Error> {
/// let client = Client::new("abc12345")?;
/// # Ok(())
/// # }
/// ```
pub fn new(token: impl Into<String>) -> Result<Self> {
let token = token.into();
if token.is_empty() {
return Err(Error::InvalidConfig("token cannot be empty".to_string()));
}
let timeout = std::env::var("PINCHO_TIMEOUT")
.ok()
.and_then(|s| s.parse::<u64>().ok())
.unwrap_or(Self::DEFAULT_TIMEOUT);
let max_retries = std::env::var("PINCHO_MAX_RETRIES")
.ok()
.and_then(|s| s.parse::<u32>().ok())
.unwrap_or(Self::DEFAULT_MAX_RETRIES);
let http_client = reqwest::Client::builder()
.timeout(Duration::from_secs(timeout))
.user_agent(format!("pincho-rust/{}", Self::VERSION))
.build()
.map_err(Error::Request)?;
Ok(Self {
http_client,
token,
base_url: Self::DEFAULT_BASE_URL.to_string(),
max_retries,
last_rate_limit: Arc::new(RwLock::new(None)),
})
}
/// Creates a client from environment variables
///
/// Reads `PINCHO_TOKEN` from environment.
///
/// # Errors
///
/// Returns an error if `PINCHO_TOKEN` is not set or empty.
pub fn from_env() -> Result<Self> {
let token = std::env::var("PINCHO_TOKEN")
.map_err(|_| Error::InvalidConfig("PINCHO_TOKEN not set".to_string()))?;
Self::new(token)
}
/// Creates a client with custom configuration
///
/// # Arguments
///
/// * `token` - Your Pincho API token
/// * `timeout` - Request timeout in seconds
/// * `max_retries` - Maximum retry attempts for retryable errors
pub fn with_config(token: impl Into<String>, timeout: u64, max_retries: u32) -> Result<Self> {
let token = token.into();
if token.is_empty() {
return Err(Error::InvalidConfig("token cannot be empty".to_string()));
}
let http_client = reqwest::Client::builder()
.timeout(Duration::from_secs(timeout))
.user_agent(format!("pincho-rust/{}", Self::VERSION))
.build()
.map_err(Error::Request)?;
Ok(Self {
http_client,
token,
base_url: Self::DEFAULT_BASE_URL.to_string(),
max_retries,
last_rate_limit: Arc::new(RwLock::new(None)),
})
}
/// Creates a client with a custom base URL (useful for testing)
///
/// # Arguments
///
/// * `token` - Your Pincho API token
/// * `base_url` - Custom base URL for the API
pub fn with_base_url(token: impl Into<String>, base_url: impl Into<String>) -> Result<Self> {
let mut client = Self::new(token)?;
client.base_url = base_url.into();
Ok(client)
}
/// Sets a custom base URL (useful for testing)
pub fn set_base_url(&mut self, base_url: impl Into<String>) {
self.base_url = base_url.into();
}
/// Sets the max retries (useful for testing)
pub fn set_max_retries(&mut self, max_retries: u32) {
self.max_retries = max_retries;
}
/// Returns the last rate limit information
pub fn get_last_rate_limit(&self) -> Option<RateLimitInfo> {
self.last_rate_limit
.read()
.ok()
.and_then(|guard| guard.clone())
}
/// Sends a simple notification with just title and message
///
/// # Arguments
///
/// * `title` - Notification title (max 256 characters)
/// * `message` - Notification message (max 4096 characters)
///
/// # Errors
///
/// Returns an error if:
/// - The HTTP request fails
/// - The API returns an error response
/// - The response cannot be parsed
///
/// # Examples
///
/// ```no_run
/// use pincho::Client;
///
/// #[tokio::main]
/// async fn main() -> Result<(), pincho::Error> {
/// let client = Client::new("abc12345")?;
/// let response = client.send("Build Complete", "v1.2.3 deployed").await?;
/// assert!(response.is_success());
/// Ok(())
/// }
/// ```
pub async fn send(
&self,
title: impl Into<String>,
message: impl Into<String>,
) -> Result<NotificationResponse> {
let notification = Notification::new(title, message);
self.send_notification(notification).await
}
/// Sends a notification with full options
///
/// # Arguments
///
/// * `notification` - The notification to send
///
/// # Errors
///
/// Returns an error if:
/// - The HTTP request fails
/// - The API returns an error response
/// - The response cannot be parsed
///
/// # Examples
///
/// ```no_run
/// use pincho::{Client, Notification};
///
/// #[tokio::main]
/// async fn main() -> Result<(), pincho::Error> {
/// let client = Client::new("abc12345")?;
///
/// let notification = Notification::builder()
/// .title("Deploy Complete")
/// .message("v1.2.3 deployed to production")
/// .notification_type("deployment")
/// .tags(vec!["prod".to_string(), "release".to_string()])
/// .build()?;
///
/// let response = client.send_notification(notification).await?;
/// assert!(response.is_success());
/// Ok(())
/// }
/// ```
pub async fn send_notification(
&self,
mut notification: Notification,
) -> Result<NotificationResponse> {
// Normalize tags
if let Some(ref mut tags) = notification.tags {
*tags = Self::normalize_tags(tags);
}
// Handle encryption if password is set
let message = if let Some(ref password) = notification.encryption_password {
// Generate random IV
use rand::Rng;
let mut iv = [0u8; 16];
rand::thread_rng().fill(&mut iv);
// Encrypt the message
let encrypted = crate::encryption::encrypt_text(¬ification.message, password, &iv)?;
// Store IV as hex (matching other SDKs)
notification.message_iv = Some(hex::encode(iv));
encrypted
} else {
notification.message.clone()
};
// Build the request body with camelCase field names
let mut body = json!({
"title": notification.title,
"message": message,
});
// Add optional fields with camelCase
if let Some(notification_type) = notification.notification_type {
body["type"] = json!(notification_type);
}
if let Some(tags) = notification.tags {
body["tags"] = json!(tags);
}
if let Some(image_url) = notification.image_url {
body["imageURL"] = json!(image_url);
}
if let Some(action_url) = notification.action_url {
body["actionURL"] = json!(action_url);
}
if let Some(message_iv) = notification.message_iv {
body["iv"] = json!(message_iv);
}
// Retry loop
let mut last_error: Option<Error> = None;
for attempt in 0..=self.max_retries {
if attempt > 0 {
// Exponential backoff: 1s, 2s, 4s, 8s... capped at 30s
let delay_secs = std::cmp::min(1u64 << (attempt - 1), Self::MAX_BACKOFF_SECS);
tokio::time::sleep(Duration::from_secs(delay_secs)).await;
}
// Send the request
let response = match self
.http_client
.post(format!("{}/send", self.base_url))
.bearer_auth(&self.token)
.json(&body)
.send()
.await
{
Ok(resp) => resp,
Err(e) => {
let error = Error::Request(e);
if error.is_retryable() && attempt < self.max_retries {
last_error = Some(error);
continue;
}
return Err(error);
}
};
// Parse rate limit headers
self.parse_rate_limit_headers(&response);
let status = response.status();
// Parse error responses
if !status.is_success() {
let error_text = response.text().await.unwrap_or_default();
let error = self.parse_error_response(status, &error_text);
if error.is_retryable() && attempt < self.max_retries {
last_error = Some(error);
continue;
}
return Err(error);
}
// Parse success response
let notification_response: NotificationResponse = response.json().await?;
return Ok(notification_response);
}
// Should not reach here, but return last error if we do
Err(last_error.unwrap_or_else(|| Error::Api {
message: "Max retries exceeded".to_string(),
status_code: 0,
}))
}
/// Normalizes tags to match API expectations
fn normalize_tags(tags: &[String]) -> Vec<String> {
tags.iter()
.map(|tag| {
tag.trim()
.to_lowercase()
.chars()
.filter(|c| c.is_alphanumeric() || *c == '-' || *c == '_')
.collect::<String>()
})
.filter(|tag| !tag.is_empty())
.collect::<std::collections::HashSet<_>>()
.into_iter()
.collect()
}
/// Parses rate limit headers from response
fn parse_rate_limit_headers(&self, response: &reqwest::Response) {
let headers = response.headers();
let limit = headers
.get("RateLimit-Limit")
.and_then(|v| v.to_str().ok())
.and_then(|s| s.parse::<u32>().ok());
let remaining = headers
.get("RateLimit-Remaining")
.and_then(|v| v.to_str().ok())
.and_then(|s| s.parse::<u32>().ok());
let reset = headers
.get("RateLimit-Reset")
.and_then(|v| v.to_str().ok())
.and_then(|s| s.parse::<u64>().ok());
if let (Some(limit), Some(remaining), Some(reset)) = (limit, remaining, reset) {
let rate_limit_info = RateLimitInfo {
limit,
remaining,
reset,
};
if let Ok(mut guard) = self.last_rate_limit.write() {
*guard = Some(rate_limit_info);
}
}
}
/// Sends a notification using NotifAI (AI-powered notification generation)
///
/// # Arguments
///
/// * `text` - Free-form text describing what happened
/// * `notification_type` - Optional notification type for categorization
///
/// # Examples
///
/// ```no_run
/// use pincho::Client;
///
/// #[tokio::main]
/// async fn main() -> Result<(), pincho::Error> {
/// let client = Client::new("abc12345")?;
/// let response = client.notifai("deployment finished v2.1.3", None).await?;
/// println!("Generated title: {}", response.notification.title);
/// Ok(())
/// }
/// ```
pub async fn notifai(
&self,
text: impl Into<String>,
notification_type: Option<String>,
) -> Result<crate::response::NotifAIResponse> {
let text = text.into();
// Build the request body
let mut body = json!({
"text": text,
});
if let Some(t) = notification_type {
body["type"] = json!(t);
}
// Retry loop
let mut last_error: Option<Error> = None;
for attempt in 0..=self.max_retries {
if attempt > 0 {
let delay_secs = std::cmp::min(1u64 << (attempt - 1), Self::MAX_BACKOFF_SECS);
tokio::time::sleep(Duration::from_secs(delay_secs)).await;
}
// Send the request
let response = match self
.http_client
.post(format!("{}/notifai", self.base_url))
.bearer_auth(&self.token)
.json(&body)
.send()
.await
{
Ok(resp) => resp,
Err(e) => {
let error = Error::Request(e);
if error.is_retryable() && attempt < self.max_retries {
last_error = Some(error);
continue;
}
return Err(error);
}
};
// Parse rate limit headers
self.parse_rate_limit_headers(&response);
let status = response.status();
if !status.is_success() {
let error_text = response.text().await.unwrap_or_default();
let error = self.parse_error_response(status, &error_text);
if error.is_retryable() && attempt < self.max_retries {
last_error = Some(error);
continue;
}
return Err(error);
}
// Parse success response
let notifai_response: crate::response::NotifAIResponse = response.json().await?;
return Ok(notifai_response);
}
Err(last_error.unwrap_or_else(|| Error::Api {
message: "Max retries exceeded".to_string(),
status_code: 0,
}))
}
/// Parses error response with nested error format
fn parse_error_response(&self, status: StatusCode, error_text: &str) -> Error {
let status_code = status.as_u16();
// Parse nested error response format (required)
let error_message = match serde_json::from_str::<ErrorResponse>(error_text) {
Ok(error_resp) => {
// Build descriptive error message from nested error details
let mut msg = error_resp.error.message.clone();
// Add error code if available and non-empty
if !error_resp.error.code.is_empty() {
msg = format!("{} [{}]", msg, error_resp.error.code);
}
// Add parameter context if available
if let Some(param) = &error_resp.error.param {
if !param.is_empty() {
msg = format!("{} (parameter: {})", msg, param);
}
}
msg
}
Err(_) => {
// Return generic error if nested format is not present
format!("API error: HTTP {}", status_code)
}
};
// Map to specific error type based on status code
match status {
StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => Error::Authentication {
message: error_message,
status_code,
},
StatusCode::BAD_REQUEST | StatusCode::NOT_FOUND => Error::Validation {
message: error_message,
status_code,
},
StatusCode::TOO_MANY_REQUESTS => Error::RateLimit {
message: error_message,
status_code,
},
_ => Error::Api {
message: error_message,
status_code,
},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_client_new() {
let client = Client::new("abc12345").unwrap();
assert_eq!(client.token, "abc12345");
assert_eq!(client.base_url, Client::DEFAULT_BASE_URL);
assert_eq!(client.max_retries, Client::DEFAULT_MAX_RETRIES);
}
#[test]
fn test_client_with_custom_base_url() {
let client = Client::with_base_url("abc12345", "https://custom.api.com").unwrap();
assert_eq!(client.base_url, "https://custom.api.com");
}
#[test]
fn test_client_with_config() {
let client = Client::with_config("abc12345", 60, 5).unwrap();
assert_eq!(client.token, "abc12345");
assert_eq!(client.max_retries, 5);
}
#[test]
fn test_client_empty_token() {
let result = Client::new("");
assert!(result.is_err());
assert!(matches!(result, Err(Error::InvalidConfig(_))));
}
#[test]
fn test_normalize_tags() {
let tags = vec![
" Production ".to_string(),
"BACKEND".to_string(),
"test-tag".to_string(),
"special@chars!".to_string(),
" ".to_string(), // Empty after trim
];
let normalized = Client::normalize_tags(&tags);
assert!(normalized.contains(&"production".to_string()));
assert!(normalized.contains(&"backend".to_string()));
assert!(normalized.contains(&"test-tag".to_string()));
assert!(normalized.contains(&"specialchars".to_string()));
assert!(!normalized.iter().any(|t| t.is_empty()));
}
#[test]
fn test_normalize_tags_deduplication() {
let tags = vec!["test".to_string(), "TEST".to_string(), "Test".to_string()];
let normalized = Client::normalize_tags(&tags);
assert_eq!(normalized.len(), 1);
assert!(normalized.contains(&"test".to_string()));
}
#[test]
fn test_parse_nested_error_format() {
let client = Client::new("token").unwrap();
let nested_error = r#"{"status":"error","error":{"type":"validation_error","code":"invalid_parameter","message":"Title is required","param":"title"}}"#;
let error = client.parse_error_response(StatusCode::BAD_REQUEST, nested_error);
assert!(error.is_validation_error());
match error {
Error::Validation {
message,
status_code,
} => {
assert_eq!(status_code, 400);
assert!(message.contains("Title is required"));
assert!(message.contains("[invalid_parameter]"));
assert!(message.contains("(parameter: title)"));
}
_ => panic!("Expected Validation error"),
}
}
#[test]
fn test_parse_nested_error_without_param() {
let client = Client::new("token").unwrap();
let nested_error = r#"{"status":"error","error":{"type":"authentication_error","code":"invalid_token","message":"Token is invalid"}}"#;
let error = client.parse_error_response(StatusCode::UNAUTHORIZED, nested_error);
assert!(error.is_authentication_error());
match error {
Error::Authentication {
message,
status_code,
} => {
assert_eq!(status_code, 401);
assert!(message.contains("Token is invalid"));
assert!(message.contains("[invalid_token]"));
assert!(!message.contains("(parameter:"));
}
_ => panic!("Expected Authentication error"),
}
}
}