Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
FantasticFiasco committed Dec 29, 2024
1 parent 50a532c commit 08995af
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 137 deletions.
6 changes: 3 additions & 3 deletions packages/axis-core/sample/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Connection, get, Protocol, Response } from '../src';
import { Connection, get, Protocol } from '../src';

(async () => {
const connection = new Connection(Protocol.Http, '<ip address>', 80, 'root', '<password>');
const res: Response = await get(connection, '/axis-cgi/param.cgi?action=list&group=Brand.ProdShortName');

console.log('Status code:', res.statusCode);
console.log('Status:', res.status);
console.log('Headers:', res.headers);
console.log('Body:', res.body);
console.log('Body:', await res.text());
})();
20 changes: 3 additions & 17 deletions packages/axis-core/src/DeviceRequest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as got from 'got';
import { get } from './client';
import { Connection } from './Connection';
import { RequestError, UnauthorizedError } from './errors';

/**
* Abstract class describing a HTTP request.
Expand All @@ -22,20 +20,8 @@ export abstract class DeviceRequest {
* Sends a HTTP GET request to a device.
* @param relativePath The relative path.
*/
protected async get(relativePath: string): Promise<Buffer> {
try {
const res = await get(this.connection, relativePath);
return res.body;
} catch (error) {
if (error instanceof got.HTTPError && error.response.statusCode === 401) {
throw new UnauthorizedError();
}
if (error instanceof got.RequestError) {
throw new RequestError(error, error.message, error.code);
}

// Fallback
throw error;
}
protected async get(relativePath: string): Promise<Response> {
const res = await get(this.connection, relativePath);
return res;
}
}
19 changes: 0 additions & 19 deletions packages/axis-core/src/Response.ts

This file was deleted.

50 changes: 47 additions & 3 deletions packages/axis-core/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,59 @@
import { clientProvider } from './auth/client-provider';
import { Connection } from './Connection';
import { Response } from './Response';
import { parse } from './auth/challenge';
import * as basic from './auth/basic';
import * as digest from './auth/digest';

/**
* Send a HTTP GET request to the device.
* @param connection The connection to the device.
* @param relativePath The relative path.
*/
export const get = (connection: Connection, relativePath: string): Promise<Response> => {
return send("GET", connection, relativePath);
};

const send = async (method: string, connection: Connection, relativePath: string): Promise<Response> => {
const url = connection.url + format(relativePath);
return clientProvider('GET', url, connection.username, connection.password, connection?.options?.agent).get<Buffer>(url);
const options: RequestInit = {
method
}

let res = await fetch(url, options);
if (res.status !== 401) {
return res;
}

const wwwAuthenticate = res.headers.get('www-authenticate');
if (!wwwAuthenticate) {
return res;
}

const challenge = parse(wwwAuthenticate);
switch (challenge.type) {
case basic.BASIC:
options.headers = {
authorization: basic.createHeader(connection.username, connection.password, challenge)
};
break;

case digest.DIGEST:
options.headers = {
authorization: digest.createHeader(
method,
url,
connection.username,
connection.password,
challenge,
challenge.qop === 'auth' ? digest.createCnonce() : undefined)
};
break;

default:
return res;
}

res = await fetch(url, options);
return res;
};

const format = (relativePath: string): string => {
Expand Down
27 changes: 0 additions & 27 deletions packages/axis-core/src/errors/RequestError.ts

This file was deleted.

4 changes: 0 additions & 4 deletions packages/axis-core/src/errors/UnauthorizedError.ts

This file was deleted.

17 changes: 0 additions & 17 deletions packages/axis-core/src/errors/UnknownError.ts

This file was deleted.

3 changes: 0 additions & 3 deletions packages/axis-core/src/errors/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/axis-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ export * from './client';
export * from './Connection';
export * from './DeviceRequest';
export * from './DeviceResponse';
export * from './errors';
export * from './Protocol';
export * from './Response';
27 changes: 0 additions & 27 deletions packages/axis-core/test/auth/client-provider.spec.ts

This file was deleted.

23 changes: 8 additions & 15 deletions packages/axis-core/test/client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { HTTPError } from 'got';
import { URL } from 'url';
import { Connection, get, Protocol } from '../src';
import { WebServer } from './web-server';
Expand All @@ -23,8 +22,8 @@ describe('#get should', () => {
const got = await get(connection, relativePath);

// Assert
expect(got.statusCode).toBe(200);
expect(got.body.toString()).toBe('Success');
expect(got.status).toBe(200);
await expect(got.text()).resolves.toBe('Success');
});

test('succeed given basic authentication', async () => {
Expand All @@ -35,8 +34,8 @@ describe('#get should', () => {
const got = await get(connection, relativePath);

// Assert
expect(got.statusCode).toBe(200);
expect(got.body.toString()).toBe('Success');
expect(got.status).toBe(200);
await expect(got.text()).resolves.toBe('Success');
});

test('succeed given digest authentication', async () => {
Expand All @@ -47,8 +46,8 @@ describe('#get should', () => {
const got = await get(connection, relativePath);

// Assert
expect(got.statusCode).toBe(200);
expect(got.body.toString()).toBe('Success');
expect(got.status).toBe(200);
await expect(got.text()).resolves.toBe('Success');
});

test('throw error given invalid credentials', async () => {
Expand All @@ -66,14 +65,8 @@ describe('#get should', () => {
const { connection, relativePath } = parseUrl(url, username, password);

// Act
try {
await get(connection, relativePath);
throw new Error('This exception should not be thrown');
} catch (error) {
// Assert
expect(error).toBeInstanceOf(HTTPError);
expect((error as HTTPError).response.statusCode).toBe(401);
}
const res = await get(connection, relativePath);
expect(res.status).toBe(401);
}
});
});
Expand Down

0 comments on commit 08995af

Please sign in to comment.