arcgis 0.1.1

Type-safe Rust SDK for the ArcGIS REST API with compile-time guarantees
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
473
# Multi-Tier Testing Configuration

This document explains the multi-tier API key configuration system used for testing the ArcGIS Rust SDK.

## Overview

The SDK supports multiple privilege tiers for API keys, each providing access to different service levels. Instead of managing multiple `.env` files, the SDK uses a **single `.env` file with multiple API keys**, automatically selecting the correct key based on which test tier you're running.

## Architecture

```
┌─────────────────┐
│  Test Feature   │ (compile-time)
│  test-public    │
│  test-location  │
│  test-content   │
│  test-features  │
└────────┬────────┘
┌─────────────────────┐
│ config/test-tiers   │ (configuration)
│ .toml               │
│                     │
│ Tier → Env Var Name │
└────────┬────────────┘
┌─────────────────────┐
│     .env file       │ (runtime)
│                     │
│ ARCGIS_PUBLIC_KEY   │
│ ARCGIS_LOCATION_KEY │
│ ARCGIS_CONTENT_KEY  │
│ ARCGIS_FEATURES_KEY │
└─────────────────────┘
```

## Configuration Files

### config/test-tiers.toml

Maps test tiers to environment variable names:

```toml
[tiers.public]
env_var = "ARCGIS_PUBLIC_KEY"
credits_per_request = 0.0
description = "Public services - no authentication required"

[tiers.location]
env_var = "ARCGIS_LOCATION_KEY"
credits_per_request = 0.004
description = "Location services - geocoding, routing, geometry"

# ... additional tiers
```

This file is:
- **Git-tracked** - Part of the repository
- **Declarative** - Configuration as data, not code
- **Extensible** - Easy to add new tiers

### .env (Single File)

Contains all your API keys:

```bash
# Single .env file with multiple keys
ARCGIS_PUBLIC_KEY=your_public_key_here
ARCGIS_LOCATION_KEY=your_location_key_here
ARCGIS_CONTENT_KEY=your_content_key_here
ARCGIS_FEATURES_KEY=your_features_key_here
```

This file is:
- **Git-ignored** - Never committed
- **User-specific** - Each developer has their own
- **Single source** - All keys in one place

## Test Tiers

### Tier 1: Public

**Feature flag:** `test-public`
**Environment variable:** `ARCGIS_PUBLIC_KEY`
**Services:** World Cities, public feature services
**Credit cost:** 0 (no auth required)

```bash
cargo test --features test-public
```

### Tier 2: Location

**Feature flag:** `test-location`
**Environment variable:** `ARCGIS_LOCATION_KEY`
**Services:** Geocoding, routing, geometry operations
**Credit cost:** ~0.004 per geocode, ~0.5 per route

```bash
cargo test --features test-location
```

### Tier 3: Content Management

**Feature flag:** `test-content`
**Environment variable:** `ARCGIS_CONTENT_KEY`
**Role:** Content Manager - create, publish, and share portal content
**Services:** Portal content management, item publishing, group management, sharing
**Credit cost:** ~0.001 per operation (storage costs)

```bash
cargo test --features test-content
```

### Tier 3: Feature Editing

**Feature flag:** `test-features`
**Environment variable:** `ARCGIS_FEATURES_KEY`
**Role:** Editor - edit features in existing hosted layers
**Services:** Feature editing, edit sessions, versioned editing workflows
**Credit cost:** ~0.001 per operation (compute costs)

```bash
cargo test --features test-features
```

## Usage in Tests

### Common Helper

All tests use the `common::api_key()` helper which automatically selects the correct environment variable:

```rust
// tests/integration_geocoding.rs
#![cfg(feature = "test-location")]

fn create_client() -> anyhow::Result<ArcGISClient> {
    // Automatically reads ARCGIS_LOCATION_KEY
    let key = common::api_key()?;
    Ok(ArcGISClient::new(ApiKeyAuth::new(key)))
}
```

### How It Works

1. **Compile time:** Feature flag determines active tier (`test-location`)
2. **Configuration:** `config/test-tiers.toml` maps tier → env var name
3. **Runtime:** `common::api_key()` reads the mapped environment variable
4. **Automatic:** No code changes needed per tier

## Setup for Developers

### 1. Copy Template

```bash
cp .env.example .env
```

### 2. Add Your Keys

Edit `.env` and fill in the keys you have:

```bash
# You don't need all keys - only add the ones you have
ARCGIS_PUBLIC_KEY=       # Optional (public services work without auth)
ARCGIS_LOCATION_KEY=     # For geocoding/routing tests
ARCGIS_CONTENT_KEY=      # For content management tests
ARCGIS_FEATURES_KEY=     # For feature editing tests
```

### 3. Create API Keys with Correct Permissions

Go to [developers.arcgis.com/api-keys](https://developers.arcgis.com/api-keys/) and create keys with the following permissions:

#### Tier 1: Public Key (ARCGIS_PUBLIC_KEY) - Optional

Public services work without authentication, but providing a key may increase rate limits.

**Creating the key:**
1. Sign in to [ArcGIS Location Platform]https://location.arcgis.com/ or [ArcGIS Online]https://arcgis.com
2. Go to **Content > My content > New item > Developer credentials > API key credentials**
3. Name: `SDK Testing - Public`
4. Set expiration date (up to 1 year)
5. **Privileges:** None required (leave all unchecked)
   - _Optional:_ Check "Basemap styles service" for enhanced rate limits

**Note:** This key is optional - public services work without authentication.

#### Tier 2: Location Key (ARCGIS_LOCATION_KEY) - Required for test-location

**Creating the key:**
1. Sign in to [ArcGIS Location Platform]https://location.arcgis.com/ or [ArcGIS Online]https://arcgis.com
2. Go to **Content > My content > New item > Developer credentials > API key credentials**
3. Name: `SDK Testing - Location Services`
4. Set expiration date (up to 1 year)
5. **Configure privileges - Check these:**

**Location Services** (check all that apply):
- ☑ Basemap styles service
- ☑ Static basemap tiles
- ☑ Geocode (stored)
- ☑ Geocode (not stored)
- ☑ Elevation
- ☑ GeoEnrichment
- ☑ Place finding
- ☑ Routing
- ☑ Service area
- ☑ Closest facility
- ☑ Origin/destination cost matrix
- ☑ Optimized routing
- ☑ Multi-vehicle routing
- ☑ Location allocation
- ☑ Last mile

**Spatial Analysis:**
- ☑ Spatial analysis service

**What this enables:**
- Geocoding (address → coordinates, coordinates → address)
- Routing (routes, directions, travel times)
- Geometry operations (buffer, project, simplify)
- Service area analysis (drive-time polygons)
- Network analysis (closest facility, vehicle routing)

**Credit consumption:**
- Geocoding: ~0.004 credits per geocode
- Simple route: ~0.5 credits per route
- Service area: ~0.5 credits per analysis
- Geometry operations: Varies by complexity

#### Tier 3: Content Key (ARCGIS_CONTENT_KEY) - Required for test-content

**Role:** Content Manager - create, publish, and share portal content

**Creating the key:**
1. Sign in to [ArcGIS Online]https://arcgis.com (requires organizational account)
2. Go to **Content > My content > New item > Developer credentials > API key credentials**
3. Name: `SDK Testing - Content Management`
4. Set expiration date (up to 1 year)
5. **Configure privileges - Check these:**

**Content:**
- ☑ Create, update, and delete
- ☑ Publish hosted feature layers
- ☑ Publish hosted tile layers (optional)
- ☑ Publish hosted scene layers (optional)

**Sharing:**
- ☑ Share with groups
- ☑ Share with organization
- ☑ Share with public (optional)

**Groups:**
- ☑ View organization members
- ☑ View organization groups
- ☑ Join organization groups

**Additional Requirements:**
Your ArcGIS Online account must have these user privileges (configured by your org administrator):

1. Log in to [arcgis.com]https://arcgis.com
2. Go to **Organization → Members → Your Profile → Privileges**
3. Verify you have:
   - Content: Create, update, delete
   - Publishing: Publish hosted feature layers
   - Groups: Create, update, delete groups
   - Sharing: Share content with groups and organization

**What this enables:**
- Portal content search and discovery
- Item creation and metadata management
- Publishing hosted feature/tile/scene layers
- Group creation and administration
- Content sharing and permissions

**Credit consumption:**
- Content operations: ~0.001 credits per operation
- Storage costs apply (check your organization's quota)

#### Tier 3: Features Key (ARCGIS_FEATURES_KEY) - Required for test-features

**Role:** Editor - edit features in existing hosted layers

**Creating the key:**
1. Sign in to [ArcGIS Online]https://arcgis.com or ArcGIS Enterprise portal
2. Go to **Content > My content > New item > Developer credentials > API key credentials**
3. Name: `SDK Testing - Feature Editing`
4. Set expiration date (up to 1 year)
5. **Configure privileges - Check these:**

**Features:**
- ☑ Edit
- ☑ Edit with full control

**Note:** This key ONLY grants editing privileges. To create the hosted feature services you'll edit, you need the Content key (test-content) to publish them first.

**Additional Requirements:**
Your ArcGIS account must have these user privileges (configured by your org administrator):

1. Log in to your portal
2. Go to **Organization → Members → Your Profile → Privileges**
3. Verify you have:
   - Features: Edit features
   - Features: Full editing control (for versioned editing)

**For ArcGIS Enterprise (versioned editing):**
- ArcGIS Enterprise 11.2 or later
- Version Management Server configured
- Additional privileges:
  - Enable branch versioning on feature layers
  - Manage versions

**What this enables:**
- Edit features in existing hosted feature layers
- Add, update, delete features
- Edit sessions with transaction support
- Branch-versioned editing workflows (Enterprise)
- Version management operations (Enterprise)
- Multi-user editing with conflict detection (Enterprise)

**Credit consumption:**
- Edit operations: ~0.001 credits per operation
- Compute costs for edit sessions and version reconciliation (Enterprise)

**Typical workflow:**
1. Use Content key to create/publish hosted feature service
2. Use Features key to edit features in that service
3. Content managers and editors have separate, minimal privileges

### 4. Verify Your Keys

After creating keys, verify they work:

```bash
# Test public key (optional)
ARCGIS_PUBLIC_KEY=your_key cargo test --features test-public test_credentials_available

# Test location key
ARCGIS_LOCATION_KEY=your_key cargo test --features test-location test_credentials_available

# Test content key (requires org account)
ARCGIS_CONTENT_KEY=your_key cargo test --features test-content test_credentials_available

# Test features key (requires hosted feature services)
ARCGIS_FEATURES_KEY=your_key cargo test --features test-features test_credentials_available
```

### 5. Run Tests

```bash
# Run tests for the tier you have keys for
cargo test --features test-public    # No key required
cargo test --features test-location  # Requires ARCGIS_LOCATION_KEY
```

## Benefits

### Single File Management

✅ **One file to manage** - All keys in `.env`
✅ **No file switching** - Don't juggle `.env_public`, `.env_location`, etc.
✅ **Easy to share templates** - `.env.example` shows all options

### Configuration-Driven

✅ **Declarative mapping** - Config file, not hardcoded logic
✅ **Easy to extend** - Add new tier in TOML, no code changes
✅ **Battle-tested** - Uses `config` crate (industry standard)

### Developer Experience

✅ **Clear error messages** - "ARCGIS_LOCATION_KEY not found (required for tier: location)"
✅ **Self-documenting** - Config file shows all tiers and requirements
✅ **Flexible** - Have all keys or just the ones you need

## Adding a New Tier

To add a new test tier (e.g., `test-analytics`):

### 1. Add Feature to Cargo.toml

```toml
[features]
test-analytics = []
```

### 2. Add Configuration

```toml
# config/test-tiers.toml
[tiers.analytics]
env_var = "ARCGIS_ANALYTICS_KEY"
credits_per_request = 0.005
description = "Analytics services - spatial analysis"
```

### 3. Update .env.example

```bash
# Tier 2: Analytics services
ARCGIS_ANALYTICS_KEY=
```

### 4. Update Helper

```rust
// tests/common/mod.rs
fn active_tier() -> &'static str {
    if cfg!(feature = "test-analytics") { "analytics" }
    // ... existing tiers
}
```

That's it! No other code changes needed.

## Troubleshooting

### Error: "Environment variable ARCGIS_LOCATION_KEY not found"

**Problem:** Running `test-location` tests without the required key.

**Solution:** Add the key to your `.env` file:
```bash
ARCGIS_LOCATION_KEY=your_key_here
```

### Error: "Failed to load config/test-tiers.toml"

**Problem:** Config file missing or malformed.

**Solution:** Ensure `config/test-tiers.toml` exists in repository root and is valid TOML.

### All tests fail with key errors

**Problem:** No `.env` file or empty keys.

**Solution:**
```bash
cp .env.example .env
# Edit .env and add at least one key
```

## Best Practices

### For SDK Developers

- ✅ Keep all your keys in a single `.env` file
- ✅ Use the helper: `common::api_key()` in all tests
- ✅ Document credit costs in test tier config
- ✅ Update `.env.example` when adding new tiers

### For CI/CD

- ✅ Use GitHub Secrets for keys
- ✅ Template `.env` file in CI workflow
- ✅ Run only the tiers with available keys
- ✅ Skip expensive tiers (portal, publishing) in PR builds

### For Users

- ✅ Start with `test-public` (no key required)
- ✅ Add keys incrementally as needed
- ✅ Monitor credit usage in ArcGIS Dashboard
- ✅ Use separate keys for dev/prod environments

## References

- [API Key Testing Strategy]./API_KEY_TESTING_STRATEGY.md
- [.env.example]../.env.example
- [config/test-tiers.toml]../config/test-tiers.toml
- [config crate documentation]https://docs.rs/config/