MCPcopy Create free account
hub / github.com/PierreGode/Ragnar / upload_file_api

Function upload_file_api

webapp_modern.py:16289–16331  ·  view source on GitHub ↗

Upload a file

()

Source from the content-addressed store, hash-verified

16287 body = _wardrift_body(r)
16288 if r.status_code == 409 and body.get('error') == 'duplicate_route':
16289 return {'ok': True, 'status': 409, 'mode': 'route', 'duplicate': True, 'response': body}
16290 if r.status_code == 401:
16291 return {'ok': False, 'status': 401, 'mode': 'route',
16292 'error': 'Wardrift session expired - sign in again', 'response': body}
16293 return {'ok': bool(r.ok and body.get('accepted', r.ok)), 'status': r.status_code,
16294 'mode': 'route', 'response': body}
16295
16296
16297def _upload_wardrift_signals(csv_text, key):
16298 import requests
16299 from datetime import datetime, timezone
16300 items = _wardrift_signal_items(csv_text)
16301 if not items:
16302 return {'ok': False, 'mode': 'signals', 'error': 'No GPS-located rows to send'}
16303 key_fp = hashlib.sha256(key.encode()).hexdigest()[:16]
16304 totals = {'batches': 0, 'duplicates': 0, 'item_count': 0, 'awarded_exp': 0, 'awarded_currency': 0}
16305 for i in range(0, len(items), WARDRIFT_BATCH_SIZE):
16306 chunk = items[i:i + WARDRIFT_BATCH_SIZE]
16307 digest = hashlib.sha256(json.dumps(chunk, sort_keys=True).encode()).hexdigest()
16308 idem = str(uuid.uuid5(uuid.NAMESPACE_URL, f'ragnar-wardrift:{key_fp}:{digest}'))
16309 seq = _wardrift_seq_for(idem)
16310 for attempt in (0, 1):
16311 env = {'idempotency_key': idem, 'sequence_no': seq,
16312 'device_ts': datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ'),
16313 'signature': '', 'items': chunk}
16314 r = requests.post(_wardrift_base() + '/v1/ingest/signals', json=env,
16315 headers={'X-API-Key': key}, timeout=60)
16316 body = _wardrift_body(r)
16317 # Our sequence fell behind the server (key used elsewhere / state
16318 # lost) -> take a fresh, higher sequence_no once and resend.
16319 msg = f"{body.get('error', '')} {body.get('message', '')}".lower()
16320 if attempt == 0 and not r.ok and ('replay' in msg or 'sequence' in msg):
16321 seq = _wardrift_seq_for(idem, fresh=True)
16322 continue
16323 break
16324 if not (r.ok and (body.get('accepted') or body.get('duplicate'))):
16325 return {'ok': False, 'status': r.status_code, 'mode': 'signals', 'response': body,
16326 'error': body.get('message') or body.get('error') or f'HTTP {r.status_code}',
16327 'sent': totals['item_count']}
16328 _wardrift_seq_done(idem)
16329 totals['batches'] += 1
16330 totals['duplicates'] += 1 if body.get('duplicate') else 0
16331 totals['item_count'] += int(body.get('item_count') or len(chunk))
16332 totals['awarded_exp'] += int(body.get('awarded_exp') or 0)
16333 totals['awarded_currency'] += int(body.get('awarded_currency') or 0)
16334 return {'ok': True, 'status': 200, 'mode': 'signals', 'response': totals}

Callers

nothing calls this directly

Calls 3

infoMethod · 0.80
errorMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected