Skip to content

Commit

Permalink
Bump to v0.7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
walsha2 committed Nov 20, 2023
1 parent 81fd039 commit 1eb5887
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 58 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.7.2

* Client generation using latest `openapi_spec` package (v0.7.8)

## 0.7.1

* Relax `meta` version requirement
Expand Down
165 changes: 110 additions & 55 deletions lib/src/generated/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// ignore_for_file: invalid_annotation_target, unused_import

import 'dart:convert';
import 'dart:io' as io;
import 'dart:typed_data';

import 'package:http/http.dart' as http;
Expand All @@ -21,7 +20,7 @@ enum HttpMethod { get, put, post, delete, options, head, patch, trace }
// ==========================================

/// HTTP exception handler for PineconeClient
class PineconeClientException implements io.HttpException {
class PineconeClientException implements Exception {
PineconeClientException({
required this.message,
required this.uri,
Expand All @@ -30,9 +29,7 @@ class PineconeClientException implements io.HttpException {
this.body,
});

@override
final String message;
@override
final Uri uri;
final HttpMethod method;
final int? code;
Expand Down Expand Up @@ -69,11 +66,13 @@ class PineconeClient {
///
/// - [PineconeClient.baseUrl] Override base URL (default: server url defined in spec)
/// - [PineconeClient.headers] Global headers to be sent with every request
/// - [PineconeClient.queryParams] Global query parameters to be sent with every request
/// - [PineconeClient.client] Override HTTP client to use for requests
PineconeClient({
this.apiKey = '',
this.baseUrl,
this.headers = const {},
this.queryParams = const {},
http.Client? client,
}) : assert(
baseUrl == null || baseUrl.startsWith('http'),
Expand All @@ -91,6 +90,9 @@ class PineconeClient {
/// Global headers to be sent with every request
final Map<String, String> headers;

/// Global query parameters to be sent with every request
final Map<String, dynamic> queryParams;

/// HTTP client for requests
final http.Client client;

Expand Down Expand Up @@ -136,12 +138,20 @@ class PineconeClient {
}

// ------------------------------------------
// METHOD: makeRequestStream
// METHOD: _jsonDecode
// ------------------------------------------

/// Reusable request stream method
dynamic _jsonDecode(http.Response r) {
return json.decode(utf8.decode(r.bodyBytes));
}

// ------------------------------------------
// METHOD: _request
// ------------------------------------------

/// Reusable request method
@protected
Future<http.StreamedResponse> makeRequestStream({
Future<http.StreamedResponse> _request({
required String baseUrl,
required String path,
required HttpMethod method,
Expand All @@ -161,6 +171,9 @@ class PineconeClient {
'baseUrl is required, but none defined in spec or provided by user',
);

// Add global query parameters
queryParams = {...queryParams, ...this.queryParams};

// Ensure query parameters are strings or iterable of strings
queryParams = queryParams.map((key, value) {
if (value is Iterable) {
Expand Down Expand Up @@ -193,46 +206,75 @@ class PineconeClient {
headers.addAll(this.headers);

// Build the request object
late http.StreamedResponse response;
try {
http.BaseRequest request;
if (isMultipart) {
// Handle multipart request
request = http.MultipartRequest(method.name, uri);
request = request as http.MultipartRequest;
if (body is List<http.MultipartFile>) {
request.files.addAll(body);
} else {
request.files.add(body as http.MultipartFile);
}
http.BaseRequest request;
if (isMultipart) {
// Handle multipart request
request = http.MultipartRequest(method.name, uri);
request = request as http.MultipartRequest;
if (body is List<http.MultipartFile>) {
request.files.addAll(body);
} else {
// Handle normal request
request = http.Request(method.name, uri);
request = request as http.Request;
try {
if (body != null) {
request.body = json.encode(body);
}
} catch (e) {
// Handle request encoding error
throw PineconeClientException(
uri: uri,
method: method,
message: 'Could not encode: ${body.runtimeType}',
body: e,
);
request.files.add(body as http.MultipartFile);
}
} else {
// Handle normal request
request = http.Request(method.name, uri);
request = request as http.Request;
try {
if (body != null) {
request.body = json.encode(body);
}
} catch (e) {
// Handle request encoding error
throw PineconeClientException(
uri: uri,
method: method,
message: 'Could not encode: ${body.runtimeType}',
body: e,
);
}
}

// Add request headers
request.headers.addAll(headers);
// Add request headers
request.headers.addAll(headers);

// Handle user request middleware
request = await onRequest(request);
// Handle user request middleware
request = await onRequest(request);

// Submit request
return await client.send(request);
}

// Submit request
response = await client.send(request);
// ------------------------------------------
// METHOD: makeRequestStream
// ------------------------------------------

/// Reusable request stream method
@protected
Future<http.StreamedResponse> makeRequestStream({
required String baseUrl,
required String path,
required HttpMethod method,
Map<String, dynamic> queryParams = const {},
Map<String, String> headerParams = const {},
bool isMultipart = false,
String requestType = '',
String responseType = '',
Object? body,
}) async {
final uri = Uri.parse((this.baseUrl ?? baseUrl) + path);
late http.StreamedResponse response;
try {
response = await _request(
baseUrl: baseUrl,
path: path,
method: method,
queryParams: queryParams,
headerParams: headerParams,
requestType: requestType,
responseType: responseType,
body: body,
);
// Handle user response middleware
response = await onStreamedResponse(response);
} catch (e) {
Expand Down Expand Up @@ -277,8 +319,10 @@ class PineconeClient {
String responseType = '',
Object? body,
}) async {
final uri = Uri.parse((this.baseUrl ?? baseUrl) + path);
late http.Response response;
try {
final streamedResponse = await makeRequestStream(
final streamedResponse = await _request(
baseUrl: baseUrl,
path: path,
method: method,
Expand All @@ -288,21 +332,32 @@ class PineconeClient {
responseType: responseType,
body: body,
);
final response = await http.Response.fromStream(streamedResponse);

response = await http.Response.fromStream(streamedResponse);
// Handle user response middleware
return await onResponse(response);
} on PineconeClientException {
rethrow;
response = await onResponse(response);
} catch (e) {
// Handle request and response errors
throw PineconeClientException(
uri: Uri.parse((this.baseUrl ?? baseUrl) + path),
uri: uri,
method: method,
message: 'Response error',
body: e,
);
}

// Check for successful response
if ((response.statusCode ~/ 100) == 2) {
return response;
}

// Handle unsuccessful response
throw PineconeClientException(
uri: uri,
method: method,
message: 'Unsuccessful response',
code: response.statusCode,
body: response.body,
);
}

// ------------------------------------------
Expand Down Expand Up @@ -330,7 +385,7 @@ class PineconeClient {
if (apiKey.isNotEmpty) 'Api-Key': apiKey,
},
);
return List<String>.from(json.decode(r.body));
return List<String>.from(_jsonDecode(r));
}

// ------------------------------------------
Expand Down Expand Up @@ -392,7 +447,7 @@ class PineconeClient {
if (apiKey.isNotEmpty) 'Api-Key': apiKey,
},
);
return Collection.fromJson(json.decode(r.body));
return Collection.fromJson(_jsonDecode(r));
}

// ------------------------------------------
Expand Down Expand Up @@ -450,7 +505,7 @@ class PineconeClient {
if (apiKey.isNotEmpty) 'Api-Key': apiKey,
},
);
return List<String>.from(json.decode(r.body));
return List<String>.from(_jsonDecode(r));
}

// ------------------------------------------
Expand Down Expand Up @@ -512,7 +567,7 @@ class PineconeClient {
if (apiKey.isNotEmpty) 'Api-Key': apiKey,
},
);
return Index.fromJson(json.decode(r.body));
return Index.fromJson(_jsonDecode(r));
}

// ------------------------------------------
Expand Down Expand Up @@ -615,7 +670,7 @@ class PineconeClient {
if (apiKey.isNotEmpty) 'Api-Key': apiKey,
},
);
return IndexStats.fromJson(json.decode(r.body));
return IndexStats.fromJson(_jsonDecode(r));
}

// ------------------------------------------
Expand Down Expand Up @@ -654,7 +709,7 @@ class PineconeClient {
if (apiKey.isNotEmpty) 'Api-Key': apiKey,
},
);
return QueryResponse.fromJson(json.decode(r.body));
return QueryResponse.fromJson(_jsonDecode(r));
}

// ------------------------------------------
Expand Down Expand Up @@ -737,7 +792,7 @@ class PineconeClient {
if (namespace != null) 'namespace': namespace,
},
);
return FetchResponse.fromJson(json.decode(r.body));
return FetchResponse.fromJson(_jsonDecode(r));
}

// ------------------------------------------
Expand Down Expand Up @@ -814,6 +869,6 @@ class PineconeClient {
if (apiKey.isNotEmpty) 'Api-Key': apiKey,
},
);
return UpsertResponse.fromJson(json.decode(r.body));
return UpsertResponse.fromJson(_jsonDecode(r));
}
}
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: pinecone
description: Unofficial Dart client for Pinecone vector database. For more details on Pinecone, please visit https://docs.pinecone.io/docs/overview.
version: 0.7.1
version: 0.7.2
maintainer: Taza Technology LLC
repository: https://github.com/tazatechnology/pinecone
issue_tracker: https://github.com/tazatechnology/pinecone/issues
Expand All @@ -17,9 +17,9 @@ dependencies:
meta: ^1.9.1

dev_dependencies:
openapi_spec: 0.7.6
openapi_spec: 0.7.9
build_runner: ^2.4.6
lints: ^3.0.0
freezed: ^2.4.5
json_serializable: ^6.7.1
test: ^1.24.7
test: ^1.24.9

0 comments on commit 1eb5887

Please sign in to comment.