Commit 691d61f9 authored by Johan Brandhorst's avatar Johan Brandhorst Committed by Sander van Harmelen

Add a WithContext function (#144)

* Add a WithContext function, Fixes #143

This enables the use of requests with a specific context attached.

* Fix indentation

* Add a test
parent 9f4cdd1f
...@@ -18,6 +18,7 @@ package gitlab ...@@ -18,6 +18,7 @@ package gitlab
import ( import (
"bytes" "bytes"
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
...@@ -546,6 +547,14 @@ func WithSudo(uid interface{}) OptionFunc { ...@@ -546,6 +547,14 @@ func WithSudo(uid interface{}) OptionFunc {
} }
} }
// WithContext runs the request with the provided context
func WithContext(ctx context.Context) OptionFunc {
return func(req *http.Request) error {
req = req.WithContext(ctx)
return nil
}
}
// Bool is a helper routine that allocates a new bool value // Bool is a helper routine that allocates a new bool value
// to store v and returns a pointer to it. // to store v and returns a pointer to it.
func Bool(v bool) *bool { func Bool(v bool) *bool {
......
package gitlab package gitlab
import ( import (
"context"
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
...@@ -137,3 +138,15 @@ func TestCheckResponse(t *testing.T) { ...@@ -137,3 +138,15 @@ func TestCheckResponse(t *testing.T) {
t.Errorf("Expected error: %s, got %s", want, errResp.Error()) t.Errorf("Expected error: %s, got %s", want, errResp.Error())
} }
} }
func TestRequestWithContext(t *testing.T) {
ctx := context.Background()
req, err := NewClient(nil, "").NewRequest("GET", "test", nil, []OptionFunc{WithContext(ctx)})
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
if req.Context() != ctx {
t.Fatal("Context was not set correctly")
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment