Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
StarryShark committed Aug 14, 2020
1 parent a8640be commit dc74b4b
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
.env
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Deno",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "deno",
"runtimeArgs": ["run", "--inspect-brk", "-A", "${file}"],
"port": 9229,
"outputCapture": "std"
}
]
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"deno.enable": true
}
21 changes: 21 additions & 0 deletions examples/getHost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Zabbix } from "../mod.ts";

const zabbix: Zabbix = new Zabbix({
url: "http://127.0.0.1/api_jsonrpc.php",
user: "Admin",
password: "zabbix",
});

const main = async () => {
try {
await zabbix.login();
const hosts = await zabbix.request("host.get", {
limit: 1,
});
console.log(JSON.stringify(hosts, null, 2));
zabbix.logout();
} catch (error) {
console.error(error);
}
};
main()
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Zabbix } from "./src/zabbix.ts";
41 changes: 41 additions & 0 deletions src/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export interface IPostConfigBodyParams {
user?: string;
password?: string;
// deno-lint-ignore no-explicit-any
[propName: string]: any;
}

interface IPostConfig {
url: string;
auth: string | null;
method: string;
params: IPostConfigBodyParams;
options?: RequestInit;
}

export async function Post(config: IPostConfig) {
try {
const url = config.url;
const auth = config.auth;
const method = config.method;
const params = config.params;
const options = config.options;

const configInit: RequestInit = Object.assign({
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
jsonrpc: "2.0",
method: method,
params: params,
id: String(Math.random()),
auth: auth,
}),
}, options);

const response = await fetch(url, configInit);
return response.json();
} catch (error) {
throw error;
}
}
69 changes: 69 additions & 0 deletions src/zabbix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Post, IPostConfigBodyParams } from "./fetch.ts";

interface IZabbixConfig {
url: string;
user: string;
password: string;
options?: RequestInit;
}

export class Zabbix {
private url: string;
private user: string;
private password: string;
private options?: RequestInit;
private auth: string | null;

constructor(config: IZabbixConfig) {
this.url = config.url;
this.user = config.user;
this.password = config.password;
this.options = config.options || {};
this.auth = null;
}

async request(method: string, params: IPostConfigBodyParams) {
try {
const response = await Post({
url: this.url,
method: method,
params: params,
auth: this.auth,
options: this.options,
});

if (response.result) {
return response.result;
} else {
throw JSON.stringify(response);
}
} catch (error) {
throw error;
}
}

async login() {
try {
const result = await this.request("user.login", {
user: this.user,
password: this.password,
});
this.auth = result;
return result;
} catch (error) {
throw error;
}
}

async logout() {
try {
const result = await this.request("user.logout", []);
this.auth = null;
return result;
} catch (error) {
throw error;
} finally {
this.auth = null;
}
}
}

0 comments on commit dc74b4b

Please sign in to comment.