[call-me] - add endpoint to list all connected users available to be called

This commit is contained in:
Miroslav Pejic
2024-12-12 00:16:25 +01:00
parent dfd275a284
commit af5b89444c
11 changed files with 171 additions and 16 deletions
+4
View File
@@ -113,7 +113,11 @@ Easily integrate `Call-Me` into your website or application with a [simple ifram
Get all connected users
```shell
# Get all connected users
curl -X GET "http://localhost:8000/api/v1/users" -H "authorization: call_me_api_key_secret" -H "Content-Type: application/json"
# Generate call links for connected users to call
curl -X POST "http://localhost:8000/api/v1/connected" -H "authorization: call_me_api_key_secret" -H "Content-Type: application/json" -d '{"user": "call-me"}'
```
Docs: http://localhost:8000/api/v1/docs/ or you can check it out live [here](https://cme.mirotalk.com/api/v1/docs/).
+19
View File
@@ -0,0 +1,19 @@
'use strict';
const url = 'http://localhost:8000/api/v1/connected';
const authorization = 'call_me_api_key_secret';
fetch(url, {
method: 'POST',
headers: {
Authorization: authorization,
'Content-Type': 'application/json',
},
body: JSON.stringify({
user: 'call-me',
}),
})
.then((response) => response.json())
.then((data) => console.log(data))
.catch((error) => console.error('Error:', error));
+24
View File
@@ -0,0 +1,24 @@
<?php
$url = "http://localhost:8000/api/v1/connected";
$authorization = "call_me_api_key_secret";
// Data to send in the request body
$data = json_encode(array('user' => 'call-me'));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"authorization: $authorization",
"Content-Type: application/json"
));
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
+18
View File
@@ -0,0 +1,18 @@
import requests # pip3 install requests
url = "http://localhost:8000/api/v1/connected"
authorization = "call_me_api_key_secret"
headers = {
'Authorization': authorization,
'Content-Type': 'application/json'
}
data = {
'user': 'call-me'
}
response = requests.post(url, headers=headers, json=data)
print(response.json())
+9
View File
@@ -0,0 +1,9 @@
#!/bin/bash
url="http://localhost:8000/api/v1/connected";
authorization="call_me_api_key_secret"
response=$(curl -s -X POST "$url" -H "authorization: call_me_api_key_secret" -H "Content-Type: application/json" -d '{"user": "call-me"}')
echo "$response"
+43 -1
View File
@@ -3,7 +3,7 @@ swagger: '2.0'
info:
title: Call-me API
description: API description for external applications that integrates with Call-me.
version: 1.0.0
version: 1.0.1
basePath: /api/v1
@@ -32,6 +32,32 @@ paths:
'403':
description: 'Unauthorized!'
/connected:
post:
tags:
- 'connected'
summary: 'Connected users to call'
description: 'Retrieve all connected users to call'
parameters:
- in: body
name: user
description: Username
schema:
$ref: '#/definitions/ConnectedRequest'
consumes:
- 'application/json'
produces:
- 'application/json'
security:
- secretApiKey: []
responses:
'200':
description: 'Connected users to call'
schema:
$ref: '#/definitions/connectedResponse'
'403':
description: 'Unauthorized!'
securityDefinitions:
secretApiKey:
type: 'apiKey'
@@ -45,3 +71,19 @@ definitions:
properties:
users:
type: 'array'
example: ['call-me', 'miro']
ConnectedRequest:
type: object
properties:
user:
type: string
description: 'User identifier'
example: 'call-me'
connectedResponse:
type: 'object'
properties:
connected:
type: array
example: ['http://localhost:8000/join?user=call-me&call=miro']
+4 -4
View File
@@ -1,6 +1,6 @@
'use strict';
const $url = 'https://localhost:8000/api/v1/users';
const url = 'http://localhost:8000/api/v1/users';
const authorization = 'call_me_api_key_secret';
@@ -11,6 +11,6 @@ fetch(url, {
'Content-Type': 'application/json',
},
})
.then((response) => response.json()) // Parse the JSON response
.then((data) => console.log(data)) // Log the data
.catch((error) => console.error('Error:', error)); // Handle errors
.then((response) => response.json())
.then((data) => console.log(data))
.catch((error) => console.error('Error:', error));
+1 -1
View File
@@ -1,6 +1,6 @@
#!/bin/bash
url = "http://localhost:8000/api/v1/users";
url="http://localhost:8000/api/v1/users";
authorization="call_me_api_key_secret"
+48 -4
View File
@@ -147,19 +147,57 @@ app.get('/join/', (req, res) => {
return notFound(res);
});
// Endpoint to list all connected users available to be called
app.post(`${config.apiBasePath}/connected`, (req, res) => {
// Check if the user is authorized for this API call
if (!isAuthorized(req)) {
console.log('Unauthorized API call: Get Connected', {
headers: req.headers,
body: req.body,
});
return res.status(403).json({ error: 'Unauthorized!' });
}
const { user } = req.body;
if (!user) {
return res.status(400).json({ error: 'User not provided in request body' });
}
// Get the protocol and host information to build the base URL
const protocol = req.protocol; // e.g., 'http' or 'https'
const host = req.get('Host'); // e.g., 'localhost:8000'
const hostName = req.hostname; // e.g., 'localhost'
// Construct the base URL depending on the protocol (https or http)
const baseUrl = `${protocol}://${protocol === 'https' ? hostName : host}`;
// Retrieve the list of connected users
const users = getConnectedUsers();
// Generate a list of user-to-call links for the provided user
const connected = Array.from(users.values()).reduce((acc, connectedUser) => {
// If the current user is not the one requesting, add to the list
if (user !== connectedUser) {
acc.push(`${baseUrl}/join?user=${user}&call=${connectedUser}`);
}
return acc;
}, []);
// Return the list of connected users that the provided user can call
return res.json({ connected });
});
// Axios API requests
app.get(`${config.apiBasePath}/users`, (req, res) => {
// check if user is authorized for the API call
const { authorization } = req.headers;
console.log(authorization);
if (authorization != config.apiKeySecret) {
if (!isAuthorized(req)) {
console.log('Unauthorized API call: Get Users', {
headers: req.headers,
body: req.body,
});
return res.status(403).json({ error: 'Unauthorized!' });
}
// Get connected users
// Retrieve the list of connected users
const users = getConnectedUsers();
return res.json({ users });
});
@@ -174,6 +212,12 @@ function notFound(res) {
res.json({ data: '404 not found' });
}
// Utility function to check API key authorization
const isAuthorized = (req) => {
const { authorization } = req.headers;
return authorization === config.apiKeySecret;
};
// Function to handle individual WebSocket connections
function handleConnection(socket) {
console.log('User connected:', socket.id);
-5
View File
@@ -1,11 +1,6 @@
version: '3'
services:
callme:
image: mirotalk/cme:latest
build:
context: .
dockerfile: Dockerfile
container_name: callme
hostname: callme
volumes:
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "call-me",
"version": "1.0.27",
"version": "1.0.28",
"description": "Your Go-To for Instant Video Calls",
"author": "Miroslav Pejic - miroslav.pejic.85@gmail.com",
"license": "AGPLv3",