Skip to content

Commit

Permalink
release: upgrading Google Ads API to v7.0.0 (#271)
Browse files Browse the repository at this point in the history
* v7.0.0

* extracted null or undefined logic to a function

* compiled protos

Co-authored-by: Kritzware <suspiciouspotato@gmail.com>

* removed timeouts

* linting

Co-authored-by: William Cooter <will@cooters.co.uk>
Co-authored-by: Kritzware <suspiciouspotato@gmail.com>
  • Loading branch information
3 people authored May 26, 2021
1 parent ef15399 commit d685a79
Show file tree
Hide file tree
Showing 17 changed files with 19,560 additions and 16,945 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-v6-009688.svg?style=flat-square">
<img src="https://img.shields.io/badge/google%20ads-v7-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 @@ -113,7 +113,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/v6/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/v7/CustomerService#listaccessiblecustomers). It returns the resource names of available customer accounts.

```ts
const client = new GoogleAdsApi({
Expand Down Expand Up @@ -571,9 +571,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/v6/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/v7/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/v6/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/v7/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
6 changes: 3 additions & 3 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": "^4.0.2",
"google-auth-library": "^6.1.3",
"google-gax": "^2.10.2",
"google-ads-node": "^5.0.0",
"google-auth-library": "^7.1.0",
"google-gax": "^2.14.0",
"long": "^4.0.0"
},
"devDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions scripts/fields.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ describe("other checkers", () => {
it("getEnumName", () => {
const attributeEnumField = new resources.GoogleAdsField({
type_url:
"google.ads.googleads.v6.enums.CampaignStatusEnum.CampaignStatus",
"google.ads.googleads.v7.enums.CampaignStatusEnum.CampaignStatus",
});
const metricEnumField = new resources.GoogleAdsField({
type_url:
"google.ads.googleads.v6.enums.QualityScoreBucketEnum.QualityScoreBucket",
"google.ads.googleads.v7.enums.QualityScoreBucketEnum.QualityScoreBucket",
});
const segmentEnumField = new resources.GoogleAdsField({
type_url: "google.ads.googleads.v6.enums.DayOfWeekEnum.DayOfWeek",
type_url: "google.ads.googleads.v7.enums.DayOfWeekEnum.DayOfWeek",
});

expect(getEnumName(attributeEnumField)).toEqual("CampaignStatus");
Expand Down
45 changes: 34 additions & 11 deletions scripts/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ export async function compileFields(): Promise<void> {
const metrics: string[] = [];

fields.forEach((field: resources.GoogleAdsField) => {
if (!isDefinedAndNotNull(field.name)) {
return;
}

const field_name = field.name as string;

if (isResource(field)) {
const resource: Resource = {
attributes: [],
Expand All @@ -70,7 +76,7 @@ export async function compileFields(): Promise<void> {
};

const selectableResources: string[] = [
field.name,
field_name,
...field.selectable_with.filter(
(selectable: string) =>
!selectable.includes("segments.") &&
Expand All @@ -83,24 +89,29 @@ export async function compileFields(): Promise<void> {
const correctResource = selectableResources.find((resource: string) =>
field.resource_name.includes(`/${resource}.`)
);
return isAttribute(field) && correctResource && field.selectable;
return (
isAttribute(field) &&
isDefinedAndNotNull(field.name) &&
correctResource &&
field.selectable
);
})
.map((field: resources.GoogleAdsField): string => field.name);
.map((field: resources.GoogleAdsField): string => field.name as string);

resourceConstructs[field.name] = resource;
resourceConstructs[field_name] = resource;
} else if (isAttribute(field) && field.selectable) {
attributes.push(field.name);
attributes.push(field_name);
if (isResourceName(field)) {
resourceNames.push(field.name);
resourceNames.push(field_name);
}
} else if (isMetric(field) && field.selectable) {
metrics.push(field.name);
metrics.push(field_name);
} else if (isSegment(field) && field.selectable) {
segments.push(field.name);
segments.push(field_name);
}

if (hasEnumDataType(field)) {
enumFields[field.name] = getEnumName(field);
enumFields[field_name] = getEnumName(field);
}
});

Expand Down Expand Up @@ -208,13 +219,25 @@ export function isSegment(field: resources.GoogleAdsField): boolean {
const resourceNameRegex = new RegExp(/^.*\.resource_name$/g);

export function isResourceName(field: resources.GoogleAdsField): boolean {
return resourceNameRegex.test(field.name);
if (isDefinedAndNotNull(field.name)) {
return resourceNameRegex.test(field.name as string);
} else {
return false;
}
}

export function hasEnumDataType(field: resources.GoogleAdsField): boolean {
return field.data_type === "ENUM";
}

export function getEnumName(field: resources.GoogleAdsField): string {
return field.type_url.replace(/.*(?=\.)\./g, "");
if (isDefinedAndNotNull(field.type_url)) {
return (field.type_url as string).replace(/.*(?=\.)\./g, "");
} else {
return "FIELD_HAS_NO_ENUM_NAME";
}
}

function isDefinedAndNotNull(input: string | null | undefined): boolean {
return typeof input !== "undefined" && input !== null;
}
7 changes: 4 additions & 3 deletions scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ async function compileAll(): Promise<void> {
console.log(`compiling only "${onlyRun}"`);
await scripts[onlyRun]();
} else {
console.log("compiling all scripts");
for (const script of Object.values(scripts)) {
console.log(script);
console.log("compiling all scripts...");
for (const [name, script] of Object.entries(scripts)) {
await script();
console.log(`${name} compiled successfully`);
}
console.log("done");
}
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface ProtoDefinition {
[fieldName: string]: {
type: string;
id: number;
options?: Record<string, string>;
options?: Record<string, string | boolean>;
};
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/customer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,7 @@ describe("mutateResources", () => {
partial_failure_error: new google.rpc.Status({
details: [
{
type_url: "google.ads.googleads.v6.errors.GoogleAdsFailure",
type_url: "google.ads.googleads.v7.errors.GoogleAdsFailure",
value: failureBuffer,
},
],
Expand Down
10 changes: 3 additions & 7 deletions src/customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ export class Customer extends ServiceFactory {
constructor(
clientOptions: ClientOptions,
customerOptions: CustomerOptions,
hooks?: Hooks,
timeout = 3600000 // 1 hour
hooks?: Hooks
) {
super(clientOptions, customerOptions, hooks ?? {}, timeout);
super(clientOptions, customerOptions, hooks ?? {});
}

/**
Expand Down Expand Up @@ -129,7 +128,6 @@ export class Customer extends ServiceFactory {

const stream = service.searchStream(request, {
otherArgs: { headers: this.callHeaders },
timeout: this.timeout,
});

let streamFinished = false;
Expand Down Expand Up @@ -230,7 +228,6 @@ export class Customer extends ServiceFactory {

return service.searchStream(request, {
otherArgs: { headers: this.callHeaders },
timeout: this.timeout,
});
}

Expand All @@ -249,7 +246,6 @@ export class Customer extends ServiceFactory {

const searchResponse = await service.search(request, {
otherArgs: { headers: this.callHeaders },
timeout: this.timeout,
autoPaginate: false, // autoPaginate doesn't work
});

Expand Down Expand Up @@ -356,7 +352,7 @@ export class Customer extends ServiceFactory {
}
}

return { response: (response as unknown) as T, totalResultsCount };
return { response: response as unknown as T, totalResultsCount };
} catch (searchError) {
const googleAdsError = this.getGoogleAdsError(searchError);
if (this.hooks.onQueryError && useHooks) {
Expand Down
Loading

0 comments on commit d685a79

Please sign in to comment.