Skip to content

Commit 143b8fb

Browse files
committed
fix: fix HTTP calls with raw text input for the R client
1 parent 69a1051 commit 143b8fb

3 files changed

Lines changed: 94 additions & 39 deletions

File tree

README.md

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,60 @@
11
# CMS Client
2-
This repo contains SDKs for interacting with the [Cogstack ModelServe](https://github.com/cogstack/CogStack-ModelServe) (CMS) APIs.
32

4-
CMS is a model serving and governance system for CogStack NLP solutions. The client SDKs in this repo are generated from the OpenAPI specification to provide type-safe, language-native ways to interact with the CMS APIs from your own applications. These cover authentication, NER annotation, streaming and real-time entity extraction and redaction, text embedding, model training and evaluation, annotation statistics and rendering, free text or structured output generation, and other auxiliary functionalities related to model serving and fine-tuning. The following languages are supported:
3+
[![Java](https://img.shields.io/badge/Java-SDK-blue?logo=java)](src/gen/java/README.md)
4+
[![Python](https://img.shields.io/badge/Python-SDK-blue?logo=python)](src/gen/python/README.md)
5+
[![R](https://img.shields.io/badge/R-SDK-blue?logo=r)](src/gen/r/README.md)
6+
7+
This repo contains SDKs for interacting with the [Cogstack ModelServe](https://github.com/cogstack/CogStack-ModelServe) (CMS) APIs.
8+
9+
CMS is a model serving and governance system for CogStack NLP solutions. The client SDKs in this repo are generated from the OpenAPI specification to provide type-safe, language-native ways to interact with the CMS APIs from your own applications. These cover authentication, NER annotation, streaming and real-time entity extraction and redaction, text embedding, model training and evaluation, annotation statistics and rendering, free text or structured output generation, and other auxiliary functionalities related to model serving and fine-tuning.
10+
11+
12+
## Usage
13+
14+
To use one of these SDKs, navigate to the specific language directory above and follow the installation instructions provided in the README file.
515
* [Java](https://github.com/cogstack/cms-client/blob/main/src/gen/java/README.md)
616
* [Python](https://github.com/cogstack/cms-client/blob/main/src/gen/python/README.md)
717
* [R](https://github.com/cogstack/cms-client/blob/main/src/gen/r/README.md)
818

9-
## Usage
19+
## Examples
20+
21+
### Entity extraction
22+
23+
Java:
24+
```java
25+
import org.cms.client.ApiClient;
26+
import org.cms.client.api.AnnotationsApi;
27+
28+
String cmsBaseUrl = "http://localhost:8000";
29+
ApiClient client = new ApiClient();
30+
client.setBasePath(cmsBaseUrl);
31+
AnnotationsApi api = new AnnotationsApi(client);
32+
String text = "John had been diagnosed with acute Kidney Failure the week beforel";
33+
var response = api.getEntitiesFromText(text);
34+
System.out.println(response);
35+
```1
36+
37+
Python:
38+
```python
39+
import cms_client
40+
41+
cms_base_url = "http://localhost:8000"
42+
configuration = cms_client.Configuration(host=cms_base_url)
43+
with cms_client.ApiClient(configuration) as api_client:
44+
api_instance = cms_client.AnnotationsApi(api_client)
45+
text = "John had been diagnosed with acute Kidney Failure the week before."
46+
response = api_instance.get_entities_from_text(text)
47+
print(response)
48+
```
49+
50+
R:
51+
```r
52+
library(cmsClient)
1053

11-
To use one of these SDKs, navigate to the specific language directory above and follow the installation instructions provided in the README file.
54+
cms_base_url <- "http://localhost:8000"
55+
api_instance <- AnnotationsApi$new()
56+
api_instance$api_client$base_path <- cms_base_url
57+
text <- "John had been diagnosed with acute Kidney Failure the week before."
58+
response <- api_instance$GetEntitiesFromText(text)
59+
print(response)
60+
```

src/gen/r/R/annotations_api.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,13 @@ AnnotationsApi <- R6::R6Class(
525525
}
526526

527527
if (!is.null(`body`)) {
528-
local_var_body <- `body`$toJSONString()
528+
if (is.character(`body`)) {
529+
local_var_body <- `body`
530+
} else if (!is.null(`body`$toJSONString)) {
531+
local_var_body <- `body`$toJSONString()
532+
} else {
533+
local_var_body <- jsonlite::toJSON(`body`, auto_unbox = TRUE, null = "null")
534+
}
529535
} else {
530536
local_var_body <- NULL
531537
}

src/gen/r/R/api_client.R

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,14 @@ ApiClient <- R6::R6Class(
216216
form_params, file_params,
217217
accepts, content_types,
218218
body, stream_callback = NULL, ...) {
219-
headers <- httr::add_headers(c(header_params, self$default_headers))
219+
headers <- character()
220+
if (!is.null(header_params) && length(header_params) > 0) {
221+
headers <- c(headers, unlist(header_params, use.names = TRUE))
222+
}
223+
if (!is.null(self$default_headers) && length(self$default_headers) > 0) {
224+
headers <- c(headers, unlist(self$default_headers, use.names = TRUE))
225+
}
226+
headers <- as.character(headers)
220227

221228
http_timeout <- NULL
222229
if (!is.null(self$timeout)) {
@@ -226,63 +233,56 @@ ApiClient <- R6::R6Class(
226233
# set HTTP accept header
227234
accept = self$select_header(accepts)
228235
if (!is.null(accept)) {
229-
headers['Accept'] = accept
236+
headers <- c(headers, "Accept" = accept)
230237
}
231238

232239
# set HTTP content-type header
233240
content_type = self$select_header(content_types)
234241
if (!is.null(content_type)) {
235-
headers['Content-Type'] = content_type
242+
headers <- c(headers, "Content-Type" = content_type)
243+
}
244+
245+
request_config <- list()
246+
if (length(headers) > 0) {
247+
request_config[[length(request_config) + 1]] <- httr::add_headers(.headers = headers)
248+
}
249+
if (!is.null(http_timeout)) {
250+
request_config[[length(request_config) + 1]] <- http_timeout
251+
}
252+
if (!is.null(self$`user_agent`)) {
253+
request_config[[length(request_config) + 1]] <- httr::user_agent(self$`user_agent`)
236254
}
237255

238256
if (typeof(stream_callback) == "closure") { # stream data
239257
if (method == "GET") {
240-
httr::GET(url, query = query_params, headers, http_timeout,
241-
httr::user_agent(self$`user_agent`), write_stream(stream_callback), ...)
258+
httr_response <- do.call(httr::GET, c(list(url = url, query = query_params), request_config, list(...)))
242259
} else if (method == "POST") {
243-
httr::POST(url, query = query_params, headers, body = body,
244-
httr::content_type("application/json"), http_timeout,
245-
httr::user_agent(self$`user_agent`), write_stream(stream_callback), ...)
260+
httr_response <- do.call(httr::POST, c(list(url = url, query = query_params, body = body, encode = "raw"), request_config, list(...)))
246261
} else if (method == "PUT") {
247-
httr::PUT(url, query = query_params, headers, body = body,
248-
httr::content_type("application/json"), http_timeout,
249-
http_timeout, httr::user_agent(self$`user_agent`), write_stream(stream_callback), ...)
262+
httr_response <- do.call(httr::PUT, c(list(url = url, query = query_params, body = body, encode = "raw"), request_config, list(...)))
250263
} else if (method == "PATCH") {
251-
httr::PATCH(url, query = query_params, headers, body = body,
252-
httr::content_type("application/json"), http_timeout,
253-
http_timeout, httr::user_agent(self$`user_agent`), write_stream(stream_callback), ...)
264+
httr_response <- do.call(httr::PATCH, c(list(url = url, query = query_params, body = body, encode = "raw"), request_config, list(...)))
254265
} else if (method == "HEAD") {
255-
httr::HEAD(url, query = query_params, headers, http_timeout,
256-
http_timeout, httr::user_agent(self$`user_agent`), write_stream(stream_callback), ...)
266+
httr_response <- do.call(httr::HEAD, c(list(url = url, query = query_params), request_config, list(...)))
257267
} else if (method == "DELETE") {
258-
httr::DELETE(url, query = query_params, headers, http_timeout,
259-
http_timeout, httr::user_agent(self$`user_agent`), write_stream(stream_callback), ...)
268+
httr_response <- do.call(httr::DELETE, c(list(url = url, query = query_params), request_config, list(...)))
260269
} else {
261270
err_msg <- "Http method must be `GET`, `HEAD`, `OPTIONS`, `POST`, `PATCH`, `PUT` or `DELETE`."
262271
stop(err_msg)
263272
}
264273
} else { # no streaming
265274
if (method == "GET") {
266-
httr_response <- httr::GET(url, query = query_params, headers, http_timeout,
267-
httr::user_agent(self$`user_agent`), ...)
275+
httr_response <- do.call(httr::GET, c(list(url = url, query = query_params), request_config, list(...)))
268276
} else if (method == "POST") {
269-
httr_response <- httr::POST(url, query = query_params, headers, body = body,
270-
httr::content_type("application/json"), http_timeout,
271-
httr::user_agent(self$`user_agent`), ...)
277+
httr_response <- do.call(httr::POST, c(list(url = url, query = query_params, body = body, encode = "raw"), request_config, list(...)))
272278
} else if (method == "PUT") {
273-
httr_response <- httr::PUT(url, query = query_params, headers, body = body,
274-
httr::content_type("application/json"), http_timeout,
275-
http_timeout, httr::user_agent(self$`user_agent`), ...)
279+
httr_response <- do.call(httr::PUT, c(list(url = url, query = query_params, body = body, encode = "raw"), request_config, list(...)))
276280
} else if (method == "PATCH") {
277-
httr_response <- httr::PATCH(url, query = query_params, headers, body = body,
278-
httr::content_type("application/json"), http_timeout,
279-
http_timeout, httr::user_agent(self$`user_agent`), ...)
281+
httr_response <- do.call(httr::PATCH, c(list(url = url, query = query_params, body = body, encode = "raw"), request_config, list(...)))
280282
} else if (method == "HEAD") {
281-
httr_response <- httr::HEAD(url, query = query_params, headers, http_timeout,
282-
http_timeout, httr::user_agent(self$`user_agent`), ...)
283+
httr_response <- do.call(httr::HEAD, c(list(url = url, query = query_params), request_config, list(...)))
283284
} else if (method == "DELETE") {
284-
httr_response <- httr::DELETE(url, query = query_params, headers, http_timeout,
285-
http_timeout, httr::user_agent(self$`user_agent`), ...)
285+
httr_response <- do.call(httr::DELETE, c(list(url = url, query = query_params), request_config, list(...)))
286286
} else {
287287
err_msg <- "Http method must be `GET`, `HEAD`, `OPTIONS`, `POST`, `PATCH`, `PUT` or `DELETE`."
288288
stop(err_msg)
@@ -412,7 +412,7 @@ ApiClient <- R6::R6Class(
412412
}
413413

414414
# not json mime type, simply return the first one
415-
return(headers[1])
415+
return(headers[[1]])
416416
}
417417
},
418418

0 commit comments

Comments
 (0)