MCPcopy Index your code
hub / github.com/googleapis/google-api-python-client / document_api

Function document_api

scripts/describe.py:394–460  ·  view source on GitHub ↗

Document the given API. Args: name (str): Name of the API. version (str): Version of the API. uri (str): URI of the API's discovery document doc_destination_dir (str): relative path where the reference documentation should be saved. artifact_d

(
    name,
    version,
    uri,
    doc_destination_dir,
    artifact_destination_dir=DISCOVERY_DOC_DIR,
)

Source from the content-addressed store, hash-verified

392
393
394def document_api(
395 name,
396 version,
397 uri,
398 doc_destination_dir,
399 artifact_destination_dir=DISCOVERY_DOC_DIR,
400):
401 """Document the given API.
402
403 Args:
404 name (str): Name of the API.
405 version (str): Version of the API.
406 uri (str): URI of the API's discovery document
407 doc_destination_dir (str): relative path where the reference
408 documentation should be saved.
409 artifact_destination_dir (Optional[str]): relative path where the discovery
410 artifacts should be saved.
411 """
412 http = build_http()
413 resp, content = http.request(uri)
414
415 if resp.status == 200:
416 discovery = json.loads(content)
417 service = build_from_document(discovery)
418 doc_name = "{}.{}.json".format(name, version)
419 discovery_file_path = artifact_destination_dir / doc_name
420 revision = None
421
422 pathlib.Path(discovery_file_path).touch(exist_ok=True)
423
424 # Write discovery artifact to disk if revision equal or newer
425 with open(discovery_file_path, "r+") as f:
426 try:
427 json_data = json.load(f)
428 revision = json_data["revision"]
429 except json.JSONDecodeError:
430 revision = None
431
432 if revision is None or discovery["revision"] >= revision:
433 # Reset position to the beginning
434 f.seek(0)
435 # Write the changes to disk
436 json.dump(discovery, f, indent=0, sort_keys=True)
437 # Truncate anything left as it's not needed
438 f.truncate()
439
440 elif resp.status == 404:
441 print(
442 "Warning: {} {} not found. HTTP Code: {}".format(name, version, resp.status)
443 )
444 return
445 else:
446 print(
447 "Warning: {} {} could not be built. HTTP Code: {}".format(
448 name, version, resp.status
449 )
450 )
451 return

Callers 1

Calls 5

build_httpFunction · 0.90
build_from_documentFunction · 0.90
safe_versionFunction · 0.70
requestMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…