fix: add context

This commit is contained in:
Vyacheslav1557 2024-08-18 20:36:26 +05:00
parent bff19f2ddb
commit e05f7bd8ef

View file

@ -2,6 +2,7 @@ package lib
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
@ -25,7 +26,7 @@ type convertRequest struct {
To string `json:"to"`
}
func (client *PandocClient) convert(text, from, to string) (string, error) {
func (client *PandocClient) convert(ctx context.Context, text, from, to string) (string, error) {
body, err := json.Marshal(convertRequest{
Text: text,
From: from,
@ -37,7 +38,12 @@ func (client *PandocClient) convert(text, from, to string) (string, error) {
buf := bytes.NewBuffer(body)
resp, err := client.client.Post(client.address, "application/json", buf)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, client.address, buf)
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.client.Do(req)
if err != nil {
return "", err
}
@ -56,6 +62,6 @@ func (client *PandocClient) convert(text, from, to string) (string, error) {
return string(body), nil
}
func (client *PandocClient) ConvertLatexToHtml5(text string) (string, error) {
return client.convert(text, "latex", "html5")
func (client *PandocClient) ConvertLatexToHtml5(ctx context.Context, text string) (string, error) {
return client.convert(ctx, text, "latex", "html5")
}