RobertIndie commented on code in PR #2437: URL: https://github.com/apache/streampipes/pull/2437#discussion_r1511288784
########## streampipes-client-go/streampipes/credential/credential.go: ########## @@ -0,0 +1,47 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package credential + +import ( + "github.com/apache/streampipes/streampipes-client-go/streampipes/internal/streampipes_http/headers" + "net/http" +) + +type StreamPipesApiKeyCredentials struct { + UserName string + ApiKey string +} + +func (s *StreamPipesApiKeyCredentials) ApiKeyCredential(username, apikey string) { + s.UserName = username + s.ApiKey = apikey +} Review Comment: We shouldn't add this kind of method. It looks like a construct. You could make it as `NewStreamPipesApiKeyCredential` ########## streampipes-client-go/streampipes/credential/credential.go: ########## @@ -0,0 +1,47 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package credential + +import ( + "github.com/apache/streampipes/streampipes-client-go/streampipes/internal/streampipes_http/headers" + "net/http" +) + +type StreamPipesApiKeyCredentials struct { + UserName string + ApiKey string +} + +func (s *StreamPipesApiKeyCredentials) ApiKeyCredential(username, apikey string) { + s.UserName = username + s.ApiKey = apikey +} + +func (s *StreamPipesApiKeyCredentials) GetUsername() string { + return s.UserName +} +func (s *StreamPipesApiKeyCredentials) GetApiKey() string { + return s.ApiKey +} Review Comment: Seems we don't need these methods. ########## streampipes-client-go/streampipes/streampipes_client.go: ########## @@ -0,0 +1,80 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package streampipes + +import ( + "errors" + "log" + "net/url" + "regexp" + "streampipes-client-go/streampipes/api" + "streampipes-client-go/streampipes/config" + "streampipes-client-go/streampipes/internal/credential" + "strings" +) + +/* + This is the central point of contact with StreamPipes and provides all the functionalities to interact with it. + The client provides so-called "API", each of which refers to the endpoint of the StreamPipes API. + e.g. `DataLakeMeasureApi` provides the actual methods to interact with StreamPipes API. +*/ + +type StreamPipesClient struct { + Config config.StreamPipesClientConnectionConfig +} + +func NewStreamPipesClient(config config.StreamPipesClientConnectionConfig) (*StreamPipesClient, error) { + + //NewStreamPipesClient returns an instance of * StreamPipesClient + //Temporarily does not support HTTPS connections, nor does it support connecting to port 443 + + if config.Credential == (credential.StreamPipesApiKeyCredentials{}) { + log.Fatal("No credential entered") + } + + re := regexp.MustCompile(`(?i)^(http|https):\/\/[^\s\/:]+:\d+$`) + ok := re.MatchString(config.Url) + if !ok { + log.Fatal("Please check if the URL is correct,Must be in the form of A://B:C," + + "where A is either HTTP or HTTPS, not case sensitive.B must be the host and C must be the port.") + } + + Url, err := url.Parse(config.Url) + if err != nil { + log.Fatal("Please enter the correct URL", err) + } + + if strings.EqualFold(Url.Scheme, "https") || Url.Port() == "443" { + return &StreamPipesClient{}, errors.New( + "Invalid configuration passed! The given client configuration has " + + "`https_disabled` set to `True` and `port` set to `443`.\n " + Review Comment: So we need to remove this from the error message. ########## streampipes-client-go/examples/main.go: ########## @@ -0,0 +1,63 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package main + +import ( + "fmt" + "github.com/apache/streampipes/streampipes-client-go/streampipes" + "github.com/apache/streampipes/streampipes-client-go/streampipes/config" + "github.com/apache/streampipes/streampipes-client-go/streampipes/credential" +) + +/* + Here are some examples of using go client, including outputting the data returned by streams. + Only supports outputting model data +*/ + +func main() { + Config := config.StreamPipesClientConnectionConfig{ + Url: "http://localhost:8030", + Credential: credential.StreamPipesApiKeyCredentials{ + UserName: "<Your-User-Name>", + ApiKey: "<Your-API-Key>", + }, + } + StreamPipesClient, err := streampipes.NewStreamPipesClient(Config) + if err != nil { + fmt.Println(err) + } Review Comment: ```suggestion if err != nil { fmt.Errorf(err) os.Exit(1) } ``` ########## streampipes-client-go/streampipes/credential/credential.go: ########## @@ -0,0 +1,47 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package credential + +import ( + "github.com/apache/streampipes/streampipes-client-go/streampipes/internal/streampipes_http/headers" + "net/http" +) + +type StreamPipesApiKeyCredentials struct { + UserName string + ApiKey string +} Review Comment: This should be part of config. Let's move it to the config package. ########## streampipes-client-go/streampipes/data_lake_measure_api.go: ########## @@ -0,0 +1,117 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package streampipes + +import ( + "github.com/apache/streampipes/streampipes-client-go/streampipes/config" + "github.com/apache/streampipes/streampipes-client-go/streampipes/internal/serializer" + "github.com/apache/streampipes/streampipes-client-go/streampipes/internal/streampipes_http" + "github.com/apache/streampipes/streampipes-client-go/streampipes/model/data_lake" + "log" +) + +/* +DataLakeMeasure connects to the DataLakeMeasure endpoint of Streampipes. +DataLakeMeasure supports GET and DELETE to delete or obtain resources +The specific interaction behavior is provided by the method bound to the DataLakeMeasure struct. +*/ + +type DataLakeMeasure struct { + config config.StreamPipesClientConnectionConfig + httpRequest streampipes_http.HttpRequest +} + +func NewDataLakeMeasures(clientConfig config.StreamPipesClientConnectionConfig) *DataLakeMeasure { + //NewDataLakeMeasure is used to return an instance of *DataLakeMeasure, + + return &DataLakeMeasure{ + config: clientConfig, + httpRequest: nil, + } +} + +func (d *DataLakeMeasure) AllDataLakeMeasure() []data_lake.DataLakeMeasure { + //Get a list of all measure + + d.httpRequest = &streampipes_http.GetRequest{ + HttpRequest: streampipes_http.NewHttpRequest(d.config), + Deserializer: &serializer.UnmarshalDataLakeMeasure{}, + } + d.resourcePath([]string{"measurements"}) + UnmarshalData := d.httpRequest.ExecuteRequest(nil) Review Comment: ```suggestion unmarshalData := d.httpRequest.ExecuteRequest(nil) ``` ########## streampipes-client-go/examples/main.go: ########## @@ -0,0 +1,63 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package main + +import ( + "fmt" + "github.com/apache/streampipes/streampipes-client-go/streampipes" + "github.com/apache/streampipes/streampipes-client-go/streampipes/config" + "github.com/apache/streampipes/streampipes-client-go/streampipes/credential" +) + +/* + Here are some examples of using go client, including outputting the data returned by streams. + Only supports outputting model data +*/ + +func main() { + Config := config.StreamPipesClientConnectionConfig{ + Url: "http://localhost:8030", + Credential: credential.StreamPipesApiKeyCredentials{ + UserName: "<Your-User-Name>", + ApiKey: "<Your-API-Key>", + }, + } + StreamPipesClient, err := streampipes.NewStreamPipesClient(Config) + if err != nil { + fmt.Println(err) + } + StreamPipesClient.DataLakeMeasures().GetSingleDataSeries("measureId").Conversion() + + /* + output format: + + There are 2 pieces of DataSerie in the Dataseries + The 1 DataSeries + time msg test + 2024-02-23T13:37:09.052Z go-client_test 2f4556 + 2024-02-23T13:37:26.044Z go-client_test 2f4556 + 2024-02-23T13:37:29.007Z go-client_test 2f4556 + The 2 DataSeries + time msg test Review Comment: Please format this line. ########## streampipes-client-go/streampipes/api/data_lake_measure_api.go: ########## @@ -0,0 +1,121 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package api + +import ( + "fmt" + "streampipes-client-go/streampipes/config" + "streampipes-client-go/streampipes/internal/serializer" + "streampipes-client-go/streampipes/internal/streampipes_http" + "streampipes-client-go/streampipes/model/data_lake" +) + +/* +DataLakeMeasureApi connects to the DataLakeMeasure endpoint of Streampipes. +DataLakeMeasureApi supports GET, POST, Delete, and PUT methods for obtaining, deleting, submitting, and updating resources. +The specific interaction behavior is provided by the method bound to the DataLakeMeasureApi struct. +Currently, only some GET and Delete methods have been implemented. +*/ +type DataLakeMeasureApi struct { + config config.StreamPipesClientConnectionConfig + httpRequest streampipes_http.HttpRequest Review Comment: But we reassign it with a new httpRequest for each query. We don't need to maintain an additional field in this struct. ########## streampipes-client-go/streampipes/credential/credential.go: ########## @@ -0,0 +1,47 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package credential + +import ( + "github.com/apache/streampipes/streampipes-client-go/streampipes/internal/streampipes_http/headers" + "net/http" +) + +type StreamPipesApiKeyCredentials struct { + UserName string + ApiKey string +} + +func (s *StreamPipesApiKeyCredentials) ApiKeyCredential(username, apikey string) { + s.UserName = username + s.ApiKey = apikey +} + +func (s *StreamPipesApiKeyCredentials) GetUsername() string { + return s.UserName +} +func (s *StreamPipesApiKeyCredentials) GetApiKey() string { + return s.ApiKey +} + +func (s *StreamPipesApiKeyCredentials) MakeHeaders(header *headers.Headers) (Rheader []http.Header) { + XApiUser := header.XApiUser(s.UserName) + XApiKey := header.XApiKey(s.ApiKey) + Rheader = append(append(Rheader, XApiUser), XApiKey) + return Rheader +} Review Comment: I couldn't find any usages on this method. ########## streampipes-client-go/streampipes/data_lake_measure_api.go: ########## @@ -0,0 +1,117 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package streampipes + +import ( + "github.com/apache/streampipes/streampipes-client-go/streampipes/config" + "github.com/apache/streampipes/streampipes-client-go/streampipes/internal/serializer" + "github.com/apache/streampipes/streampipes-client-go/streampipes/internal/streampipes_http" + "github.com/apache/streampipes/streampipes-client-go/streampipes/model/data_lake" + "log" +) + +/* +DataLakeMeasure connects to the DataLakeMeasure endpoint of Streampipes. +DataLakeMeasure supports GET and DELETE to delete or obtain resources +The specific interaction behavior is provided by the method bound to the DataLakeMeasure struct. +*/ + +type DataLakeMeasure struct { + config config.StreamPipesClientConnectionConfig + httpRequest streampipes_http.HttpRequest +} + +func NewDataLakeMeasures(clientConfig config.StreamPipesClientConnectionConfig) *DataLakeMeasure { + //NewDataLakeMeasure is used to return an instance of *DataLakeMeasure, + + return &DataLakeMeasure{ + config: clientConfig, + httpRequest: nil, + } +} + +func (d *DataLakeMeasure) AllDataLakeMeasure() []data_lake.DataLakeMeasure { + //Get a list of all measure + + d.httpRequest = &streampipes_http.GetRequest{ + HttpRequest: streampipes_http.NewHttpRequest(d.config), + Deserializer: &serializer.UnmarshalDataLakeMeasure{}, + } + d.resourcePath([]string{"measurements"}) + UnmarshalData := d.httpRequest.ExecuteRequest(nil) + return UnmarshalData.([]data_lake.DataLakeMeasure) +} + +func (d *DataLakeMeasure) GetSingleDataLakeMeasure(elementId string) data_lake.DataLakeMeasure { + //Get a measure + + d.httpRequest = &streampipes_http.GetRequest{ + HttpRequest: streampipes_http.NewHttpRequest(d.config), + Deserializer: &serializer.UnmarshalDataLakeMeasure{}, + } + d.resourcePath([]string{"measure", elementId}) + UnmarshalData := d.httpRequest.ExecuteRequest(nil) + return UnmarshalData.(data_lake.DataLakeMeasure) +} + +func (d *DataLakeMeasure) GetSingleDataSeries(measureName string) data_lake.DataSeries { + + //Get data from a single measurement series by a given id + //Currently not supporting parameter queries + + d.httpRequest = &streampipes_http.GetRequest{ + HttpRequest: streampipes_http.NewHttpRequest(d.config), + Deserializer: &serializer.UnmarshalDataSeries{}, + } + d.resourcePath([]string{"measurements", measureName}) + UnmarshalData := d.httpRequest.ExecuteRequest(nil) + return UnmarshalData.(data_lake.DataSeries) +} + +func (d *DataLakeMeasure) ClearDataLakeMeasureData(measureName string) { + //Remove data from a single measurement series with given id + + d.httpRequest = &streampipes_http.DeleteRequest{ + HttpRequest: streampipes_http.NewHttpRequest(d.config), + } + d.resourcePath([]string{"measurements", measureName}) + d.httpRequest.ExecuteRequest(nil) + log.Printf("Successfully deleted data from a single measurement sequence of %s", measureName) +} + +func (d *DataLakeMeasure) DeleteDataLakeMeasure(measureName string) { + //Drop a single measurement series with given id from Data Lake and remove related event property + + d.httpRequest = &streampipes_http.DeleteRequest{ + HttpRequest: streampipes_http.NewHttpRequest(d.config), + } + d.resourcePath([]string{"measurements", measureName, "drop"}) + d.httpRequest.ExecuteRequest(nil) + log.Printf("Successfully dropped a single measurement series for %s from DataLake and remove related event property", measureName) +} + +func (d *DataLakeMeasure) resourcePath(parameter []string) { Review Comment: ```suggestion func (d *DataLakeMeasure) setResourcePath(parameter []string) { ``` ########## streampipes-client-go/streampipes/streampipes_credentials.go: ########## @@ -0,0 +1,26 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package streampipes + +import "github.com/apache/streampipes/streampipes-client-go/streampipes/credential" + +func NewStreamPipesApiKeyCredentials(username, apiKey string) credential.StreamPipesApiKeyCredentials { + Credentials := credential.StreamPipesApiKeyCredentials{} + Credentials.ApiKeyCredential(username, apiKey) + return Credentials +} Review Comment: We should make this construct to `credential.go` ########## streampipes-client-go/streampipes/data_lake_measure_api.go: ########## @@ -0,0 +1,117 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package streampipes + +import ( + "github.com/apache/streampipes/streampipes-client-go/streampipes/config" + "github.com/apache/streampipes/streampipes-client-go/streampipes/internal/serializer" + "github.com/apache/streampipes/streampipes-client-go/streampipes/internal/streampipes_http" + "github.com/apache/streampipes/streampipes-client-go/streampipes/model/data_lake" + "log" +) + +/* +DataLakeMeasure connects to the DataLakeMeasure endpoint of Streampipes. +DataLakeMeasure supports GET and DELETE to delete or obtain resources +The specific interaction behavior is provided by the method bound to the DataLakeMeasure struct. +*/ + +type DataLakeMeasure struct { + config config.StreamPipesClientConnectionConfig + httpRequest streampipes_http.HttpRequest +} + +func NewDataLakeMeasures(clientConfig config.StreamPipesClientConnectionConfig) *DataLakeMeasure { + //NewDataLakeMeasure is used to return an instance of *DataLakeMeasure, + + return &DataLakeMeasure{ + config: clientConfig, + httpRequest: nil, + } +} + +func (d *DataLakeMeasure) AllDataLakeMeasure() []data_lake.DataLakeMeasure { + //Get a list of all measure + + d.httpRequest = &streampipes_http.GetRequest{ + HttpRequest: streampipes_http.NewHttpRequest(d.config), + Deserializer: &serializer.UnmarshalDataLakeMeasure{}, + } + d.resourcePath([]string{"measurements"}) Review Comment: Can we abstrct this logic into one method? (and also remove the `resourcePath` method) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
