fix: add context
This commit is contained in:
parent
bff19f2ddb
commit
e05f7bd8ef
1 changed files with 10 additions and 4 deletions
|
@ -2,6 +2,7 @@ package lib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -25,7 +26,7 @@ type convertRequest struct {
|
||||||
To string `json:"to"`
|
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{
|
body, err := json.Marshal(convertRequest{
|
||||||
Text: text,
|
Text: text,
|
||||||
From: from,
|
From: from,
|
||||||
|
@ -37,7 +38,12 @@ func (client *PandocClient) convert(text, from, to string) (string, error) {
|
||||||
|
|
||||||
buf := bytes.NewBuffer(body)
|
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 {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -56,6 +62,6 @@ func (client *PandocClient) convert(text, from, to string) (string, error) {
|
||||||
return string(body), nil
|
return string(body), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *PandocClient) ConvertLatexToHtml5(text string) (string, error) {
|
func (client *PandocClient) ConvertLatexToHtml5(ctx context.Context, text string) (string, error) {
|
||||||
return client.convert(text, "latex", "html5")
|
return client.convert(ctx, text, "latex", "html5")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue