Skip to content

Commit

Permalink
add region setting
Browse files Browse the repository at this point in the history
  • Loading branch information
josiasmontag committed Jan 22, 2024
1 parent 999c39c commit 9346041
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ const inputFile = fs.createReadStream('./file.pdf');

await cloudConvert.tasks.upload(uploadTask, inputFile, 'file.pdf');
```
> **Note on custom streams**:
The length of the stream needs to be known prior to uploading. The SDK automatically detects the file size of file-based read streams. If you are using a custom stream, you need to pass a `filesize` as fourth parameter to the `upload()` method.


## Websocket Events

Expand Down Expand Up @@ -180,6 +183,15 @@ const cloudConvert = new CloudConvert('api_key', true);

> Don't forget to generate MD5 Hashes for the files you will use for testing.
## Setting a Region

By default, the region in your [account settings](https://cloudconvert.com/dashboard/region) is used. Alternatively, you can set a fixed region:

```js
// Pass the region as third argument to the constructor
const cloudConvert = new CloudConvert('api_key', false, 'us-east');
```

## Contributing

This section is intended for people who want to contribute to the development of this library.
Expand Down
8 changes: 6 additions & 2 deletions lib/CloudConvert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default class CloudConvert {

public readonly apiKey: string;
public readonly useSandbox: boolean;
public readonly region: string | null;

public axios!: AxiosInstance;
public tasks!: TasksResource;
Expand All @@ -24,9 +25,10 @@ export default class CloudConvert {
public webhooks!: WebhooksResource;
public signedUrls!: SignedUrlResource;

constructor(apiKey: string, useSandbox = false) {
constructor(apiKey: string, useSandbox = false, region = null) {
this.apiKey = apiKey;
this.useSandbox = useSandbox;
this.region = region;

this.createAxiosInstance();
this.createResources();
Expand All @@ -36,7 +38,9 @@ export default class CloudConvert {
this.axios = axios.create({
baseURL: this.useSandbox
? 'https://api.sandbox.cloudconvert.com/v2/'
: 'https://api.cloudconvert.com/v2/',
: `https://${
this.region ? this.region + '.' : ''
}api.cloudconvert.com/v2/`,
headers: {
Authorization: `Bearer ${this.apiKey}`,
'User-Agent': `cloudconvert-node/v${version} (https://github.com/cloudconvert/cloudconvert-node)`
Expand Down
6 changes: 5 additions & 1 deletion lib/JobsResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ export default class JobsResource {
const response = await this.cloudConvert.axios.get(`jobs/${id}`, {
baseURL: this.cloudConvert.useSandbox
? 'https://sync.api.sandbox.cloudconvert.com/v2/'
: 'https://sync.api.cloudconvert.com/v2/'
: `https://${
this.cloudConvert.region
? this.cloudConvert.region + '.'
: ''
}sync.api.cloudconvert.com/v2/`
});
return response.data.data;
}
Expand Down
6 changes: 5 additions & 1 deletion lib/TasksResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,11 @@ export default class TasksResource {
const response = await this.cloudConvert.axios.get(`tasks/${id}`, {
baseURL: this.cloudConvert.useSandbox
? 'https://sync.api.sandbox.cloudconvert.com/v2/'
: 'https://sync.api.cloudconvert.com/v2/'
: `https://${
this.cloudConvert.region
? this.cloudConvert.region + '.'
: ''
}sync.api.cloudconvert.com/v2/`
});
return response.data.data;
}
Expand Down

0 comments on commit 9346041

Please sign in to comment.