Skip to content

Instantly share code, notes, and snippets.

View roshanadh's full-sized avatar
🌅
Working from home

Roshan Adhikari roshanadh

🌅
Working from home
View GitHub Profile
@roshanadh
roshanadh / package.json
Created May 8, 2025 12:52
package.json for the realtime audio API
{
"name": "azure-gpt4o-audio",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "tsc && node index.js"
},
"keywords": [],
"author": "",
@roshanadh
roshanadh / index.ts
Last active May 8, 2025 23:39
Realtime API with TypeScript and Azure OpenAI
import { OpenAIRealtimeWebSocket } from 'openai/beta/realtime/websocket';
import { AzureOpenAI } from "openai";
import Speaker from "speaker";
import "dotenv/config";
// configure the speaker instance -- this will pipe the audio to the speakers in realtime
const speaker = new Speaker({
channels: 1, // mono channel
bitDepth: 16, // 16-bit
sampleRate: 24000, // 24Khz
@roshanadh
roshanadh / ApplicationTests.java
Created October 21, 2022 18:16
Testing POST /batteries
@Test
void shouldPersistBattery() throws Exception {
Battery mockBattery =
new Battery("Gold Coast Mc", "9729", 50000);
mockMvc
.perform(
post("/batteries")
.content(objectMapper.writeValueAsString(mockBattery))
.contentType(MediaType.APPLICATION_JSON)
@roshanadh
roshanadh / ApplicationTests.java
Last active October 21, 2022 18:10
Testing GET /batteries
@Test
void shouldReturnAllBatteries() throws Exception {
mockMvc
.perform(get("/batteries/"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$", Matchers.hasSize(H2Bootstrap.mockBatteries.size())));
}
@roshanadh
roshanadh / ApplicationTests.java
Last active October 21, 2022 18:03
Testing GET /batteries/:id
package np.com.roshanadhikary.testdemo;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@SpringBootTest
@AutoConfigureMockMvc
class ApplicationTests {
// ..
@roshanadh
roshanadh / ApplicationTests.java
Created October 21, 2022 17:48
Spring Boot test class
package np.com.roshanadhikary.testdemo;
@SpringBootTest
@AutoConfigureMockMvc
class ApplicationTests {
@Autowired
private MockMvc mockMvc;
@Autowired
@roshanadh
roshanadh / application.properties
Created October 21, 2022 14:09
Spring Boot properties
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=user
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
@roshanadh
roshanadh / H2Bootstrap.java
Created October 21, 2022 13:57
Bootstrap H2 database with preloaded records
package np.com.roshanadhikary.testdemo.bootstrap;
/**
* Bootstrap the in-memory H2 database with some Battery resources when
* the application starts
*/
@Configuration
public class H2Bootstrap {
private static final Logger logger = LoggerFactory.getLogger(H2Bootstrap.class);
@roshanadh
roshanadh / BatteryRepository.java
Created October 21, 2022 13:48
Repository interface for Battery entity
package np.com.roshanadhikary.testdemo.repository;
import np.com.roshanadhikary.testdemo.entity.Battery;
import org.springframework.data.jpa.repository.JpaRepository;
public interface BatteryRepository extends JpaRepository<Battery, Integer> {
}
@roshanadh
roshanadh / Battery.java
Created October 21, 2022 13:45
Battery resource POJO
package np.com.roshanadhikary.testdemo.entity;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import javax.persistence.*;
/**