|
44 | 44 | "import secrets, httpx" |
45 | 45 | ] |
46 | 46 | }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": null, |
| 50 | + "id": "b560f0c3", |
| 51 | + "metadata": {}, |
| 52 | + "outputs": [], |
| 53 | + "source": [ |
| 54 | + "#| hide\n", |
| 55 | + "from nbdev.showdoc import show_doc" |
| 56 | + ] |
| 57 | + }, |
47 | 58 | { |
48 | 59 | "cell_type": "code", |
49 | 60 | "execution_count": null, |
|
483 | 494 | " def get_auth(self, info, ident, session, state): raise NotImplementedError()" |
484 | 495 | ] |
485 | 496 | }, |
| 497 | + { |
| 498 | + "cell_type": "markdown", |
| 499 | + "id": "538de5d3", |
| 500 | + "metadata": {}, |
| 501 | + "source": [ |
| 502 | + "### Google helpers" |
| 503 | + ] |
| 504 | + }, |
| 505 | + { |
| 506 | + "cell_type": "code", |
| 507 | + "execution_count": null, |
| 508 | + "id": "be2990cf", |
| 509 | + "metadata": {}, |
| 510 | + "outputs": [], |
| 511 | + "source": [ |
| 512 | + "#| export\n", |
| 513 | + "try:\n", |
| 514 | + " from google.oauth2.credentials import Credentials\n", |
| 515 | + " from google.auth.transport.requests import Request\n", |
| 516 | + "except ImportError:\n", |
| 517 | + " Request=None\n", |
| 518 | + " class Credentials: pass" |
| 519 | + ] |
| 520 | + }, |
| 521 | + { |
| 522 | + "cell_type": "code", |
| 523 | + "execution_count": null, |
| 524 | + "id": "7393bc12", |
| 525 | + "metadata": {}, |
| 526 | + "outputs": [], |
| 527 | + "source": [ |
| 528 | + "#| export\n", |
| 529 | + "@patch\n", |
| 530 | + "def update(self:Credentials):\n", |
| 531 | + " \"Refresh the credentials if they are expired, and return them\"\n", |
| 532 | + " if self.expired: self.refresh(Request())\n", |
| 533 | + " return self" |
| 534 | + ] |
| 535 | + }, |
| 536 | + { |
| 537 | + "cell_type": "code", |
| 538 | + "execution_count": null, |
| 539 | + "id": "b68863e4", |
| 540 | + "metadata": {}, |
| 541 | + "outputs": [ |
| 542 | + { |
| 543 | + "data": { |
| 544 | + "text/markdown": [ |
| 545 | + "---\n", |
| 546 | + "\n", |
| 547 | + "### Credentials.update\n", |
| 548 | + "\n", |
| 549 | + "> Credentials.update ()\n", |
| 550 | + "\n", |
| 551 | + "*Refresh the credentials if they are expired, and return them*" |
| 552 | + ], |
| 553 | + "text/plain": [ |
| 554 | + "---\n", |
| 555 | + "\n", |
| 556 | + "### Credentials.update\n", |
| 557 | + "\n", |
| 558 | + "> Credentials.update ()\n", |
| 559 | + "\n", |
| 560 | + "*Refresh the credentials if they are expired, and return them*" |
| 561 | + ] |
| 562 | + }, |
| 563 | + "execution_count": null, |
| 564 | + "metadata": {}, |
| 565 | + "output_type": "execute_result" |
| 566 | + } |
| 567 | + ], |
| 568 | + "source": [ |
| 569 | + "show_doc(Credentials.update)" |
| 570 | + ] |
| 571 | + }, |
| 572 | + { |
| 573 | + "cell_type": "code", |
| 574 | + "execution_count": null, |
| 575 | + "id": "ace16168", |
| 576 | + "metadata": {}, |
| 577 | + "outputs": [], |
| 578 | + "source": [ |
| 579 | + "#| export\n", |
| 580 | + "@patch\n", |
| 581 | + "def save(self:Credentials, fname):\n", |
| 582 | + " \"Save credentials to `fname`\"\n", |
| 583 | + " save_pickle(fname, self)" |
| 584 | + ] |
| 585 | + }, |
| 586 | + { |
| 587 | + "cell_type": "code", |
| 588 | + "execution_count": null, |
| 589 | + "id": "877a7b20", |
| 590 | + "metadata": {}, |
| 591 | + "outputs": [ |
| 592 | + { |
| 593 | + "data": { |
| 594 | + "text/markdown": [ |
| 595 | + "---\n", |
| 596 | + "\n", |
| 597 | + "### Credentials.save\n", |
| 598 | + "\n", |
| 599 | + "> Credentials.save (fname)\n", |
| 600 | + "\n", |
| 601 | + "*Save credentials to `fname`*" |
| 602 | + ], |
| 603 | + "text/plain": [ |
| 604 | + "---\n", |
| 605 | + "\n", |
| 606 | + "### Credentials.save\n", |
| 607 | + "\n", |
| 608 | + "> Credentials.save (fname)\n", |
| 609 | + "\n", |
| 610 | + "*Save credentials to `fname`*" |
| 611 | + ] |
| 612 | + }, |
| 613 | + "execution_count": null, |
| 614 | + "metadata": {}, |
| 615 | + "output_type": "execute_result" |
| 616 | + } |
| 617 | + ], |
| 618 | + "source": [ |
| 619 | + "show_doc(Credentials.save)" |
| 620 | + ] |
| 621 | + }, |
| 622 | + { |
| 623 | + "cell_type": "code", |
| 624 | + "execution_count": null, |
| 625 | + "id": "5ef02a88", |
| 626 | + "metadata": {}, |
| 627 | + "outputs": [], |
| 628 | + "source": [ |
| 629 | + "#| export\n", |
| 630 | + "def load_creds(fname):\n", |
| 631 | + " \"Load credentials from `fname`\"\n", |
| 632 | + " return load_pickle(fname).update()" |
| 633 | + ] |
| 634 | + }, |
| 635 | + { |
| 636 | + "cell_type": "code", |
| 637 | + "execution_count": null, |
| 638 | + "id": "a0024ab0", |
| 639 | + "metadata": {}, |
| 640 | + "outputs": [], |
| 641 | + "source": [ |
| 642 | + "#| export\n", |
| 643 | + "@patch\n", |
| 644 | + "def creds(self:GoogleAppClient):\n", |
| 645 | + " \"Create `Credentials` from the client, refreshing if needed\"\n", |
| 646 | + " return Credentials(token=self.access_token, refresh_token=self.refresh_token, \n", |
| 647 | + " token_uri=self.token_url, client_id=self.client_id,\n", |
| 648 | + " client_secret=self.client_secret, scopes=self.scope).update()" |
| 649 | + ] |
| 650 | + }, |
| 651 | + { |
| 652 | + "cell_type": "code", |
| 653 | + "execution_count": null, |
| 654 | + "id": "8de24c61", |
| 655 | + "metadata": {}, |
| 656 | + "outputs": [ |
| 657 | + { |
| 658 | + "data": { |
| 659 | + "text/markdown": [ |
| 660 | + "---\n", |
| 661 | + "\n", |
| 662 | + "### GoogleAppClient.creds\n", |
| 663 | + "\n", |
| 664 | + "> GoogleAppClient.creds ()\n", |
| 665 | + "\n", |
| 666 | + "*Create `Credentials` from the client, refreshing if needed*" |
| 667 | + ], |
| 668 | + "text/plain": [ |
| 669 | + "---\n", |
| 670 | + "\n", |
| 671 | + "### GoogleAppClient.creds\n", |
| 672 | + "\n", |
| 673 | + "> GoogleAppClient.creds ()\n", |
| 674 | + "\n", |
| 675 | + "*Create `Credentials` from the client, refreshing if needed*" |
| 676 | + ] |
| 677 | + }, |
| 678 | + "execution_count": null, |
| 679 | + "metadata": {}, |
| 680 | + "output_type": "execute_result" |
| 681 | + } |
| 682 | + ], |
| 683 | + "source": [ |
| 684 | + "show_doc(GoogleAppClient.creds)" |
| 685 | + ] |
| 686 | + }, |
486 | 687 | { |
487 | 688 | "cell_type": "markdown", |
488 | 689 | "id": "474e14b4", |
|
0 commit comments