github.com/anthropics/anthropic-sdk-go@v1.70.1/file_test.go (about)

     1  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
     2  
     3  package anthropic_test
     4  
     5  import (
     6  	"bytes"
     7  	"context"
     8  	"errors"
     9  	"io"
    10  	"net/http"
    11  	"net/http/httptest"
    12  	"os"
    13  	"testing"
    14  
    15  	"github.com/anthropics/anthropic-sdk-go"
    16  	"github.com/anthropics/anthropic-sdk-go/internal/testutil"
    17  	"github.com/anthropics/anthropic-sdk-go/option"
    18  )
    19  
    20  func TestFileListWithOptionalParams(t *testing.T) {
    21  	baseURL := "http://localhost:4010"
    22  	if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
    23  		baseURL = envURL
    24  	}
    25  	if !testutil.CheckTestServer(t, baseURL) {
    26  		return
    27  	}
    28  	client := anthropic.NewClient(
    29  		option.WithBaseURL(baseURL),
    30  		option.WithAPIKey("my-anthropic-api-key"),
    31  	)
    32  	_, err := client.Files.List(context.TODO(), anthropic.FileListParams{
    33  		IDs:         []string{"string"},
    34  		Limit:       anthropic.Int(1),
    35  		Page:        anthropic.String("page"),
    36  		WorkspaceID: anthropic.String("wrkspc_011CZkZaBF1tNoB5wlCeusgy"),
    37  	})
    38  	if err != nil {
    39  		var apierr *anthropic.Error
    40  		if errors.As(err, &apierr) {
    41  			t.Log(string(apierr.DumpRequest(true)))
    42  		}
    43  		t.Fatalf("err should be nil: %s", err.Error())
    44  	}
    45  }
    46  
    47  func TestFileDeleteWithOptionalParams(t *testing.T) {
    48  	baseURL := "http://localhost:4010"
    49  	if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
    50  		baseURL = envURL
    51  	}
    52  	if !testutil.CheckTestServer(t, baseURL) {
    53  		return
    54  	}
    55  	client := anthropic.NewClient(
    56  		option.WithBaseURL(baseURL),
    57  		option.WithAPIKey("my-anthropic-api-key"),
    58  	)
    59  	_, err := client.Files.Delete(
    60  		context.TODO(),
    61  		"file_id",
    62  		anthropic.FileDeleteParams{
    63  			WorkspaceID: anthropic.String("wrkspc_011CZkZaBF1tNoB5wlCeusgy"),
    64  		},
    65  	)
    66  	if err != nil {
    67  		var apierr *anthropic.Error
    68  		if errors.As(err, &apierr) {
    69  			t.Log(string(apierr.DumpRequest(true)))
    70  		}
    71  		t.Fatalf("err should be nil: %s", err.Error())
    72  	}
    73  }
    74  
    75  func TestFileDownloadWithOptionalParams(t *testing.T) {
    76  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    77  		w.WriteHeader(200)
    78  		w.Write([]byte("abc"))
    79  	}))
    80  	defer server.Close()
    81  	baseURL := server.URL
    82  	client := anthropic.NewClient(
    83  		option.WithBaseURL(baseURL),
    84  		option.WithAPIKey("my-anthropic-api-key"),
    85  	)
    86  	resp, err := client.Files.Download(
    87  		context.TODO(),
    88  		"file_id",
    89  		anthropic.FileDownloadParams{
    90  			WorkspaceID: anthropic.String("wrkspc_011CZkZaBF1tNoB5wlCeusgy"),
    91  		},
    92  	)
    93  	if err != nil {
    94  		var apierr *anthropic.Error
    95  		if errors.As(err, &apierr) {
    96  			t.Log(string(apierr.DumpRequest(true)))
    97  		}
    98  		t.Fatalf("err should be nil: %s", err.Error())
    99  	}
   100  	defer resp.Body.Close()
   101  
   102  	b, err := io.ReadAll(resp.Body)
   103  	if err != nil {
   104  		var apierr *anthropic.Error
   105  		if errors.As(err, &apierr) {
   106  			t.Log(string(apierr.DumpRequest(true)))
   107  		}
   108  		t.Fatalf("err should be nil: %s", err.Error())
   109  	}
   110  	if !bytes.Equal(b, []byte("abc")) {
   111  		t.Fatalf("return value not %s: %s", "abc", b)
   112  	}
   113  }
   114  
   115  func TestFileGetMetadataWithOptionalParams(t *testing.T) {
   116  	baseURL := "http://localhost:4010"
   117  	if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
   118  		baseURL = envURL
   119  	}
   120  	if !testutil.CheckTestServer(t, baseURL) {
   121  		return
   122  	}
   123  	client := anthropic.NewClient(
   124  		option.WithBaseURL(baseURL),
   125  		option.WithAPIKey("my-anthropic-api-key"),
   126  	)
   127  	_, err := client.Files.GetMetadata(
   128  		context.TODO(),
   129  		"file_id",
   130  		anthropic.FileGetMetadataParams{
   131  			WorkspaceID: anthropic.String("wrkspc_011CZkZaBF1tNoB5wlCeusgy"),
   132  		},
   133  	)
   134  	if err != nil {
   135  		var apierr *anthropic.Error
   136  		if errors.As(err, &apierr) {
   137  			t.Log(string(apierr.DumpRequest(true)))
   138  		}
   139  		t.Fatalf("err should be nil: %s", err.Error())
   140  	}
   141  }
   142  
   143  func TestFileUploadWithOptionalParams(t *testing.T) {
   144  	baseURL := "http://localhost:4010"
   145  	if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
   146  		baseURL = envURL
   147  	}
   148  	if !testutil.CheckTestServer(t, baseURL) {
   149  		return
   150  	}
   151  	client := anthropic.NewClient(
   152  		option.WithBaseURL(baseURL),
   153  		option.WithAPIKey("my-anthropic-api-key"),
   154  	)
   155  	_, err := client.Files.Upload(context.TODO(), anthropic.FileUploadParams{
   156  		File:             io.Reader(bytes.NewBuffer([]byte("Example data"))),
   157  		ExpiresInSeconds: anthropic.Int(3600),
   158  		WorkspaceID:      anthropic.String("wrkspc_011CZkZaBF1tNoB5wlCeusgy"),
   159  	})
   160  	if err != nil {
   161  		var apierr *anthropic.Error
   162  		if errors.As(err, &apierr) {
   163  			t.Log(string(apierr.DumpRequest(true)))
   164  		}
   165  		t.Fatalf("err should be nil: %s", err.Error())
   166  	}
   167  }