Skip to content

Commit

Permalink
refactor: improved response handling for api client.
Browse files Browse the repository at this point in the history
  • Loading branch information
kingmariano committed Jul 20, 2024
1 parent dc22d92 commit 1165295
Show file tree
Hide file tree
Showing 19 changed files with 22 additions and 35 deletions.
6 changes: 1 addition & 5 deletions packages/convert2mp3/convert2mp3.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ import (
"github.com/kingmariano/omnicron/utils"
)

type Response struct {
Response string `json:"response"`
}

func ConvertToMp3(w http.ResponseWriter, r *http.Request, cfg *config.APIConfig) {
ctx := r.Context()
// creates a unique folder within the current directory
Expand Down Expand Up @@ -62,5 +58,5 @@ func ConvertToMp3(w http.ResponseWriter, r *http.Request, cfg *config.APIConfig)
return
}
//returns back the response in JSON format
utils.RespondWithJSON(w, http.StatusOK, Response{Response: urlLink})
utils.RespondWithJSON(w, http.StatusOK, utils.ResponseMsg{Response: urlLink})
}
2 changes: 1 addition & 1 deletion packages/docgpt/docgpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ func DocGPT(w http.ResponseWriter, r *http.Request, cfg *config.APIConfig) {
return
}
//responds with JSON
utils.RespondWithJSON(w, http.StatusOK, response)
utils.RespondWithJSON(w, http.StatusOK, utils.ResponseMsg{Response: response})
}
2 changes: 1 addition & 1 deletion packages/gpt/gpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ func ChatCompletion(w http.ResponseWriter, r *http.Request, cfg *config.APIConfi
utils.RespondWithError(w, http.StatusInternalServerError, fmt.Sprintf("Error handling chat completion, %v", err))
return
}
utils.RespondWithJSON(w, http.StatusOK, response)
utils.RespondWithJSON(w, http.StatusOK, utils.ResponseMsg{Response: response})

}
2 changes: 1 addition & 1 deletion packages/grok/chatcompletion.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ func ChatCompletion(w http.ResponseWriter, r *http.Request, cfg *config.APIConfi

// new release for grok response
// Respond with JSON containing the response message
utils.RespondWithJSON(w, http.StatusOK, response)
utils.RespondWithJSON(w, http.StatusOK, utils.ResponseMsg{Response: response})
}
4 changes: 1 addition & 3 deletions packages/grok/transcription.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,5 @@ func Transcription(w http.ResponseWriter, r *http.Request, cfg *config.APIConfig
return
}

// Retrieve and respond with the transcription result
transcriptionResult := response.Choices[0].Message.Content
utils.RespondWithJSON(w, http.StatusOK, transcriptionResult)
utils.RespondWithJSON(w, http.StatusOK, utils.ResponseMsg{Response: response})
}
2 changes: 1 addition & 1 deletion packages/image2text/image2text.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ func Image2text(w http.ResponseWriter, r *http.Request, cfg *config.APIConfig) {
utils.RespondWithError(w, http.StatusInternalServerError, fmt.Sprintf("Error calling the Image To Text Endpoint, %v", err))
return
}
utils.RespondWithJSON(w, http.StatusOK, response)
utils.RespondWithJSON(w, http.StatusOK, utils.ResponseMsg{Response: response})
}
7 changes: 1 addition & 6 deletions packages/musicdownloader/music.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ type SongRequest struct {
Song string `json:"song"`
}

// SongResponse represents the structure of the response
type SongResponse struct {
Response []string `json:"response"`
}

var maxLength int64 //specifies the maxmium length of data returned from the youtube sdk
func DownloadMusic(w http.ResponseWriter, r *http.Request, cfg *config.APIConfig) {
ctx := r.Context()
Expand Down Expand Up @@ -70,5 +65,5 @@ func DownloadMusic(w http.ResponseWriter, r *http.Request, cfg *config.APIConfig
utils.RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
utils.RespondWithJSON(w, http.StatusOK, SongResponse{Response: audioDirectURL})
utils.RespondWithJSON(w, http.StatusOK, utils.ResponseMsg{Response: audioDirectURL})
}
2 changes: 1 addition & 1 deletion packages/musicsearch/musicsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ func MusicSearch(w http.ResponseWriter, r *http.Request, cfg *config.APIConfig)
utils.RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
utils.RespondWithJSON(w, http.StatusOK, response)
utils.RespondWithJSON(w, http.StatusOK, utils.ResponseMsg{Response: response})
}
2 changes: 1 addition & 1 deletion packages/replicate/generateimages/image_generation.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ func ImageGeneration(w http.ResponseWriter, r *http.Request, cfg *config.APIConf
utils.RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
utils.RespondWithJSON(w, http.StatusOK, ImagePrediction)
utils.RespondWithJSON(w, http.StatusOK, utils.ResponseMsg{Response: ImagePrediction})
}
2 changes: 1 addition & 1 deletion packages/replicate/generatemusic/generatemusic.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ func MusicGen(w http.ResponseWriter, r *http.Request, cfg *config.APIConfig) {
return
}

utils.RespondWithJSON(w, http.StatusOK, MusicGenPrediction)
utils.RespondWithJSON(w, http.StatusOK, utils.ResponseMsg{Response: MusicGenPrediction})

}
4 changes: 2 additions & 2 deletions packages/replicate/generatevideos/generate_videos.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ func VideoGeneration(w http.ResponseWriter, r *http.Request, cfg *config.APIConf
utils.RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
videoPrediction, err := predictionFunc(ctx, cfg.ReplicateAPIKey, repVideoModel.Version, predictionInput, nil, false)
videoGenPrediction, err := predictionFunc(ctx, cfg.ReplicateAPIKey, repVideoModel.Version, predictionInput, nil, false)
if err != nil {
utils.RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
utils.RespondWithJSON(w, http.StatusOK, videoPrediction)
utils.RespondWithJSON(w, http.StatusOK, utils.ResponseMsg{Response: videoGenPrediction})
}
2 changes: 1 addition & 1 deletion packages/replicate/imageupscale/upscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ func ImageUpscale(w http.ResponseWriter, r *http.Request, cfg *config.APIConfig)
return
}

utils.RespondWithJSON(w, http.StatusOK, ImageUpscalePrediction)
utils.RespondWithJSON(w, http.StatusOK, utils.ResponseMsg{Response: ImageUpscalePrediction})
}
2 changes: 1 addition & 1 deletion packages/replicate/stt/stt.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ func STT(w http.ResponseWriter, r *http.Request, cfg *config.APIConfig) {
return
}

utils.RespondWithJSON(w, http.StatusOK, STTPrediction)
utils.RespondWithJSON(w, http.StatusOK, utils.ResponseMsg{Response: STTPrediction})

}
2 changes: 1 addition & 1 deletion packages/replicate/tts/tts.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ func TTS(w http.ResponseWriter, r *http.Request, cfg *config.APIConfig) {
return
}

utils.RespondWithJSON(w, http.StatusOK, TTSPrediction)
utils.RespondWithJSON(w, http.StatusOK, utils.ResponseMsg{Response: TTSPrediction})

}
2 changes: 1 addition & 1 deletion packages/shazam/shazam.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ func Shazam(w http.ResponseWriter, r *http.Request, cfg *config.APIConfig) {
utils.RespondWithError(w, http.StatusInternalServerError, fmt.Sprintf("Error calling Shazam Endpoint, %v", err))
return
}
utils.RespondWithJSON(w, http.StatusOK, response)
utils.RespondWithJSON(w, http.StatusOK, utils.ResponseMsg{Response: response})
}
5 changes: 1 addition & 4 deletions packages/videodownloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ type DownloadParams struct {
URL string `json:"url"`
Resolution string `json:"resolution"`
}
type Responseparams struct {
Response string `json:"response"`
}

// DownloadVideo handles the video download process.
// It accepts a POST request with JSON body containing URL and resolution.
Expand Down Expand Up @@ -87,5 +84,5 @@ func DownloadVideo(w http.ResponseWriter, r *http.Request, cfg *config.APIConfig
return
}

utils.RespondWithJSON(w, http.StatusOK, Responseparams{Response: urlLink})
utils.RespondWithJSON(w, http.StatusOK, utils.ResponseMsg{Response: urlLink})
}
3 changes: 0 additions & 3 deletions packages/videodownloader/handle_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package videodownloader

import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -112,7 +111,6 @@ func DownloadVideoData(url string, outputName string, outputPath string, resolut
MultiThread: true,
ThreadNumber: 50,
})
log.Printf("the output is %v", outputPath)
err = download.Download(data[0])
if err != nil {
log.Println("cleaning up, deleting folder...")
Expand All @@ -121,7 +119,6 @@ func DownloadVideoData(url string, outputName string, outputPath string, resolut
}
return "", err
}
fmt.Println("this is final output path", outputPath+outputName+".*")
files, err := filepath.Glob(outputPath + "/" + outputName + ".*")
if err != nil {
return "", err
Expand Down
2 changes: 1 addition & 1 deletion packages/youtubesummarize/youtubesummarize.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ func YoutubeSummarization(w http.ResponseWriter, r *http.Request, cfg *config.AP
utils.RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
utils.RespondWithJSON(w, http.StatusOK, summary)
utils.RespondWithJSON(w, http.StatusOK, utils.ResponseMsg{Response: summary})
}
4 changes: 4 additions & 0 deletions utils/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import (
"net/http"
)

type ResponseMsg struct {
Response interface{} `json:"response"`
}

func RespondWithError(w http.ResponseWriter, code int, errmessage string) {
if code < 499 {
log.Printf("Responding with 5XX error: %s", errmessage)
Expand Down

0 comments on commit 1165295

Please sign in to comment.