Merge pull request #1 from MarvinJWendt/patch-1

added syntax highlighting to readme
This commit is contained in:
Samuel Lorch 2021-08-30 18:19:18 +02:00 committed by GitHub
commit 4da06d62b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -18,7 +18,8 @@ PR's are Welcome, if it's something bigger / fundamental: Please make a Issue an
# Examples # Examples
## Login ## Login
First you will need to Create a Client, and then Login on the Server using the Client First you will need to Create a Client, and then Login on the Server using the Client
```
```go
package main package main
import ( import (
@ -65,7 +66,8 @@ You can do this using the `client.CheckSession()` function.
## Create a Resource ## Create a Resource
Creating a Resource using the helper package is simple, first add `"github.com/speatzle/go-passbolt/helper"` to your imports. Creating a Resource using the helper package is simple, first add `"github.com/speatzle/go-passbolt/helper"` to your imports.
Then you can simply: Then you can simply:
```
```go
resourceID, err := helper.CreateResource( resourceID, err := helper.CreateResource(
ctx, // Context ctx, // Context
client, // API Client client, // API Client
@ -77,8 +79,10 @@ resourceID, err := helper.CreateResource(
"This is a Account for the example test portal", // Description "This is a Account for the example test portal", // Description
) )
``` ```
Creating a (Legacy) Resource Without the helper package would look like this: Creating a (Legacy) Resource Without the helper package would look like this:
```
```go
enc, err := client.EncryptMessage("securePassword123") enc, err := client.EncryptMessage("securePassword123")
if err != nil { if err != nil {
panic(err) panic(err)
@ -99,6 +103,7 @@ if err != nil {
panic(err) panic(err)
} }
``` ```
Note: Since Passbolt v3 There are Resource Types, this Manual Example just creates a "password-string" Type Password where the Description is Unencrypted, Read More [Here](https://help.passbolt.com/api/resource-types). Note: Since Passbolt v3 There are Resource Types, this Manual Example just creates a "password-string" Type Password where the Description is Unencrypted, Read More [Here](https://help.passbolt.com/api/resource-types).
## Getting ## Getting
@ -106,14 +111,17 @@ Generally API Get Calls will have options (opts) that allow for specifing filter
Filters just filter by whatever is given, contains on the otherhand specify what to include in the response. Many Filters And Contains are undocumented in the Passbolt Docs. Filters just filter by whatever is given, contains on the otherhand specify what to include in the response. Many Filters And Contains are undocumented in the Passbolt Docs.
Here We Specify that we want to Filter by Favorites and that the Response Should Contain the Permissions for each Resource: Here We Specify that we want to Filter by Favorites and that the Response Should Contain the Permissions for each Resource:
```
```go
favorites, err := client.GetResources(ctx, &api.GetResourcesOptions{ favorites, err := client.GetResources(ctx, &api.GetResourcesOptions{
FilterIsFavorite: true, FilterIsFavorite: true,
ContainPermissions: true, ContainPermissions: true,
}) })
``` ```
We Can do the Same for Users: We Can do the Same for Users:
```
```go
users, err := client.GetUsers(ctx, &api.GetUsersOptions{ users, err := client.GetUsers(ctx, &api.GetUsersOptions{
FilterSearch: "Samuel", FilterSearch: "Samuel",
ContainLastLoggedIn: true, ContainLastLoggedIn: true,
@ -121,7 +129,8 @@ users, err := client.GetUsers(ctx, &api.GetUsersOptions{
``` ```
Groups: Groups:
```
```go
groups, err := client.GetGroups(ctx, &api.GetGroupsOptions{ groups, err := client.GetGroups(ctx, &api.GetGroupsOptions{
FilterHasUsers: []string{"id of user", "id of other user"}, FilterHasUsers: []string{"id of user", "id of other user"},
ContainUser: true, ContainUser: true,
@ -129,7 +138,8 @@ groups, err := client.GetGroups(ctx, &api.GetGroupsOptions{
``` ```
And also for Folders (PRO Only): And also for Folders (PRO Only):
```
```go
folders, err := client.GetFolders(ctx, &api.GetFolderOptions{ folders, err := client.GetFolders(ctx, &api.GetFolderOptions{
FilterSearch: "Test Folder", FilterSearch: "Test Folder",
ContainChildrenResources: true, ContainChildrenResources: true,
@ -137,25 +147,29 @@ folders, err := client.GetFolders(ctx, &api.GetFolderOptions{
``` ```
Getting by ID is also Supported Using the Singular Form: Getting by ID is also Supported Using the Singular Form:
```
```go
resource, err := client.GetResource(ctx, "resource ID") resource, err := client.GetResource(ctx, "resource ID")
``` ```
Since the Password is Encrypted (and sometimes the description too) the helper package has a function to decrypt all encrypted fields Automatically: Since the Password is Encrypted (and sometimes the description too) the helper package has a function to decrypt all encrypted fields Automatically:
```
```go
folderParentID, name, username, uri, password, description, err := helper.GetResource(ctx, client, "resource id") folderParentID, name, username, uri, password, description, err := helper.GetResource(ctx, client, "resource id")
``` ```
## Updating ## Updating
The Helper Package has a function to save you needing to deal with Resource Types When Updating a Resource: The Helper Package has a function to save you needing to deal with Resource Types When Updating a Resource:
```
```go
err := helper.UpdateResource(ctx, client,"resource id", "name", "username", "https://test.example.com", "pass123", "very descriptive") err := helper.UpdateResource(ctx, client,"resource id", "name", "username", "https://test.example.com", "pass123", "very descriptive")
``` ```
Note: As Groups are also Complicated to Update there will be a helper function for them in the future. Note: As Groups are also Complicated to Update there will be a helper function for them in the future.
For other less Complicated Updates you can Simply use the Client directly: For other less Complicated Updates you can Simply use the Client directly:
```
```go
client.UpdateUser(ctx, "user id", api.User{ client.UpdateUser(ctx, "user id", api.User{
Profile: &api.Profile{ Profile: &api.Profile{
FirstName: "Test", FirstName: "Test",
@ -170,13 +184,16 @@ As Sharing Resources is very Complicated there are multipe helper Functions. Dur
The permissionType can be 1 for Read only, 7 for Can Update, 15 for Owner or -1 if you want to delete Existing Permissions. The permissionType can be 1 for Read only, 7 for Can Update, 15 for Owner or -1 if you want to delete Existing Permissions.
The ShareResourceWithUsersAndGroups function Shares the Resource With all Provided Users and Groups with the Given permissionType. The ShareResourceWithUsersAndGroups function Shares the Resource With all Provided Users and Groups with the Given permissionType.
```
```go
err := helper.ShareResourceWithUsersAndGroups(ctx, client, "resource id", []string{"user 1 id"}, []string{"group 1 id"}, 7) err := helper.ShareResourceWithUsersAndGroups(ctx, client, "resource id", []string{"user 1 id"}, []string{"group 1 id"}, 7)
``` ```
Note: Existing Permission of Users and Groups will be adjusted to be of the Provided permissionType. Note: Existing Permission of Users and Groups will be adjusted to be of the Provided permissionType.
If you need to do something more Complicated like setting Users/Groups to different Type then you can Use ShareResource directly: If you need to do something more Complicated like setting Users/Groups to different Type then you can Use ShareResource directly:
```
```go
changes := []helper.ShareOperation{} changes := []helper.ShareOperation{}
// Make this User Owner // Make this User Owner
@ -209,11 +226,13 @@ changes = append(changes, ShareOperation{
err := helper.ShareResource(ctx, c, resourceID, changes) err := helper.ShareResource(ctx, c, resourceID, changes)
``` ```
Note: These Functions are Also Availabe for Folders (PRO) Note: These Functions are Also Availabe for Folders (PRO)
## Full Example ## Full Example
This Example Creates a Resource, Searches for a User Named Test User, Checks that its Not itself and Shares the Password with the Test User if Nessesary: This Example Creates a Resource, Searches for a User Named Test User, Checks that its Not itself and Shares the Password with the Test User if Nessesary:
```
```go
package main package main
import ( import (
@ -302,4 +321,4 @@ func main() {
- get a Passbolt Instance to Work in Github Actions - get a Passbolt Instance to Work in Github Actions
- write Integration Tests - write Integration Tests
- add ability to verify Server - add ability to verify Server
- add helper functions for updating Groups - add helper functions for updating Groups