-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #363 from AppQuality/develop
release-11-05-2024
- Loading branch information
Showing
5 changed files
with
153 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import config from "@src/config"; | ||
|
||
class Unguess { | ||
private baseUrl: string; | ||
private username: string; | ||
private password: string; | ||
|
||
constructor() { | ||
const { basePath, username, password } = config.unguessApi || {}; | ||
this.baseUrl = basePath || ""; | ||
this.username = username || ""; | ||
this.password = password || ""; | ||
} | ||
|
||
/** | ||
* Private method to fetch a token for API requests | ||
*/ | ||
private async getToken(): Promise<string> { | ||
const response = await fetch(`${this.baseUrl}/authenticate`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
username: this.username, | ||
password: this.password, | ||
}), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error("Failed to authenticate: " + response.statusText); | ||
} | ||
|
||
const data = await response.json(); | ||
if (!data.token) { | ||
throw new Error("Authentication failed: Token not found"); | ||
} | ||
|
||
return data.token; | ||
} | ||
|
||
/** | ||
* Private method to perform authenticated POST requests | ||
*/ | ||
private async authPost( | ||
path: string, | ||
body: Record<string, any> | ||
): Promise<any> { | ||
const token = await this.getToken(); | ||
const response = await fetch(`${this.baseUrl}${path}`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
body: JSON.stringify(body), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to post to ${path}: ${response.statusText}`); | ||
} | ||
|
||
return response.json(); | ||
} | ||
|
||
/** | ||
* Public method to post a new customer | ||
*/ | ||
public async postCustomer({ | ||
userId, | ||
name, | ||
}: { | ||
userId: number; | ||
name: string; | ||
}): Promise<{ id: number; name: string }> { | ||
const body = { | ||
company: name, | ||
pm_id: userId, | ||
}; | ||
const result = await this.authPost("/workspaces", body); | ||
return { | ||
id: result.id, | ||
name: result.name, | ||
}; | ||
} | ||
} | ||
|
||
export default Unguess; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters