Skip to content

Instantly share code, notes, and snippets.

@ntotten
Last active May 12, 2023 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ntotten/eb2cb257938163018c80f1988f1a1fd2 to your computer and use it in GitHub Desktop.
Save ntotten/eb2cb257938163018c80f1988f1a1fd2 to your computer and use it in GitHub Desktop.
// This was generated with ChatGTP as an expiriment. If you find this, don't trust that the
// code works. I didn't test this or anything. See: https://twitter.com/ntotten/status/1657012479942066177
class HubspotSDK {
private apiKey: string;
private baseUrl: string = 'https://api.hubapi.com';
constructor(apiKey: string) {
this.apiKey = apiKey;
}
private async request(
path: string,
method: string,
body?: Record<string, any>
): Promise<any> {
const url = `${this.baseUrl}${path}?hapikey=${this.apiKey}`;
const options: RequestInit = {
method,
headers: {
'Content-Type': 'application/json',
},
};
if (body) {
options.body = JSON.stringify(body);
}
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`HTTP error: ${response.status} - ${response.statusText}`);
}
return await response.json();
}
private async backoffRetry(fn: () => Promise<any>, delay: number): Promise<any> {
await new Promise(resolve => setTimeout(resolve, delay));
return await fn();
}
public async createContact(contact: Record<string, any>): Promise<any> {
try {
return await this.request('/contacts/v1/contact', 'POST', contact);
} catch (error) {
// Retry after 100ms
return this.backoffRetry(() => this.createContact(contact), 100);
}
}
public async updateContact(contactId: string, contact: Record<string, any>): Promise<any> {
try {
return await this.request(`/contacts/v1/contact/vid/${contactId}/profile`, 'POST', contact);
} catch (error) {
// Retry after 100ms
return this.backoffRetry(() => this.updateContact(contactId, contact), 100);
}
}
public async deleteContact(contactId: string): Promise<void> {
try {
await this.request(`/contacts/v1/contact/vid/${contactId}`, 'DELETE');
} catch (error) {
// Retry after 100ms
await this.backoffRetry(() => this.deleteContact(contactId), 100);
}
}
public async createCompany(company: Record<string, any>): Promise<any> {
try {
return await this.request('/companies/v1/companies', 'POST', company);
} catch (error) {
// Retry after 500ms
return this.backoffRetry(() => this.createCompany(company), 500);
}
}
public async updateCompany(companyId: string, company: Record<string, any>): Promise<any> {
try {
return await this.request(`/companies/v1/companies/${companyId}`, 'PUT', company);
} catch (error) {
// Retry after 500ms
return this.backoffRetry(() => this.updateCompany(companyId, company), 500);
}
}
public async deleteCompany(companyId: string): Promise<void> {
try {
await this.request(`/companies/v1/companies/${companyId}`, 'DELETE');
} catch (error) {
// Retry after 500ms
await this.backoffRetry(() => this.deleteCompany(companyId), 500);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment