Skip to content

Commit

Permalink
release: upgrading Google Ads API to v10 (Opteo#364)
Browse files Browse the repository at this point in the history
* release: upgrading Google Ads API to v10

* eslint formatting

* added forced error type to service factory errors

Co-authored-by: WillCooter <[email protected]>
  • Loading branch information
kritzware and wcoots authored Feb 24, 2022
1 parent 967df31 commit 04dd101
Show file tree
Hide file tree
Showing 10 changed files with 21,237 additions and 22,123 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</p>
<p align="center">
<a href="https://developers.google.com/google-ads/api/docs/release-notes">
<img src="https://img.shields.io/badge/google%20ads-v9.1.0-009688.svg?style=flat-square">
<img src="https://img.shields.io/badge/google%20ads-v10.0.0-009688.svg?style=flat-square">
</a>
<a href="https://www.npmjs.com/package/google-ads-api">
<img src="https://img.shields.io/npm/v/google-ads-api.svg?style=flat-square">
Expand Down Expand Up @@ -102,7 +102,7 @@ const customer = client.Customer({

## List accessible customers

This is a special client method for listing the accessible customers for a given refresh token, and is equivalent to [CustomerService.listAccessibleCustomers](https://developers.google.com/google-ads/api/reference/rpc/v9/CustomerService#listaccessiblecustomers). It returns the resource names of available customer accounts.
This is a special client method for listing the accessible customers for a given refresh token, and is equivalent to [CustomerService.listAccessibleCustomers](https://developers.google.com/google-ads/api/reference/rpc/v10/CustomerService#listaccessiblecustomers). It returns the resource names of available customer accounts.

```ts
const client = new GoogleAdsApi({
Expand Down Expand Up @@ -566,9 +566,9 @@ const customer = client.Customer({

## Error handling

All errors, apart from GRPC specific cases (such as a connection problem or timeout, [see more here](https://github.com/grpc/grpc/blob/master/doc/statuscodes.md)), are instances of a [GoogleAdsFailure](https://developers.google.com/google-ads/api/reference/rpc/v9/GoogleAdsFailure).
All errors, apart from GRPC specific cases (such as a connection problem or timeout, [see more here](https://github.com/grpc/grpc/blob/master/doc/statuscodes.md)), are instances of a [GoogleAdsFailure](https://developers.google.com/google-ads/api/reference/rpc/v10/GoogleAdsFailure).

You can find a list of all error types for a specific version in [the official documentation](https://developers.google.com/google-ads/api/reference/rpc/v9/AccessInvitationErrorEnum.AccessInvitationError), as well as more information about [handling errors here](https://developers.google.com/google-ads/api/docs/best-practices/error-types).
You can find a list of all error types for a specific version in [the official documentation](https://developers.google.com/google-ads/api/reference/rpc/v10/AccessInvitationErrorEnum.AccessInvitationError), as well as more information about [handling errors here](https://developers.google.com/google-ads/api/docs/best-practices/error-types).

```ts
import { errors } from "google-ads-api";
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"author": "Opteo",
"license": "MIT",
"dependencies": {
"google-ads-node": "^7.0.0",
"google-ads-node": "^8.0.0",
"google-auth-library": "^7.1.0",
"google-gax": "^2.28.1",
"google-gax": "^2.29.7",
"long": "^4.0.0"
},
"devDependencies": {
Expand Down
51 changes: 9 additions & 42 deletions scripts/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,6 @@ export default class ServiceFactory extends Service {
for (const [methodName, methodDef] of Object.entries<MethodDefinition>(
service.methods
)) {
if (methodName.startsWith("Get")) {
const getMethod = compileGetMethod(methodName, methodDef);
compiledMethods.push(getMethod);
continue;
}
if (methodName.startsWith("Mutate")) {
const { mutateMethods, mutateOptions } = compileMutateMethods(
name,
Expand Down Expand Up @@ -154,10 +149,16 @@ function compileSpecialMethod(
const requestType = `services.${methodDef.requestType}`;

// Some special methods use types such as google.longrunning.Operation
const responseType = methodDef.responseType.includes("google.")
let responseType = methodDef.responseType.includes("google.")
? methodDef.responseType.split("google.")[1]
: `services.${methodDef.responseType}`;

if (methodDef.responseType.includes(`.googleads.${googleAdsVersion}`)) {
[, responseType] = methodDef.responseType.split(
`.googleads.${googleAdsVersion}.`
);
}

return `
/**
* @link ${GOOGLE_ADS_DOCS_URL}/rpc/${VERSION}/${serviceName}#${methodName.toLowerCase()}
Expand All @@ -173,41 +174,7 @@ function compileSpecialMethod(
});
return response;
} catch (err) {
throw this.getGoogleAdsError(err);
}
}
`;
}

function compileGetMethod(
methodName: string,
methodDef: MethodDefinition
): string {
const serviceMethod = toCamelCase(methodName);
const requestType = `services.${methodDef.requestType}`;
const responseType = methodDef.responseType.split(`${VERSION}.`)[1];

return `
/**
* @description Retrieve a ${responseType} in full detail
* @warning Don't use get in production!
* @returns ${responseType}
*/
get: async (resourceName: string): Promise<${responseType}> => {
const request = new ${requestType}({
resource_name: resourceName,
});
try {
// @ts-expect-error Response is an array type
const [response] = await service.${serviceMethod}(request, {
// @ts-expect-error This arg doesn't exist in the type definitions
otherArgs: {
headers: this.callHeaders,
},
});
return response;
} catch (err) {
throw this.getGoogleAdsError(err);
throw this.getGoogleAdsError(err as Error);
}
}
`;
Expand Down Expand Up @@ -423,7 +390,7 @@ function buildMutateHookEnd(response: string) {
}

function buildMutateHookError() {
return `const googleAdsError = this.getGoogleAdsError(err);
return `const googleAdsError = this.getGoogleAdsError(err as Error);
if (this.hooks.onMutationError) {
await this.hooks.onMutationError({
...baseHookArguments,
Expand Down
Loading

0 comments on commit 04dd101

Please sign in to comment.