Skip to content

Commit

Permalink
Merge pull request #16 from commercetools/Add-ReadMe
Browse files Browse the repository at this point in the history
Add initial version of readMe file
  • Loading branch information
MicheleRezk authored Jan 19, 2021
2 parents 7041e4e + 469df6a commit 9bef201
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 34 deletions.
58 changes: 58 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# https://help.github.com/en/categories/automating-your-workflow-with-github-actions

name: RELEASE

on:
push:
tags:
- '[0-9]+.[0-9]+.[0-9]+*'

jobs:
artifacts:
name: Release
runs-on: ubuntu-latest
# needs: [unittests, integrationtests]
steps:
- uses: actions/checkout@v2
# Authenticates packages to push to GPR
- uses: actions/setup-dotnet@v1
with:
dotnet-version: '3.1'
source-url: https://nuget.pkg.github.com/commercetools/index.json
env:
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Retrieve branch name
id: branch_name
run: echo ::set-output name=SOURCE_TAG::${GITHUB_REF#refs/tags/}
- run: ./patch_csproj.rb --version $SOURCE_TAG --patch **/*.csproj
env:
SOURCE_TAG: ${{ steps.branch_name.outputs.SOURCE_TAG }}
- run: dotnet restore --configfile Nuget.config --packages ../packages
working-directory: ./commercetools.Sdk
- run: dotnet build --no-restore --source ../packages -c Release
working-directory: ./commercetools.Sdk
- name: Create the package
run: dotnet pack -c Release -o ../pack/ --configfile Nuget.config
working-directory: ./commercetools.Sdk
- name: List packages
run: ls -la pack
- name: Publish Nuget to GitHub registry
run: dotnet nuget push "pack/*.nupkg" -k ${GITHUB_TOKEN} -s https://nuget.pkg.github.com/commercetools/index.json --skip-duplicate --no-symbols true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/upload-artifact@v1
with:
name: commercetools.Sdkv2.${{ steps.branch_name.outputs.SOURCE_TAG }}
path: pack
- name: Push generated package to NuGet
run: dotnet nuget push "pack/*.nupkg" --api-key ${{ secrets.NUGET_TOKEN }} -s https://api.nuget.org/v3/index.json --skip-duplicate --no-symbols true
- name: Create GH Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: ${{ github.ref }}
draft: false
prerelease: false
142 changes: 142 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# commercetools .NET SDK

## Introduction

This repository contains the commercetools platform C# SDK generated from our api reference. The SDK enables developers to communicate with the [commercetools HTTP API](https://docs.commercetools.com/http-api.html). The developers do not need to create plain HTTP requests, but they can instead use the domain specific classes and methods to formulate valid requests.

## Technical Overview

The SDK consists of the following projects:
* `commercetools.Base.Abstractions`: Contains common classes and interfaces that can be used in other SDK projects like IClient and ISerializerService.
* `commercetools.Base.Client`: Contains CtpClient which communicate with the platform to execute requests, it contains also the classes related to the client like tokens,middlewares and handlers.
* `commercetools.Base.Registration`: Helper classes for things like types retriever.
* `commercetools.Base.Serialization`: Serialization and deserialization services for responses and requests to the HTTP API using System.Text.Json.
* `commercetools.SDK.Api`: Contains all generated models and request builders to communicate with the platform api.

In addition, the SDK has the following directories:
* `/IntegrationTests`: Integration tests for the SDK. A good way for anyone using the .NET SDK to understand it further.
* `/Tests`: Unit Tests for serialization and request builders.


## Getting Started with the .NET SDK

All operations (get, query, create, update, delete and others) are available in the generated request builders, so you can build the request and use the client to execute it. all the request builders accessible through ApiRoot, you can use the instance inside the injected client using function client.WithApi() or use ApiFactory to create a new instance, In order to use the client object, it needs to be setup first through dependency injection setup.

### Basic Workflow

At a high level, to make a basic call to the API, do the following:

1. Use the dependency injection class to set things up.
2. get a client object from the services responsible for calling requests to the API
3. use the ApiRoot instance inside the client and identify the project-key.
4. If needed – Create a draft object as a required for the request based on the documentation.
5. Build your request and execute it using ExecuteAsync.
6. Receive the response as a model.

### Dependency Injection Setup

In the ConfigureServices method of Startup.cs add the following:

```c#
services.UseCommercetoolsApi(
this.configuration); // replace with your instance of IConfiguration
```
### Configuration
The client configuration needs to be added to appsettings.json in order for the client to work. The structure is as follows:

```c#
{
"Client": {
"ClientId": "", // replace with your client ID
"ClientSecret": "", // replace with your client secret
"AuthorizationBaseAddress": "https://auth.europe-west1.gcp.commercetools.com/", // replace if needed
"Scope": "", // replace with your scope
"ProjectKey": "", // replace with your project key
"ApiBaseAddress": "https://api.europe-west1.gcp.commercetools.com/" // replace if needed
}
}
```

## Using SDK

SDK follows a builder pattern when creating requests. Category resource will be used to demonstrate how to use the SDK. This behaviour is the same for all resources.
The IClient interface can be used by injecting it and calling its ExecuteAsync method for different requests.
```c#
private readonly IClient client;
public CategoryController(IClient client)
{
this.client = client;
}
public void CreatingRequests()
{
// Create CategoryDraft using builder pattern
var categoryDraft = new CategoryDraft
{
Name = new LocalizedString {{"en", "Name"}},
Slug = new LocalizedString {{"en", "Slug"}},
Key = "Key"
};

// Use in the previous step configured client instance to send and receive a newly created Category
var category = await client.WithApi().WithProjectKey("project-key")
.Categories()
.Post(categoryDraft)
.ExecuteAsync();

// Get Category by id
var queriedCategory = await client.WithApi().WithProjectKey("project-key")
.Categories()
.WithId(category.Id)
.Get()
.ExecuteAsync();

// Get Category by key
var queriedCategory = await client.WithApi().WithProjectKey("project-key")
.Categories()
.WithKey(category.Key)
.Get()
.ExecuteAsync();

// Query Categories
var response = await client.WithApi().WithProjectKey("project-key")
.Categories()
.Get()
.WithWhere($"key = \"{category.Key}\"")
.ExecuteAsync();

// Delete Category by id
var deletedCategory = await client.WithApi().WithProjectKey("project-key")
.Categories()
.WithId(category.Id)
.Delete()
.WithVersion(category.version)
.ExecuteAsync();

// Update Category
var newName = "newName";
var action = new CategoryChangeNameAction
{
Name = new LocalizedString {{"en", newName}}
};
var update = new CategoryUpdate
{
Version = category.Version,
Actions = new List<ICategoryUpdateAction> {action}
};

var updatedCategory = await client.WithApi().WithProjectKey("project-key")
.Categories()
.WithId(category.Id)
.Post(categoryUpdate)
.ExecuteAsync();

// Delete Category by key
var deletedCategory = await client.WithApi().WithProjectKey("project-key")
.Categories()
.WithKey(category.Key)
.Delete()
.WithVersion(category.Version)
.ExecuteAsync();
}

```
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static CategoryDraft DefaultCategoryDraftWithParent(CategoryDraft draft,

public static async Task<Category> CreateCategory(IClient client, CategoryDraft categoryDraft)
{
return await client.ApiRoot().WithProjectKey(DefaultProjectKey)
return await client.WithApi().WithProjectKey(DefaultProjectKey)
.Categories()
.Post(categoryDraft)
.ExecuteAsync();
Expand All @@ -56,7 +56,7 @@ public static async Task DeleteCategory(IClient client, Category category)
{
try
{
await client.ApiRoot().WithProjectKey(DefaultProjectKey)
await client.WithApi().WithProjectKey(DefaultProjectKey)
.Categories()
.WithId(category.Id)
.Delete()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ await WithCategory(
async category =>
{
Assert.NotNull(category);
var retrievedCategory = await _client.ApiRoot().WithProjectKey(_projectKey)
var retrievedCategory = await _client.WithApi().WithProjectKey(_projectKey)
.Categories()
.WithId(category.Id)
.Get()
Expand All @@ -66,7 +66,7 @@ await WithCategory(
async category =>
{
Assert.NotNull(category);
var retrievedCategory = await _client.ApiRoot().WithProjectKey(_projectKey)
var retrievedCategory = await _client.WithApi().WithProjectKey(_projectKey)
.Categories()
.WithKey(category.Key)
.Get()
Expand All @@ -86,7 +86,7 @@ await WithCategory(
async category =>
{
Assert.NotNull(category);
var returnedSet = await _client.ApiRoot().WithProjectKey(_projectKey)
var returnedSet = await _client.WithApi().WithProjectKey(_projectKey)
.Categories()
.Get()
.WithWhere($"key = \"{category.Key}\"")
Expand All @@ -107,7 +107,7 @@ await WithCategory(
{
Assert.NotNull(category);

var returnedSet = await _client.ApiRoot().WithProjectKey(_projectKey)
var returnedSet = await _client.WithApi().WithProjectKey(_projectKey)
.Categories()
.Get()
.WithWhere($"key = \"{category.Key}\"")
Expand Down Expand Up @@ -140,7 +140,7 @@ await WithListOfCategories(
var orderedCategoriesNames =
categoriesList.OrderBy(c => c.Name["en"]).Select(c => c.Name["en"]).ToList();

var returnedSet = await _client.ApiRoot().WithProjectKey(_projectKey)
var returnedSet = await _client.WithApi().WithProjectKey(_projectKey)
.Categories()
.Get()
.WithWhere($"parent(id = \"{parentCategory.Id}\")")
Expand Down Expand Up @@ -172,7 +172,7 @@ await WithListOfCategories(
var orderedCategoriesNames =
categoriesList.OrderByDescending(c => c.Name["en"]).Select(c => c.Name["en"]).ToList();

var returnedSet = await _client.ApiRoot().WithProjectKey(_projectKey)
var returnedSet = await _client.WithApi().WithProjectKey(_projectKey)
.Categories()
.Get()
.WithWhere($"parent(id = \"{parentCategory.Id}\")")
Expand Down Expand Up @@ -205,7 +205,7 @@ await WithListOfCategories(

await AssertEventuallyAsync(async () =>
{
var returnedSet = await _client.ApiRoot().WithProjectKey(_projectKey)
var returnedSet = await _client.WithApi().WithProjectKey(_projectKey)
.Categories()
.Get()
.WithWhere($"parent(id = \"{parentCategory.Id}\")")
Expand Down Expand Up @@ -236,15 +236,15 @@ await WithCategory(
{
Assert.NotNull(category);

await _client.ApiRoot().WithProjectKey(_projectKey)
await _client.WithApi().WithProjectKey(_projectKey)
.Categories()
.WithId(category.Id)
.Delete()
.WithVersion(category.Version)
.ExecuteAsync();

await Assert.ThrowsAsync<NotFoundException>(
() => _client.ApiRoot().WithProjectKey(_projectKey).Categories().WithId(category.Id).Get().ExecuteAsync());
() => _client.WithApi().WithProjectKey(_projectKey).Categories().WithId(category.Id).Get().ExecuteAsync());
});
}

Expand All @@ -258,15 +258,15 @@ await WithCategory(
{
Assert.NotNull(category);

await _client.ApiRoot().WithProjectKey(_projectKey)
await _client.WithApi().WithProjectKey(_projectKey)
.Categories()
.WithKey(category.Key)
.Delete()
.WithVersion(category.Version)
.ExecuteAsync();

await Assert.ThrowsAsync<NotFoundException>(
() => _client.ApiRoot().WithProjectKey(_projectKey).Categories().WithId(category.Id).Get().ExecuteAsync());
() => _client.WithApi().WithProjectKey(_projectKey).Categories().WithId(category.Id).Get().ExecuteAsync());
});
}

Expand All @@ -279,7 +279,7 @@ await WithCategory(
async category =>
{
Assert.NotNull(category);
var retrievedCategory = await _client.ApiRoot().WithProjectKey(_projectKey)
var retrievedCategory = await _client.WithApi().WithProjectKey(_projectKey)
.Categories()
.WithId(category.Id)
.Get()
Expand Down Expand Up @@ -312,7 +312,7 @@ await WithUpdateableCategory(_client, async category =>
Actions = new List<ICategoryUpdateAction> {action}
};

var updatedCategory = await _client.ApiRoot().WithProjectKey(_projectKey)
var updatedCategory = await _client.WithApi().WithProjectKey(_projectKey)
.Categories()
.WithKey(category.Key)
.Post(update)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static PaymentDraft DefaultPaymentDraftWithTransaction(PaymentDraft draft

public static async Task<Payment> CreatePayment(IClient client, PaymentDraft paymentDraft)
{
var resource = await client.ApiRoot().WithProjectKey(DefaultProjectKey)
var resource = await client.WithApi().WithProjectKey(DefaultProjectKey)
.Payments()
.Post(paymentDraft)
.ExecuteAsync();
Expand All @@ -43,7 +43,7 @@ public static async Task DeletePayment(IClient client, Payment payment)
{
try
{
await client.ApiRoot().WithProjectKey(DefaultProjectKey)
await client.WithApi().WithProjectKey(DefaultProjectKey)
.Payments()
.WithId(payment.Id)
.Delete()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task SearchByFullLocale()
await AssertEventuallyAsync(async () =>
{
//Act
var searchResult = await _client.ApiRoot()
var searchResult = await _client.WithApi()
.WithProjectKey(_projectKey)
.ProductProjections()
.Search()
Expand Down
Loading

0 comments on commit 9bef201

Please sign in to comment.