Skip to content

Commit

Permalink
Merge pull request #3 from tembo-io/invoices
Browse files Browse the repository at this point in the history
add invoices table
  • Loading branch information
Jayko001 authored Feb 8, 2024
2 parents 6dc6199 + 75fe1d6 commit 5b4c876
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,23 @@ create foreign table orb_subscriptions (
options (
object 'subscriptions'
);
```

### Invoices Table

This table will store information about the subscriptions.

``` sql
create foreign table orb_invoices (
customer_id text,
subscription_id text,
organization_id text,
status text,
due_date text,
amount text
)
server my_orb_server
options (
object 'invoices'
);
```
12 changes: 12 additions & 0 deletions sql/orb_fdw--0.0.1--0.0.2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
create foreign table orb_invoices (
customer_id text,
subscription_id text,
organization_id text,
status text,
due_date text,
amount text
)
server my_orb_server
options (
object 'invoices'
);
31 changes: 24 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use orb_billing::Subscription;
use pgrx::warning;
use pgrx::{pg_sys, prelude::*, JsonB};
use serde_json::Value as JsonValue;
Expand All @@ -7,7 +6,7 @@ use std::env;
use std::str::FromStr;
use supabase_wrappers::prelude::*;
use tokio::runtime::Runtime;
pgrx::pg_module_magic!();
pg_module_magic!();
mod orb_fdw;
use crate::orb_fdw::OrbFdwError;
use reqwest::{self, header};
Expand Down Expand Up @@ -52,6 +51,21 @@ fn resp_to_rows(obj: &str, resp: &JsonValue, tgt_cols: &[Column]) -> Vec<Row> {
tgt_cols,
);
}
"invoices" => {
result = body_to_rows(
resp,
"data",
vec![
("subscription.id", "subscription_id", "string"),
("customer.id", "customer_id", "string"),
("customer.external_customer_id", "organization_id", "string"),
("status", "status", "string"),
("due_date", "due_date", "string"),
("amount_due", "amount", "string"),
],
tgt_cols,
);
}
_ => {
warning!("unsupported object: {}", obj);
}
Expand Down Expand Up @@ -158,21 +172,24 @@ impl OrbFdw {
const DEFAULT_BASE_URL: &'static str = "https://api.withorb.com/v1";
const DEFAULT_ROWS_LIMIT: usize = 10_000;

// TODO: will have to incorportate offset at some point
// TODO: will have to incorporate offset at some point
const PAGE_SIZE: usize = 500;

fn build_url(&self, obj: &str, options: &HashMap<String, String>, offset: usize) -> String {
fn build_url(&self, obj: &str, _offset: usize) -> String {
let base_url = Self::DEFAULT_BASE_URL.to_owned();
match obj {
"customers" => {
let base_url = Self::DEFAULT_BASE_URL.to_owned();
let ret = format!("{}/customers?limit={}", base_url, Self::PAGE_SIZE);
ret
}
"subscriptions" => {
let base_url = Self::DEFAULT_BASE_URL.to_owned();
let ret = format!("{}/subscriptions?limit={}", base_url, Self::PAGE_SIZE);
ret
}
"invoices" => {
let ret = format! {"{}/invoices?limit={}", base_url, Self::PAGE_SIZE};
ret
}
_ => {
warning!("unsupported object: {:#?}", obj);
return "".to_string();
Expand Down Expand Up @@ -235,7 +252,7 @@ impl ForeignDataWrapper for OrbFdw {
if let Some(client) = &self.client {
let mut result = Vec::new();

let url = self.build_url(&obj, options, 0);
let url = self.build_url(&obj, 0);

let body = self
.rt
Expand Down
2 changes: 1 addition & 1 deletion src/orb_fdw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ impl From<OrbFdwError> for ErrorReport {
}
}

pub type OrbFdwResult<T> = Result<T, OrbFdwError>;
pub type _OrbFdwResult<T> = Result<T, OrbFdwError>;

0 comments on commit 5b4c876

Please sign in to comment.