drasi-bootstrap-http 0.1.8

HTTP bootstrap plugin for Drasi - fetches initial state from REST APIs
Documentation
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
# HTTP Bootstrap Plugin for Drasi

Fetches initial state from REST APIs to populate Drasi continuous queries. Supports multiple endpoints, various authentication methods, pagination strategies, and flexible response-to-graph-element mapping.

## Features

- **Multiple endpoints** – fetch nodes and relations from different APIs in a single bootstrap
- **5 pagination strategies** – offset/limit, page number, cursor-based, Link header, next-URL
- **4 authentication methods** – Bearer token, API key, Basic auth, OAuth2 client credentials
- **Flexible mapping** – Handlebars templates transform any JSON/XML/YAML response into graph elements
- **Retry with backoff** – configurable retries for transient failures
- **Streaming** – emits elements as they're parsed, no full-dataset buffering

## Configuration Reference

### Top-Level Config

| Field | Type | Default | Description |
|-------|------|---------|-------------|
| `endpoints` | array | (required) | List of endpoint configurations |
| `timeoutSeconds` | integer | 30 | HTTP request timeout in seconds |
| `maxRetries` | integer | 3 | Maximum retry attempts on failure |
| `retryDelayMs` | integer | 1000 | Base delay between retries in milliseconds; grows exponentially (delay × 2^(attempt−1)), capped at 60 seconds |

### Endpoint Config

| Field | Type | Default | Description |
|-------|------|---------|-------------|
| `url` | string | (required) | The URL to fetch data from |
| `method` | string | `"GET"` | HTTP method (`GET`, `POST`, `PUT`) |
| `headers` | object | `{}` | Additional HTTP headers |
| `body` | object | null | Request body (for POST/PUT) |
| `auth` | object | null | Authentication configuration |
| `pagination` | object | null | Pagination configuration |
| `response` | object | (required) | Response parsing and mapping |

### Authentication

#### Bearer Token
```json
{
  "type": "bearer",
  "token_env": "MY_API_TOKEN"
}
```

#### API Key (Header or Query)
```json
{
  "type": "api-key",
  "location": "header",
  "name": "X-API-Key",
  "value_env": "MY_API_KEY"
}
```

```json
{
  "type": "api-key",
  "location": "query",
  "name": "api_key",
  "value_env": "MY_API_KEY"
}
```

#### Basic Auth
```json
{
  "type": "basic",
  "username_env": "MY_USERNAME",
  "password_env": "MY_PASSWORD"
}
```

#### OAuth2 Client Credentials
```json
{
  "type": "oauth2-client-credentials",
  "token_url": "https://auth.example.com/oauth/token",
  "client_id_env": "MY_CLIENT_ID",
  "client_secret_env": "MY_CLIENT_SECRET",
  "scopes": ["read"]
}
```

### Pagination Strategies

#### Offset/Limit
Classic offset-based pagination. Increments offset by `page_size` each request.

```json
{
  "type": "offset-limit",
  "offset_param": "offset",
  "limit_param": "limit",
  "page_size": 100,
  "total_path": "$.meta.total"
}
```

Stop conditions: `total_path` exceeded, or page returns fewer items than `page_size`.

#### Page Number
Simple page number pagination. Increments page number each request.

```json
{
  "type": "page-number",
  "page_param": "page",
  "page_size_param": "per_page",
  "page_size": 100,
  "total_pages_path": "$.meta.total_pages"
}
```

Stop conditions: `total_pages_path` exceeded, or page returns fewer items than `page_size`.

#### Cursor (Stripe-style)
Extracts a cursor value from the response and passes it as a query parameter.

```json
{
  "type": "cursor",
  "cursor_param": "starting_after",
  "cursor_path": "$.data[-1].id",
  "has_more_path": "$.has_more",
  "page_size_param": "limit",
  "page_size": 100
}
```

Stop conditions: `has_more_path` is `false`, cursor is null/empty, or page is empty.

#### Link Header (GitHub/Shopify-style)
Follows the `rel="next"` URL in the RFC 5988 `Link` response header.

```json
{
  "type": "link-header",
  "page_size_param": "per_page",
  "page_size": 100
}
```

Stop conditions: no `Link` header with `rel="next"`, or page is empty.

#### Next URL (Salesforce/Twilio-style)
Extracts the next page URL from the response body.

```json
{
  "type": "next-url",
  "next_url_path": "$.nextRecordsUrl",
  "base_url": "https://instance.salesforce.com"
}
```

If the extracted URL is relative, `base_url` is prepended. Stop conditions: field is null/absent.

### Response Config

| Field | Type | Default | Description |
|-------|------|---------|-------------|
| `itemsPath` | string | `"$"` | JSONPath to the array of items in the response |
| `contentType` | string | auto-detect | Override content type (`json`, `xml`, `yaml`) |
| `mappings` | array | (required) | Element mapping configurations |

### Element Mapping

Each mapping produces a node or relation from each item:

```json
{
  "elementType": "node",
  "template": {
    "id": "{{item.id}}",
    "labels": ["User"],
    "properties": {
      "name": "{{item.name}}",
      "email": "{{item.email}}"
    }
  }
}
```

For relations, include `from` and `to`:

```json
{
  "elementType": "relation",
  "template": {
    "id": "{{item.id}}",
    "labels": ["FOLLOWS"],
    "from": "{{item.follower_id}}",
    "to": "{{item.following_id}}"
  }
}
```

Template values use [Handlebars](https://handlebarsjs.com/) syntax. The `item` variable contains the current response item.

---

## Real-World API Examples

### GitHub REST API

Fetch all repositories for an organization. Uses **Link header** pagination and **Bearer** token auth.

```json
{
  "endpoints": [
    {
      "url": "https://api.github.com/orgs/my-org/repos",
      "method": "GET",
      "headers": {
        "Accept": "application/vnd.github+json",
        "X-GitHub-Api-Version": "2022-11-28"
      },
      "auth": {
        "type": "bearer",
        "token_env": "GITHUB_TOKEN"
      },
      "pagination": {
        "type": "link-header",
        "page_size_param": "per_page",
        "page_size": 100
      },
      "response": {
        "itemsPath": "$",
        "mappings": [
          {
            "elementType": "node",
            "template": {
              "id": "{{item.id}}",
              "labels": ["Repository"],
              "properties": {
                "name": "{{item.name}}",
                "full_name": "{{item.full_name}}",
                "description": "{{item.description}}",
                "language": "{{item.language}}",
                "stars": "{{item.stargazers_count}}",
                "forks": "{{item.forks_count}}",
                "private": "{{item.private}}"
              }
            }
          }
        ]
      }
    }
  ],
  "timeoutSeconds": 30,
  "maxRetries": 3,
  "retryDelayMs": 2000
}
```

### Shopify REST API

Fetch products from a Shopify store. Uses **Link header** pagination (with cursor in `page_info`) and **API key** header auth.

```json
{
  "endpoints": [
    {
      "url": "https://mystore.myshopify.com/admin/api/2024-01/products.json",
      "method": "GET",
      "auth": {
        "type": "api-key",
        "location": "header",
        "name": "X-Shopify-Access-Token",
        "value_env": "SHOPIFY_ACCESS_TOKEN"
      },
      "pagination": {
        "type": "link-header",
        "page_size_param": "limit",
        "page_size": 50
      },
      "response": {
        "itemsPath": "$.products",
        "mappings": [
          {
            "elementType": "node",
            "template": {
              "id": "{{item.id}}",
              "labels": ["Product"],
              "properties": {
                "title": "{{item.title}}",
                "vendor": "{{item.vendor}}",
                "product_type": "{{item.product_type}}",
                "status": "{{item.status}}",
                "created_at": "{{item.created_at}}"
              }
            }
          }
        ]
      }
    }
  ],
  "timeoutSeconds": 30,
  "maxRetries": 3,
  "retryDelayMs": 1000
}
```

### Stripe API

Fetch all customers. Uses **cursor-based** pagination (`starting_after` + `has_more`) and **Basic** auth (API key as username).

```json
{
  "endpoints": [
    {
      "url": "https://api.stripe.com/v1/customers",
      "method": "GET",
      "auth": {
        "type": "basic",
        "username_env": "STRIPE_SECRET_KEY"
      },
      "pagination": {
        "type": "cursor",
        "cursor_param": "starting_after",
        "cursor_path": "$.data[-1].id",
        "has_more_path": "$.has_more",
        "page_size_param": "limit",
        "page_size": 100
      },
      "response": {
        "itemsPath": "$.data",
        "mappings": [
          {
            "elementType": "node",
            "template": {
              "id": "{{item.id}}",
              "labels": ["Customer"],
              "properties": {
                "name": "{{item.name}}",
                "email": "{{item.email}}",
                "created": "{{item.created}}",
                "currency": "{{item.currency}}"
              }
            }
          }
        ]
      }
    }
  ],
  "timeoutSeconds": 60,
  "maxRetries": 5,
  "retryDelayMs": 2000
}
```

### Salesforce REST API

Fetch Accounts via SOQL query. Uses **next-URL** pagination (`nextRecordsUrl`) and **OAuth2 client credentials**.

```json
{
  "endpoints": [
    {
      "url": "https://myinstance.salesforce.com/services/data/v59.0/query?q=SELECT+Id,Name,Industry,AnnualRevenue+FROM+Account",
      "method": "GET",
      "auth": {
        "type": "oauth2-client-credentials",
        "token_url": "https://login.salesforce.com/services/oauth2/token",
        "client_id_env": "SF_CLIENT_ID",
        "client_secret_env": "SF_CLIENT_SECRET",
        "scopes": []
      },
      "pagination": {
        "type": "next-url",
        "next_url_path": "$.nextRecordsUrl",
        "base_url": "https://myinstance.salesforce.com"
      },
      "response": {
        "itemsPath": "$.records",
        "mappings": [
          {
            "elementType": "node",
            "template": {
              "id": "{{item.Id}}",
              "labels": ["Account"],
              "properties": {
                "name": "{{item.Name}}",
                "industry": "{{item.Industry}}",
                "annual_revenue": "{{item.AnnualRevenue}}"
              }
            }
          }
        ]
      }
    }
  ],
  "timeoutSeconds": 120,
  "maxRetries": 3,
  "retryDelayMs": 5000
}
```

### Twilio REST API

Fetch call records. Uses **next-URL** pagination (`next_page_uri`) and **Basic** auth (AccountSid:AuthToken).

```json
{
  "endpoints": [
    {
      "url": "https://api.twilio.com/2010-04-01/Accounts/ACXXXXXXXXX/Calls.json",
      "method": "GET",
      "auth": {
        "type": "basic",
        "username_env": "TWILIO_ACCOUNT_SID",
        "password_env": "TWILIO_AUTH_TOKEN"
      },
      "pagination": {
        "type": "next-url",
        "next_url_path": "$.next_page_uri",
        "base_url": "https://api.twilio.com"
      },
      "response": {
        "itemsPath": "$.calls",
        "mappings": [
          {
            "elementType": "node",
            "template": {
              "id": "{{item.sid}}",
              "labels": ["Call"],
              "properties": {
                "from": "{{item.from}}",
                "to": "{{item.to}}",
                "status": "{{item.status}}",
                "duration": "{{item.duration}}",
                "start_time": "{{item.start_time}}"
              }
            }
          }
        ]
      }
    }
  ],
  "timeoutSeconds": 60,
  "maxRetries": 3,
  "retryDelayMs": 1000
}
```

---

## Development

### Building

```bash
cargo build -p drasi-bootstrap-http
```

### Running Tests

```bash
# Unit tests
cargo test -p drasi-bootstrap-http --lib

# Integration tests (spins up real HTTP servers)
cargo test -p drasi-bootstrap-http --test integration_test
```

### Linting

```bash
cargo clippy -p drasi-bootstrap-http
```