-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(docs): publish Client libraries help to reference documentation #16575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bshaffer
wants to merge
7
commits into
googleapis:main
Choose a base branch
from
bshaffer:client-libraries-help
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9224885
feat(docs): publish Client libraries help to python reference documen…
bshaffer 4aa9b08
fix tests and apply suggestions from code review
bshaffer 3bc7a82
move help to docs/devsite-help
bshaffer 7883727
add getting-started
bshaffer 5d9c033
add generate-devsite-help.sh
bshaffer a0fd613
Apply suggestions from code review
bshaffer 52972e9
add path helper example and remove print()
bshaffer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| # Getting started with client libraries | ||
|
|
||
| The Google Cloud Libraries for Python are a mixture of handwritten and autogenerated | ||
| libraries connecting to Google Cloud services. The handwritten libraries (such | ||
| as [google-cloud-firestore](https://docs.cloud.google.com/python/docs/reference/firestore/latest) and | ||
| [google-cloud-spanner](https://docs.cloud.google.com/python/docs/reference/spanner/latest)) | ||
| are mostly higher level abstractions over the underlying API. See the documentation | ||
| for those individual libraries for details; the documentation here is primarily | ||
| aimed at the autogenerated libraries. | ||
|
|
||
| If you haven't already found the library for the API you're interested in, please consult | ||
| [the list of Python libraries](https://cloud.google.com/python/docs/reference) which shows both the package | ||
| name and the link to the library-specific documentation. In particular, each library has: | ||
|
|
||
| - A "getting started" page which lists the client types within that library | ||
| - Version history for the library | ||
| - API reference documentation | ||
|
|
||
| This page demonstrates using the [google-cloud-translate](https://docs.cloud.google.com/python/docs/reference/translate/latest) | ||
| API as a simple example; the steps required for other APIs are very similar. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| All Google Cloud APIs require a Google Cloud project. If you haven't set one up already, | ||
| please [create one](https://cloud.google.com/resource-manager/docs/creating-managing-projects). | ||
| You'll also need to [enable your chosen API](https://console.cloud.google.com/apis/library) if it hasn't | ||
| already been used within that Google Cloud project. | ||
|
|
||
| There are no specific tools required to develop using the Google Cloud Libraries for Python. All | ||
| development environments should work, but you should check that you're targeting a | ||
| [supported version of Python](https://docs.cloud.google.com/python/docs/supported-python-versions). | ||
|
|
||
| We recommend installing the [gcloud CLI](https://cloud.google.com/sdk/gcloud). | ||
|
|
||
| ## Install the library | ||
|
|
||
| All Google Cloud Libraries for Python are available from [PyPI](https://pypi.org) and can be installed | ||
| using `pip`. If you wish to install a pre-release version, you can specify the version explicitly | ||
| with the installation command. | ||
|
|
||
| The libraries can be installed in any regular environment, including virtual environments (recommended), | ||
| containerized applications, and web frameworks like Django or Flask. | ||
|
|
||
| For the translation example, we'll create a new directory, set up a virtual environment, | ||
| and install the package. | ||
|
|
||
| Note that for simplicity, the sample code below uses synchronous calls. Most libraries also provide | ||
| asynchronous clients (usually with suffix `AsyncClient`, rather than `Client`) for use in naturally asynchronous environments | ||
| using `asyncio`. | ||
|
|
||
| ```sh | ||
| mkdir TranslationExample | ||
| cd TranslationExample | ||
| python3 -m venv venv | ||
| source venv/bin/activate | ||
| pip install google-cloud-translate | ||
| ``` | ||
|
|
||
| > **Dependencies** | ||
| > If you install the library, you may notice | ||
| > transitive dependencies being installed. This is entirely expected, but you may not recognize | ||
| > some of those dependencies. The list below is not comprehensive, but highlights some of the packages | ||
| > you'll see being installed. | ||
| > | ||
| > - protobuf: the library supporting the [Protocol Buffers](https://protobuf.dev) serialization format | ||
| > - google-api-core: support libraries specifically tailored for the Google Cloud client libraries | ||
| > - google-auth: authentication support for Google Cloud credentials | ||
| > - grpcio: support for the [gRPC](https://grpc.io/) RPC protocol | ||
| > - google-cloud-core: common helpers and support for [long-running operations](https://docs.cloud.google.com/python/docs/reference/google-cloud-core/latest) | ||
|
|
||
| ## Create a client | ||
|
|
||
| The first step in making any API calls is to create a client. Some libraries have multiple clients | ||
| for operations involving different resources; others have a single client. In the Translation API | ||
| we're using, we use `TranslationServiceClient` which can be used to create a synchronous client. | ||
|
|
||
| Clients can be configured in a number of ways, but in many cases the defaults are fine. The most | ||
| common reason to use an explicit configuration is to specify credentials for | ||
| [authentication](https://cloud.google.com/docs/authentication/use-cases). For this example, we'll just use | ||
| [Application Default Credentials (ADC)](https://cloud.google.com/docs/authentication/provide-credentials-adc#local-dev). | ||
| To set up ADC in your local environment, follow the instructions in | ||
| [Local development environment](https://cloud.google.com/docs/authentication/provide-credentials-adc#local-dev). | ||
| When you create a client, it automatically detects and uses these credentials. | ||
|
|
||
| To create a client with default settings: | ||
|
|
||
| ```python | ||
| from google.cloud import translate_v3 as translate | ||
|
|
||
| client = translate.TranslationServiceClient() | ||
| ``` | ||
|
|
||
| ## Make requests | ||
|
|
||
| The Google Cloud Libraries use [Protocol Buffers](https://protobuf.dev) | ||
| to represent requests and responses, with some additional types to make the APIs more | ||
| convenient to work with. | ||
|
|
||
| Most APIs are expressed in terms of a single request returning a single response, although | ||
| there are also streaming APIs requiring multiple requests and/or multiple responses. | ||
| For our Translation API example, we'll create a simple request for the `translate_text` API. | ||
|
|
||
| ```python | ||
| from google.cloud import translate_v3 as translate | ||
|
|
||
| client = translate.TranslationServiceClient() | ||
|
|
||
| project_id = "your-project-id-here" | ||
| location_id = "global" | ||
| parent = f"projects/{project_id}/locations/{location_id}" | ||
| request = translate.TranslateTextRequest( | ||
| contents=["It is raining.", "It is sunny."], | ||
| target_language_code="fr-FR", | ||
| parent=parent | ||
| ) | ||
|
|
||
| response = client.translate_text(request=request) | ||
| ``` | ||
|
|
||
| This example demonstrates a few features: | ||
|
|
||
| - Protocol Buffer messages are instantiated with keyword arguments representing the fields | ||
| - Repeated fields (like `contents`) are represented as Python lists | ||
| - The `parent` field is a string representation of a resource name. The library provides helper methods | ||
| on the client to construct these paths, ensuring you don't need to concern yourself | ||
| with the underlying resource name format. | ||
|
bshaffer marked this conversation as resolved.
|
||
| ```python | ||
| # produces the string `projects/your-project-id-here/locations/global` | ||
| parent = client.location_path(project_id, location_id) | ||
| ``` | ||
|
|
||
| The Google Cloud Libraries always expose methods accepting an API request object directly, | ||
| but they also support passing keyword arguments directly to the method for convenience: | ||
|
|
||
| ```python | ||
| response = client.translate_text( | ||
| contents=["It is raining.", "It is sunny."], | ||
| target_language_code="fr-FR", | ||
| parent=parent, | ||
| ) | ||
| ``` | ||
|
|
||
| The response is also a Protocol Buffers message. You can inspect the structure of the response | ||
| by printing it, or by accessing its attributes directly. In this case we'll look at the | ||
| `translations` attribute: | ||
|
|
||
| ```python | ||
| print(f"Translations returned: {len(response.translations)}") | ||
| print() | ||
| for translation in response.translations: | ||
| print(f"Detected language: {translation.detected_language_code}") | ||
| print(f"Translated text: {translation.translated_text}") | ||
| ``` | ||
|
|
||
| This produces output of: | ||
|
|
||
| ```text | ||
| Translations returned: 2 | ||
|
|
||
| Detected language: en | ||
| Translated text: Il pleut. | ||
|
|
||
| Detected language: en | ||
| Translated text: Il fait beau. | ||
| ``` | ||
|
|
||
| The complete code is: | ||
|
|
||
| ```python | ||
| from google.cloud import translate_v3 as translate | ||
|
|
||
| client = translate.TranslationServiceClient() | ||
|
|
||
| project_id = "your-project-id-here" | ||
| location_id = "global" | ||
| parent = f"projects/{project_id}/locations/{location_id}" | ||
|
|
||
| request = translate.TranslateTextRequest( | ||
| contents=["It is raining.", "It is sunny."], | ||
| target_language_code="fr-FR", | ||
| parent=parent, | ||
| ) | ||
|
|
||
| response = client.translate_text(request=request) | ||
|
|
||
| print(f"Translations returned: {len(response.translations)}") | ||
| print() | ||
| for translation in response.translations: | ||
| print(f"Detected language: {translation.detected_language_code}") | ||
| print(f"Translated text: {translation.translated_text}") | ||
| ``` | ||
|
|
||
| This is just a simple example, which hasn't touched on features like pagination | ||
| or specifying call configurations like timeouts and retries. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| - uid: product-neutral-guides | ||
| name: 'Client library help' | ||
| items: | ||
| - | ||
| name: 'Getting Started' | ||
| href: 'getting-started.md' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -e | ||
| REPOROOT=$(git rev-parse --show-toplevel) | ||
|
|
||
| if [[ -z "$1" ]] | ||
| then | ||
| declare -r VERSION="1.0.0" | ||
| else | ||
| declare -r VERSION=$1 | ||
| fi | ||
|
|
||
| DEVSITE_STAGING_BUCKET=docs-staging-v2 | ||
|
|
||
| rm -rf $REPOROOT/docs/output | ||
| mkdir -p $REPOROOT/docs/output | ||
|
|
||
| cp $REPOROOT/docs/devsite-help/* $REPOROOT/docs/output | ||
| cd $REPOROOT/docs/output | ||
|
|
||
| # Create the docs metadata. | ||
| docuploader create-metadata \ | ||
| --name help \ | ||
| --version $VERSION \ | ||
| --language python | ||
|
|
||
| # Upload the | ||
| docuploader upload . \ | ||
| --staging-bucket="$DEVSITE_STAGING_BUCKET" \ | ||
| --destination-prefix="docfx-" \ | ||
| --metadata-file="docs.metadata" | ||
|
|
||
| echo 'Done' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.