Skip to content

Commit 14538a8

Browse files
committed
Clean up exceptions, integrate rxjs to request
1 parent 3d2ac53 commit 14538a8

File tree

4 files changed

+25
-35
lines changed

4 files changed

+25
-35
lines changed

src/core/request.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { IgApiClient } from '../client';
21
import { inRange, random } from 'lodash';
2+
import { Subject } from 'rxjs';
33
import * as request from 'request-promise';
44
import { Options, Response } from 'request';
5+
import hmac = require('crypto-js/hmac-sha256');
6+
import { IgApiClient } from '../client';
57
import {
68
ActionSpamError,
79
AuthenticationError,
@@ -10,9 +12,6 @@ import {
1012
RequestError,
1113
SentryBlockError,
1214
} from '../exceptions';
13-
import { plainToClass } from 'class-transformer';
14-
import { CheckpointResponse } from '../responses';
15-
import hmac = require('crypto-js/hmac-sha256');
1615

1716
type Payload = { [key: string]: any } | string;
1817

@@ -22,6 +21,7 @@ interface SignedPost {
2221
}
2322

2423
export class Request {
24+
end$ = new Subject();
2525
constructor(private client: IgApiClient) {}
2626

2727
private static requestTransform(body, response: Response, resolveWithFullResponse) {
@@ -37,15 +37,15 @@ export class Request {
3737
return resolveWithFullResponse ? response : response.body;
3838
}
3939

40-
private static handleError(response: Response) {
40+
private handleError(response: Response) {
4141
const json = response.body;
4242
if (json.spam) {
4343
return new ActionSpamError(response);
4444
}
4545
if (typeof json.message === 'string') {
4646
if (json.message === 'challenge_required') {
47-
const checkpointResponse = plainToClass(CheckpointResponse, json as CheckpointResponse);
48-
return new CheckpointError(checkpointResponse);
47+
this.client.state.checkpoint = json;
48+
return new CheckpointError(response);
4949
}
5050
if (json.message === 'login_required') {
5151
return new AuthenticationError(response);
@@ -80,7 +80,8 @@ export class Request {
8080
if (response.body.status === 'ok') {
8181
return response;
8282
}
83-
const error = Request.handleError(response);
83+
const error = this.handleError(response);
84+
this.end$.next();
8485
throw error;
8586
}
8687

src/core/state.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
import * as _ from 'lodash';
2+
import * as Bluebird from 'bluebird';
13
import * as Chance from 'chance';
4+
import { jar } from 'request';
5+
import { MemoryCookieStore, Cookie, CookieJar } from 'tough-cookie';
26
import * as devices from '../samples/devices.json';
37
import * as builds from '../samples/builds.json';
48
import * as loginExperiments from '../samples/login-experiments.json';
59
import * as experiments from '../samples/experiments.json';
6-
import { MemoryCookieStore, Cookie, CookieJar } from 'tough-cookie';
7-
import { jar } from 'request';
810
import * as CONSTANTS from './constants';
9-
import * as _ from 'lodash';
10-
import * as Bluebird from 'bluebird';
1111
import { TLD } from './constants';
12+
import { CheckpointResponse } from '../responses';
1213

1314
export class State {
1415
signatureKey: string = '19ce5f445dbfd9d29c59dc2a78c616a7fc090a8e018b9267bc4240a30244c53b';
@@ -36,6 +37,7 @@ export class State {
3637
proxyUrl: string;
3738
cookieStore = new MemoryCookieStore();
3839
cookieJar = jar(this.cookieStore);
40+
checkpoint: CheckpointResponse = null;
3941
get CSRFToken() {
4042
const cookies = this.cookieJar.getCookies(CONSTANTS.HOST);
4143
const item = _.find(cookies, { key: 'csrftoken' });

src/exceptions.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import * as _ from 'lodash';
21
import { CustomError } from 'ts-custom-error';
3-
import * as routes from './core/routes';
42
import { CheckpointResponse } from './responses';
53
import { Response } from 'request';
64
import { LoginRequiredResponse, SpamResponse } from './responses';
@@ -38,20 +36,13 @@ export class ActionSpamError extends RequestError<SpamResponse> {
3836
}
3937
}
4038

41-
export class CheckpointError extends APIError {
42-
constructor(public json: CheckpointResponse) {
43-
super('Instagram call checkpoint for this action!');
44-
}
45-
39+
export class CheckpointError extends RequestError<CheckpointResponse> {
4640
get url() {
47-
if (_.isObject(this.json.challenge) && _.isString(this.json.challenge.url)) {
48-
return this.json.challenge.url;
49-
}
50-
return routes.getWebUrl('challenge');
41+
return this.response.body.challenge.url;
5142
}
5243

5344
get apiUrl() {
54-
return 'https://i.instagram.com/api/v1' + this.json.challenge.api_path;
45+
return 'https://i.instagram.com/api/v1' + this.response.body.challenge.api_path;
5546
}
5647
}
5748

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
import { Type } from 'class-transformer';
2-
3-
class Challenge {
1+
export interface CheckpointResponse {
2+
message: string;
3+
challenge: CheckpointResponseChallenge;
4+
status: string;
5+
error_type: string;
6+
}
7+
export interface CheckpointResponseChallenge {
48
url: string;
59
api_path: string;
610
hide_webview_header: boolean;
711
lock: boolean;
812
logout: boolean;
913
native_flow: boolean;
1014
}
11-
12-
export class CheckpointResponse {
13-
message: string;
14-
@Type(() => Challenge)
15-
challenge: Challenge;
16-
status: string;
17-
error_type: string;
18-
}

0 commit comments

Comments
 (0)