Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several improvements and fixes #5

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 52 additions & 30 deletions module/genericsUtils.ts → genericsUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios, {AxiosError} from 'axios'
import { fetch } from 'native-fetch'
const { v4: uuidv4 } = require('uuid')

let endpoint: string = 'https://api.banana.dev/'
Expand All @@ -11,8 +11,9 @@ if ("BANANA_URL" in process.env){
}
}

type Object = { [key: string]: any }

export async function runMain(apiKey: string, modelKey: string, modelInputs: object = {}): Promise<any>{
export async function runMain(apiKey: string, modelKey: string, modelInputs: Object = {}): Promise<any>{
const startOut = await startAPI(apiKey, modelKey, modelInputs)
if (startOut["finished"] == true){
const res = {
Expand All @@ -37,18 +38,18 @@ export async function runMain(apiKey: string, modelKey: string, modelInputs: obj

}

export async function startMain(apiKey: string, modelKey: string, modelInputs: object = {}): Promise<string>{
export async function startMain(apiKey: string, modelKey: string, modelInputs: Object = {}): Promise<string>{
const jsonOut = await startAPI(apiKey, modelKey, modelInputs, true)
return jsonOut["callID"]
}


export async function checkMain(apiKey: string, callID: string): Promise<object>{
export async function checkMain(apiKey: string, callID: string): Promise<Object>{
const jsonOut = await checkAPI(apiKey, callID)
return jsonOut
}

const startAPI = async (apiKey: string, modelKey: string, modelInputs: object, startOnly: boolean = false): Promise<any> => {
const startAPI = async (apiKey: string, modelKey: string, modelInputs: Object, startOnly: boolean = false): Promise<any> => {
const urlStart = endpoint.concat("start/v4/")
const payload = {
"id": uuidv4(),
Expand All @@ -59,25 +60,53 @@ const startAPI = async (apiKey: string, modelKey: string, modelInputs: object, s
"startOnly": startOnly,
}

const response = await axios.post(urlStart, payload).catch(err => {
if (err.response) {
throw `server error: status code ${err.response.status}`
} else if (err.request) {
throw 'server error: endpoint busy or not available.'
} else {
console.log(err)
throw "Misc axios error. Please email [email protected] with above error"
const response = await fetch(urlStart, {
method: 'POST',
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json'
}
})
const jsonOut = response.data
const jsonOut = await getBananaJsonOutput(response)

return jsonOut
}

async function getBananaJsonOutput(response: Response) {
const text = await response.text()
let jsonOut: any = null
try {
jsonOut = JSON.parse(text)
} catch {
throw new BananaError(`Could not parse response from server: ${text}`, )
}

if (jsonOut.message.toLowerCase().includes("error")){
throw jsonOut.message
if (!response.ok) {
if (jsonOut.response) {
throw new BananaError(`${response.status}: server error: status code ${jsonOut.response.status}`, jsonOut)
} else if (jsonOut.request) {
throw new BananaError( `${response.status}: server error: endpoint busy or not available.`, jsonOut)
} else {
console.log(jsonOut)
throw new BananaError(`${response.status}: misc error. Please email [email protected] with above error`, jsonOut)
}
}

if (jsonOut.message.toLowerCase().includes("error")){
throw new BananaError(jsonOut.message, jsonOut)
}
return jsonOut
}

export class BananaError extends Error {
json: Object
constructor(message: string, json: any = null) {
super(message)
this.name = "BananaError"
this.json = json
}
}

const checkAPI = async (apiKey: string, callID: string): Promise<any> => {
const urlCheck = endpoint.concat("check/v4/")

Expand All @@ -88,21 +117,14 @@ const checkAPI = async (apiKey: string, callID: string): Promise<any> => {
"apiKey" : apiKey,
"callID" : callID
}

const response = await axios.post(urlCheck, payload).catch(err => {
if (err.response) {
throw `server error: status code ${err.response.status}`
} else if (err.request) {
throw 'server error: endpoint busy or not available.'
} else {
console.log(err)
throw "Misc axios error. Please email [email protected] with above error"
const response = await fetch(urlCheck, {
method: 'POST',
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json'
}
})
const jsonOut = response.data

if (jsonOut.message.toLowerCase().includes("error")){
throw jsonOut.message
}

const jsonOut = await getBananaJsonOutput(response)
return jsonOut
}
18 changes: 11 additions & 7 deletions module/index.ts → index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import genericsUtils = require("./genericsUtils")
import {checkMain, runMain, startMain, BananaError} from "./genericsUtils"

export async function run(apiKey: string, modelKey: string, modelInputs: object = {}): Promise<object>{
const out = await genericsUtils.runMain(
type Object = { [key: string]: any }

export async function run(apiKey: string, modelKey: string, modelInputs: object = {}): Promise<Object>{
const out = await runMain(
apiKey = apiKey,
modelKey = modelKey,
modelInputs=modelInputs)
return out
}

export async function start(apiKey: string, modelKey: string, modelInputs: object = {}): Promise<string>{
const callID = await genericsUtils.startMain(
const callID = await startMain(
apiKey = apiKey,
modelKey = modelKey,
modelInputs=modelInputs)
return callID
}

export async function check(apiKey: string, callID: string): Promise<object>{
const jsonOut = await genericsUtils.checkMain(apiKey, callID)
export async function check(apiKey: string, callID: string): Promise<Object>{
const jsonOut = await checkMain(apiKey, callID)
return jsonOut
}
}

export { BananaError }
191 changes: 0 additions & 191 deletions module/package-lock.json

This file was deleted.

Loading