Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for uploading application logos #103

Merged
merged 1 commit into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added internal/test/testlogo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions msgraph/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,3 +583,24 @@ func (c *ApplicationsClient) DeleteExtension(ctx context.Context, applicationId,

return status, nil
}

// UploadLogo uploads the application logo which should be a gif, jpeg or png image
func (c *ApplicationsClient) UploadLogo(ctx context.Context, applicationId, contentType string, logoData []byte) (int, error) {
var status int

_, status, _, err := c.BaseClient.Put(ctx, PutHttpRequestInput{
Body: logoData,
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ContentType: contentType,
ValidStatusCodes: []int{http.StatusNoContent},
Uri: Uri{
Entity: fmt.Sprintf("/applications/%s/logo", applicationId),
HasTenantId: true,
},
})
if err != nil {
return status, fmt.Errorf("ApplicationsClient.BaseClient.Put(): %v", err)
}

return status, nil
}
17 changes: 17 additions & 0 deletions msgraph/applications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package msgraph_test

import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/manicminer/hamilton/auth"
Expand Down Expand Up @@ -91,6 +93,7 @@ func TestApplicationsClient(t *testing.T) {
testApplicationsClient_AddOwners(t, c, app)
pwd := testApplicationsClient_AddPassword(t, c, app)
testApplicationsClient_RemovePassword(t, c, app, pwd)
testApplicationsClient_UploadLogo(t, c, app)
testApplicationsClient_List(t, c)
testApplicationsClient_Delete(t, c, *app.ID)
testApplicationsClient_ListDeleted(t, c, *app.ID)
Expand Down Expand Up @@ -370,3 +373,17 @@ func testApplicationsClient_ListDeleted(t *testing.T, c ApplicationsClientTest,
}
return
}

func testApplicationsClient_UploadLogo(t *testing.T, c ApplicationsClientTest, a *msgraph.Application) {
b, err := os.ReadFile(filepath.Join("..", "internal", "test", "testlogo.png"))
if err != nil {
t.Fatalf("reading testlogo.png: %v", err)
}
status, err := c.client.UploadLogo(c.connection.Context, *a.ID, "image/png", b)
if err != nil {
t.Fatalf("ApplicationsClient.UploadLogo(): %v", err)
}
if status < 200 || status >= 300 {
t.Fatalf("ApplicationsClient.UploadLogo(): invalid status: %d", status)
}
}
32 changes: 31 additions & 1 deletion msgraph/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type ValidStatusFunc func(*http.Response, *odata.OData) bool
// HttpRequestInput is any type that can validate the response to an HTTP request.
type HttpRequestInput interface {
GetConsistencyFailureFunc() ConsistencyFailureFunc
GetContentType() string
GetValidStatusCodes() []int
GetValidStatusFunc() ValidStatusFunc
}
Expand Down Expand Up @@ -140,7 +141,7 @@ func (c Client) performRequest(req *http.Request, input HttpRequestInput) (*http
}

req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json; charset=utf-8")
req.Header.Add("Content-Type", input.GetContentType())
//req.Header.Add("ConsistencyLevel", "eventual")

if c.UserAgent != "" {
Expand Down Expand Up @@ -265,6 +266,11 @@ func (i DeleteHttpRequestInput) GetConsistencyFailureFunc() ConsistencyFailureFu
return i.ConsistencyFailureFunc
}

// GetContentType returns the content type for the request, currently only application/json is supported
func (i DeleteHttpRequestInput) GetContentType() string {
return "application/json; charset=utf-8"
}

// GetValidStatusCodes returns a []int of status codes considered valid for a DELETE request.
func (i DeleteHttpRequestInput) GetValidStatusCodes() []int {
return i.ValidStatusCodes
Expand Down Expand Up @@ -308,6 +314,11 @@ func (i GetHttpRequestInput) GetConsistencyFailureFunc() ConsistencyFailureFunc
return i.ConsistencyFailureFunc
}

// GetContentType returns the content type for the request, currently only application/json is supported
func (i GetHttpRequestInput) GetContentType() string {
return "application/json; charset=utf-8"
}

// GetValidStatusCodes returns a []int of status codes considered valid for a GET request.
func (i GetHttpRequestInput) GetValidStatusCodes() []int {
return i.ValidStatusCodes
Expand Down Expand Up @@ -421,6 +432,11 @@ func (i PatchHttpRequestInput) GetConsistencyFailureFunc() ConsistencyFailureFun
return i.ConsistencyFailureFunc
}

// GetContentType returns the content type for the request, currently only application/json is supported
func (i PatchHttpRequestInput) GetContentType() string {
return "application/json; charset=utf-8"
}

// GetValidStatusCodes returns a []int of status codes considered valid for a PATCH request.
func (i PatchHttpRequestInput) GetValidStatusCodes() []int {
return i.ValidStatusCodes
Expand Down Expand Up @@ -463,6 +479,11 @@ func (i PostHttpRequestInput) GetConsistencyFailureFunc() ConsistencyFailureFunc
return i.ConsistencyFailureFunc
}

// GetContentType returns the content type for the request, currently only application/json is supported
func (i PostHttpRequestInput) GetContentType() string {
return "application/json; charset=utf-8"
}

// GetValidStatusCodes returns a []int of status codes considered valid for a POST request.
func (i PostHttpRequestInput) GetValidStatusCodes() []int {
return i.ValidStatusCodes
Expand Down Expand Up @@ -494,6 +515,7 @@ func (c Client) Post(ctx context.Context, input PostHttpRequestInput) (*http.Res
// PutHttpRequestInput configures a PUT request.
type PutHttpRequestInput struct {
ConsistencyFailureFunc ConsistencyFailureFunc
ContentType string
Body []byte
ValidStatusCodes []int
ValidStatusFunc ValidStatusFunc
Expand All @@ -505,6 +527,14 @@ func (i PutHttpRequestInput) GetConsistencyFailureFunc() ConsistencyFailureFunc
return i.ConsistencyFailureFunc
}

// GetContentType returns the content type for the request, defaults to application/json
func (i PutHttpRequestInput) GetContentType() string {
if i.ContentType != "" {
return i.ContentType
}
return "application/json; charset=utf-8"
}

// GetValidStatusCodes returns a []int of status codes considered valid for a PUT request.
func (i PutHttpRequestInput) GetValidStatusCodes() []int {
return i.ValidStatusCodes
Expand Down