Skip to content

Commit

Permalink
simple awnAPI connector
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanWalsh95 committed Mar 9, 2021
1 parent ee39a6b commit f8756ea
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
7 changes: 7 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
},
"connectors": {
"discord": "Discord Login Token",
"awnAPI": {
"orgID": "YourOrgID",
"creds": {
"username": "AwnUsername",
"password": "AwnPassword"
}
},
"mysql": {
"host": "host",
"port": 3306,
Expand Down
7 changes: 7 additions & 0 deletions squad-server/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fileURLToPath } from 'url';

import Discord from 'discord.js';
import sequelize from 'sequelize';
import AwnAPI from './utils/awn-api.js';

import Logger from 'core/logger';

Expand Down Expand Up @@ -129,6 +130,12 @@ export default class SquadServerFactory {
return connector;
}

if (type === 'awnAPI') {
const awn = new AwnAPI(connectorConfig);
await awn.auth(connectorConfig);
return awn;
}

throw new Error(`${type.connector} is an unsupported connector type.`);
}

Expand Down
1 change: 1 addition & 0 deletions squad-server/templates/config-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"connectors": {
"discord": "Discord Login Token",
"awnAPI": {"orgID":"YourOrgID", "creds": {"username":"AwnUsername", "password":"AwnPassword"}},
"mysql": {
"host": "host",
"port": 3306,
Expand Down
87 changes: 87 additions & 0 deletions squad-server/utils/awn-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import axios from 'axios';
import Logger from 'core/logger';

export default class AwnAPI {
constructor(options) {
this.orgID = options.orgID;
this.xAuthToken = null;
}

async auth(options) {
const requestData = {
username: `${options.creds.username}`,
password: `${options.creds.password}`
};
const res = await this.request('post', '/v5/auth/login', requestData);

if (res.status === 200 && res.data.valid) {
this.xAuthToken = res.data.accessToken;
Logger.verbose('awnAPI', 1, `Authentication succeeded.`);
}
}

async request(method, endpoint, data) {
let ret = null;

if (!endpoint.startsWith('/')) endpoint = `/${endpoint}`;
if (!endpoint.startsWith('/v5/')) endpoint = `/v5/org/${this.orgID}${endpoint}`;
if (endpoint.includes(':orgId')) endpoint = endpoint.replace(':orgId', `${this.orgID}`);

try {
const axiosRequest = {
method: method,
url: `https://api.awn.gg${endpoint}`,
data: data,
headers: { 'X-AWN-ACCESS-TOKEN': this.xAuthToken }
};

Logger.verbose('awnAPI', 1, `${axiosRequest.method.toUpperCase()}: ${axiosRequest.url}`);
Logger.verbose('awnAPI', 3, `Request Data: ${JSON.stringify(data)}`);

const res = await axios(axiosRequest);

ret = { status: res.status, statusText: res.statusText, data: res.data };
Logger.verbose('awnAPI', 3, `${JSON.stringify(ret)}`);
return ret;
} catch (err) {
ret = {
status: err.response.status,
statusText: err.response.statusText,
error: err.response.data.error
};
Logger.verbose('awnAPI', 1, `ERROR: ${JSON.stringify(err.response.status)}`);
Logger.verbose('awnAPI', 3, `ERROR: ${JSON.stringify(ret)}`);
return ret;
}
}

async addAdmin(listID, steamID) {
const ret = await this.request('post', `game-servers/admin-lists/${listID}/admins`, {
type: 'steam64',
value: `${steamID}`
});
ret.success = ret.status === 200;
return ret;
}

async getAdmin(listID, adminID) {
const ret = await this.request('get', `game-servers/admin-lists/${listID}/admins/${adminID}`);
ret.success = ret.status === 200;
return ret;
}

async removeAdmin(listID, adminID) {
const ret = await this.request(
'delete',
`game-servers/admin-lists/${listID}/admins/${adminID}`
);
ret.success = ret.status === 204;
return ret;
}

async getAdminList(listID) {
const ret = await this.request('get', `game-servers/admin-lists/${listID}`);
ret.success = ret.status === 200;
return ret;
}
}

0 comments on commit f8756ea

Please sign in to comment.