Skip to content

Commit

Permalink
Change binary to use io.Reader (#2329)
Browse files Browse the repository at this point in the history
* Change binary to use io.Reader

os.File is very inconvenient when you work with in memory data. This is
a small backward incompatible change but I think it's alright.

* Cassettes
:#
  • Loading branch information
therve authored Dec 22, 2023
1 parent fe0acd8 commit 2a5860b
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 76 deletions.
4 changes: 2 additions & 2 deletions .generator/src/generator/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def simple_type(schema, render_nullable=False, render_new=False):
"date": "time.Time" if not nullable else f"{nullable_prefix}Time",
"date-time": "time.Time" if not nullable else f"{nullable_prefix}Time",
"email": "string" if not nullable else f"{nullable_prefix}String",
"binary": "*os.File",
"binary": "_io.Reader",
"uuid": "uuid.UUID" if not nullable else f"{nullable_prefix}String",
None: "string" if not nullable else f"{nullable_prefix}String",
}[type_format]
Expand Down Expand Up @@ -454,7 +454,7 @@ def format_uuid(x):
return f'uuid.MustParse("{x}")'

def open_file(x):
return f"func() *os.File {{ fp, _ := os.Open({format_string(x)}); return fp }}()"
return f"func() io.Reader {{ fp, _ := os.Open({format_string(x)}); return fp }}()"

formatter = {
"int32": str,
Expand Down
2 changes: 0 additions & 2 deletions .generator/src/generator/templates/api.j2
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,6 @@ localVarQueryParams.Add("{{ parameter.name }}", {{ common_package_name }}.Parame
if localVarFile != nil {
fbs, _ := _io.ReadAll(localVarFile)
formFile.FileBytes = fbs
formFile.FileName = localVarFile.Name()
localVarFile.Close()
}
{%- endif %}

Expand Down
5 changes: 1 addition & 4 deletions api/datadogV1/api_organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
_io "io"
_nethttp "net/http"
_neturl "net/url"
"os"
"strings"

"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
Expand Down Expand Up @@ -393,7 +392,7 @@ func (a *OrganizationsApi) UpdateOrg(ctx _context.Context, publicId string, body
// * **Multipart Form-Data**: Post the IdP metadata file using a form post.
//
// * **XML Body:** Post the IdP metadata file as the body of the request.
func (a *OrganizationsApi) UploadIdPForOrg(ctx _context.Context, publicId string, idpFile *os.File) (IdpResponse, *_nethttp.Response, error) {
func (a *OrganizationsApi) UploadIdPForOrg(ctx _context.Context, publicId string, idpFile _io.Reader) (IdpResponse, *_nethttp.Response, error) {
var (
localVarHTTPMethod = _nethttp.MethodPost
localVarPostBody interface{}
Expand All @@ -420,8 +419,6 @@ func (a *OrganizationsApi) UploadIdPForOrg(ctx _context.Context, publicId string
if localVarFile != nil {
fbs, _ := _io.ReadAll(localVarFile)
formFile.FileBytes = fbs
formFile.FileName = localVarFile.Name()
localVarFile.Close()
}
datadog.SetAuthKeys(
ctx,
Expand Down
16 changes: 8 additions & 8 deletions api/datadogV1/model_idp_form_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ package datadogV1

import (
"fmt"
"os"
_io "io"

"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
)

// IdpFormData Object describing the IdP configuration.
type IdpFormData struct {
// The path to the XML metadata file you wish to upload.
IdpFile *os.File `json:"idp_file"`
IdpFile _io.Reader `json:"idp_file"`
// UnparsedObject contains the raw value of the object if there was an error when deserializing into the struct
UnparsedObject map[string]interface{} `json:"-"`
AdditionalProperties map[string]interface{}
Expand All @@ -24,7 +24,7 @@ type IdpFormData struct {
// This constructor will assign default values to properties that have it defined,
// and makes sure properties required by API are set, but the set of arguments
// will change when the set of required properties is changed.
func NewIdpFormData(idpFile *os.File) *IdpFormData {
func NewIdpFormData(idpFile _io.Reader) *IdpFormData {
this := IdpFormData{}
this.IdpFile = idpFile
return &this
Expand All @@ -39,25 +39,25 @@ func NewIdpFormDataWithDefaults() *IdpFormData {
}

// GetIdpFile returns the IdpFile field value.
func (o *IdpFormData) GetIdpFile() *os.File {
func (o *IdpFormData) GetIdpFile() _io.Reader {
if o == nil {
var ret *os.File
var ret _io.Reader
return ret
}
return o.IdpFile
}

// GetIdpFileOk returns a tuple with the IdpFile field value
// and a boolean to check if the value has been set.
func (o *IdpFormData) GetIdpFileOk() (**os.File, bool) {
func (o *IdpFormData) GetIdpFileOk() (*_io.Reader, bool) {
if o == nil {
return nil, false
}
return &o.IdpFile, true
}

// SetIdpFile sets field value.
func (o *IdpFormData) SetIdpFile(v *os.File) {
func (o *IdpFormData) SetIdpFile(v _io.Reader) {
o.IdpFile = v
}

Expand All @@ -78,7 +78,7 @@ func (o IdpFormData) MarshalJSON() ([]byte, error) {
// UnmarshalJSON deserializes the given payload.
func (o *IdpFormData) UnmarshalJSON(bytes []byte) (err error) {
all := struct {
IdpFile **os.File `json:"idp_file"`
IdpFile *_io.Reader `json:"idp_file"`
}{}
if err = datadog.Unmarshal(bytes, &all); err != nil {
return datadog.Unmarshal(bytes, &o.UnparsedObject)
Expand Down
6 changes: 3 additions & 3 deletions api/datadogV2/api_cloud_workload_security.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ package datadogV2

import (
_context "context"
_io "io"
_nethttp "net/http"
_neturl "net/url"
"os"
"strings"

"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
Expand Down Expand Up @@ -155,11 +155,11 @@ func (a *CloudWorkloadSecurityApi) DeleteCloudWorkloadSecurityAgentRule(ctx _con
// The download endpoint generates a Cloud Workload Security policy file from your currently active
// Cloud Workload Security rules, and downloads them as a .policy file. This file can then be deployed to
// your Agents to update the policy running in your environment.
func (a *CloudWorkloadSecurityApi) DownloadCloudWorkloadPolicyFile(ctx _context.Context) (*os.File, *_nethttp.Response, error) {
func (a *CloudWorkloadSecurityApi) DownloadCloudWorkloadPolicyFile(ctx _context.Context) (_io.Reader, *_nethttp.Response, error) {
var (
localVarHTTPMethod = _nethttp.MethodGet
localVarPostBody interface{}
localVarReturnValue *os.File
localVarReturnValue _io.Reader
)

localBasePath, err := a.Client.Cfg.ServerURLWithContext(ctx, "v2.CloudWorkloadSecurityApi.DownloadCloudWorkloadPolicyFile")
Expand Down
9 changes: 3 additions & 6 deletions api/datadogV2/api_organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
_io "io"
_nethttp "net/http"
_neturl "net/url"
"os"

"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
)
Expand All @@ -19,7 +18,7 @@ type OrganizationsApi datadog.Service

// UploadIdPMetadataOptionalParameters holds optional parameters for UploadIdPMetadata.
type UploadIdPMetadataOptionalParameters struct {
IdpFile **os.File
IdpFile *_io.Reader
}

// NewUploadIdPMetadataOptionalParameters creates an empty struct for parameters.
Expand All @@ -29,7 +28,7 @@ func NewUploadIdPMetadataOptionalParameters() *UploadIdPMetadataOptionalParamete
}

// WithIdpFile sets the corresponding parameter name and returns the struct.
func (r *UploadIdPMetadataOptionalParameters) WithIdpFile(idpFile *os.File) *UploadIdPMetadataOptionalParameters {
func (r *UploadIdPMetadataOptionalParameters) WithIdpFile(idpFile _io.Reader) *UploadIdPMetadataOptionalParameters {
r.IdpFile = &idpFile
return r
}
Expand Down Expand Up @@ -67,15 +66,13 @@ func (a *OrganizationsApi) UploadIdPMetadata(ctx _context.Context, o ...UploadId

formFile := datadog.FormFile{}
formFile.FormFileName = "idp_file"
var localVarFile *os.File
var localVarFile _io.Reader
if optionalParams.IdpFile != nil {
localVarFile = *optionalParams.IdpFile
}
if localVarFile != nil {
fbs, _ := _io.ReadAll(localVarFile)
formFile.FileBytes = fbs
formFile.FileName = localVarFile.Name()
localVarFile.Close()
}
datadog.SetAuthKeys(
ctx,
Expand Down
16 changes: 8 additions & 8 deletions api/datadogV2/model_id_p_metadata_form_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
package datadogV2

import (
"os"
_io "io"

"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
)

// IdPMetadataFormData The form data submitted to upload IdP metadata
type IdPMetadataFormData struct {
// The IdP metadata XML file
IdpFile **os.File `json:"idp_file,omitempty"`
IdpFile *_io.Reader `json:"idp_file,omitempty"`
// UnparsedObject contains the raw value of the object if there was an error when deserializing into the struct
UnparsedObject map[string]interface{} `json:"-"`
AdditionalProperties map[string]interface{}
Expand All @@ -37,17 +37,17 @@ func NewIdPMetadataFormDataWithDefaults() *IdPMetadataFormData {
}

// GetIdpFile returns the IdpFile field value if set, zero value otherwise.
func (o *IdPMetadataFormData) GetIdpFile() *os.File {
func (o *IdPMetadataFormData) GetIdpFile() _io.Reader {
if o == nil || o.IdpFile == nil {
var ret *os.File
var ret _io.Reader
return ret
}
return *o.IdpFile
}

// GetIdpFileOk returns a tuple with the IdpFile field value if set, nil otherwise
// and a boolean to check if the value has been set.
func (o *IdPMetadataFormData) GetIdpFileOk() (**os.File, bool) {
func (o *IdPMetadataFormData) GetIdpFileOk() (*_io.Reader, bool) {
if o == nil || o.IdpFile == nil {
return nil, false
}
Expand All @@ -59,8 +59,8 @@ func (o *IdPMetadataFormData) HasIdpFile() bool {
return o != nil && o.IdpFile != nil
}

// SetIdpFile gets a reference to the given *os.File and assigns it to the IdpFile field.
func (o *IdPMetadataFormData) SetIdpFile(v *os.File) {
// SetIdpFile gets a reference to the given _io.Reader and assigns it to the IdpFile field.
func (o *IdPMetadataFormData) SetIdpFile(v _io.Reader) {
o.IdpFile = &v
}

Expand All @@ -83,7 +83,7 @@ func (o IdPMetadataFormData) MarshalJSON() ([]byte, error) {
// UnmarshalJSON deserializes the given payload.
func (o *IdPMetadataFormData) UnmarshalJSON(bytes []byte) (err error) {
all := struct {
IdpFile **os.File `json:"idp_file,omitempty"`
IdpFile *_io.Reader `json:"idp_file,omitempty"`
}{}
if err = datadog.Unmarshal(bytes, &all); err != nil {
return datadog.Unmarshal(bytes, &o.UnparsedObject)
Expand Down
3 changes: 2 additions & 1 deletion examples/v1/organizations/UploadIdPForOrg.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"os"

"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
Expand All @@ -17,7 +18,7 @@ func main() {
configuration := datadog.NewConfiguration()
apiClient := datadog.NewAPIClient(configuration)
api := datadogV1.NewOrganizationsApi(apiClient)
resp, r, err := api.UploadIdPForOrg(ctx, "abc123", func() *os.File { fp, _ := os.Open("./idp_metadata.xml"); return fp }())
resp, r, err := api.UploadIdPForOrg(ctx, "abc123", func() io.Reader { fp, _ := os.Open("./idp_metadata.xml"); return fp }())

if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `OrganizationsApi.UploadIdPForOrg`: %v\n", err)
Expand Down
3 changes: 2 additions & 1 deletion examples/v2/organizations/UploadIdPMetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main
import (
"context"
"fmt"
"io"
"os"

"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
Expand All @@ -16,7 +17,7 @@ func main() {
configuration := datadog.NewConfiguration()
apiClient := datadog.NewAPIClient(configuration)
api := datadogV2.NewOrganizationsApi(apiClient)
r, err := api.UploadIdPMetadata(ctx, *datadogV2.NewUploadIdPMetadataOptionalParameters().WithIdpFile(func() *os.File {
r, err := api.UploadIdPMetadata(ctx, *datadogV2.NewUploadIdPMetadataOptionalParameters().WithIdpFile(func() io.Reader {
fp, _ := os.Open("fixtures/organizations/saml_configurations/valid_idp_metadata.xml")
return fp
}()))
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2021-04-12T11:53:46.715643+02:00
2023-12-22T14:30:08.685141784+01:00
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,42 @@
version: 1
interactions:
- request:
body: "--a1a19613f5d9afb0dc96c923c789d65708322400bd16f5341f51906e00e0\r\nContent-Disposition:
form-data; name=\"idp_file\"; filename=\"error_415.json\"\r\nContent-Type: application/octet-stream\r\n\r\n{\"errors\":
[\"Attempt to upload idp metadata with an unsupported Content-Type\"]}\n\r\n--a1a19613f5d9afb0dc96c923c789d65708322400bd16f5341f51906e00e0--\r\n"
body: "--8d7a497f381cc1f9f9c0ddb59aba34ec3a25f1875e7930165eadae6e6632\r\nContent-Disposition:
form-data; name=\"idp_file\"; filename=\".\"\r\nContent-Type: application/octet-stream\r\n\r\n{\"errors\":
[\"Attempt to upload idp metadata with an unsupported Content-Type\"]}\n\r\n--8d7a497f381cc1f9f9c0ddb59aba34ec3a25f1875e7930165eadae6e6632--\r\n"
form: {}
headers:
Accept:
- application/json
Content-Length:
- "262"
- "317"
Content-Type:
- multipart/form-data; boundary=a1a19613f5d9afb0dc96c923c789d65708322400bd16f5341f51906e00e0
- multipart/form-data; boundary=8d7a497f381cc1f9f9c0ddb59aba34ec3a25f1875e7930165eadae6e6632
User-Agent:
- datadog-api-client-go/1.0.0-beta.19+dev (go go1.15.7; os darwin; arch amd64)
- datadog-api-client-go/2.20.1+dev (go go1.21.5; os linux; arch amd64)
X-Datadog-Parent-Id:
- "2135779211706672752"
- "7670172946023811925"
X-Datadog-Sampling-Priority:
- "1"
- "2"
X-Datadog-Trace-Id:
- "1207290177630451530"
url: https://api.datadoghq.com/api/v1/org/Test-TestOrgsUploadIdpErrors_400_Bad_Request-1618221226/idp_metadata
- "1208129418755230576"
url: https://api.datadoghq.com/api/v1/org/Test-TestOrgsUploadIdpErrors_400_Bad_Request-1703251808/idp_metadata
method: POST
response:
body: '{"errors": ["Not allowed to access this organization"]}'
body: '{"errors":["Not allowed to access this organization"]}'
headers:
Cache-Control:
- no-cache
Connection:
- keep-alive
Content-Length:
- "55"
Content-Type:
- application/json
Date:
- Mon, 12 Apr 2021 09:53:46 GMT
Pragma:
- no-cache
- Fri, 22 Dec 2023 13:30:09 GMT
Strict-Transport-Security:
- max-age=15724800;
- max-age=31536000; includeSubDomains; preload
Vary:
- Accept-Encoding
X-Content-Type-Options:
- nosniff
X-Dd-Version:
- "35.4287331"
X-Frame-Options:
- SAMEORIGIN
status: 400 Bad Request
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2021-04-12T11:53:46.63022+02:00
2023-12-22T14:31:05.452351676+01:00
Loading

0 comments on commit 2a5860b

Please sign in to comment.