Skip to content

Commit

Permalink
Add APIVersion policy (Azure#326)
Browse files Browse the repository at this point in the history
Add APIVersion policy
  -Policy adds the service version to all requests.
  -Service can be set on headers or query parameters

Rename boolean for clarity.
Add comment describing how the boolean operates
  • Loading branch information
RickWinter authored Jan 31, 2020
1 parent ff9737d commit c90c1a2
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 23 deletions.
2 changes: 1 addition & 1 deletion sdk/core/core/inc/az_http_pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <_az_cfg_prefix.h>

typedef struct {
az_http_policy policies[8];
az_http_policy policies[9];
} az_http_pipeline;

// Start the pipeline
Expand Down
21 changes: 21 additions & 0 deletions sdk/core/core/inc/az_http_policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@

typedef struct az_http_policy az_http_policy;

/**
* @brief options for retry policy
* max_retry = maximun number of retry intents before returning error
* delay_in_ms = waiting time before retrying in miliseconds
*
*/
typedef struct {
// Services pass API versions in the header or in query parameters
// true: api version is passed via headers
// false: api version is passed via query parameters
bool add_as_header;
az_span name;
az_span version;
} _az_http_policy_apiversion_options;

/**
* @brief options for retry policy
* max_retry = maximun number of retry intents before returning error
Expand All @@ -52,6 +67,12 @@ struct az_http_policy {
void * data;
};

AZ_NODISCARD az_result az_http_pipeline_policy_apiversion(
az_http_policy * const policies,
void * const data,
az_http_request_builder * const hrb,
az_http_response * const response);

AZ_NODISCARD az_result az_http_pipeline_policy_uniquerequestid(
az_http_policy * const policies,
void * const data,
Expand Down
2 changes: 2 additions & 0 deletions sdk/core/core/internal/az_http_header_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

#include <_az_cfg_prefix.h>

static az_span const AZ_HTTP_HEADER_API_VERSION = AZ_CONST_STR("api-version");

static az_span const AZ_HTTP_HEADER_CONTENT_LENGTH = AZ_CONST_STR("Content-Length");
static az_span const AZ_HTTP_HEADER_CONTENT_TYPE = AZ_CONST_STR("Content-Type");

Expand Down
20 changes: 20 additions & 0 deletions sdk/core/core/src/az_http_policy.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ AZ_NODISCARD AZ_INLINE az_result az_http_pipeline_nextpolicy(

static az_span const AZ_MS_CLIENT_REQUESTID = AZ_CONST_STR("x-ms-client-request-id");

AZ_NODISCARD az_result az_http_pipeline_policy_apiversion(
az_http_policy * const p_policies,
void * const data,
az_http_request_builder * const hrb,
az_http_response * const response) {

_az_http_policy_apiversion_options * options = (_az_http_policy_apiversion_options *)(data);

if (options->add_as_header) {
// Add the version as a header
AZ_RETURN_IF_FAILED(
az_http_request_builder_append_header(hrb, options->name, options->version));
} else {
// Add the version as a query parameter
AZ_RETURN_IF_FAILED(
az_http_request_builder_set_query_parameter(hrb, options->name, options->version));
}
return az_http_pipeline_nextpolicy(p_policies, hrb, response);
}

AZ_NODISCARD az_result az_http_pipeline_policy_uniquerequestid(
az_http_policy * const p_policies,
void * const data,
Expand Down
17 changes: 14 additions & 3 deletions sdk/keyvault/keyvault/inc/az_keyvault.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
#ifndef _az_KEYVAULT_H
#define _az_KEYVAULT_H

#include <az_contract.h>
#include <az_http_header_internal.h>
#include <az_http_pipeline.h>
#include <az_http_response.h>
#include <az_contract.h>
#include <az_identity_access_token.h>
#include <az_identity_access_token_context.h>
#include <az_keyvault_create_key_options.h>
Expand All @@ -18,6 +19,9 @@

#include <_az_cfg_prefix.h>

/**
* @brief SDKs are specific to a fixed version of the KeyVault service
*/
static az_span const AZ_KEYVAULT_API_VERSION = AZ_CONST_STR("7.0");

typedef struct {
Expand All @@ -32,11 +36,13 @@ typedef struct {
} az_keyvault_keys_keys_options;

typedef struct {
_az_http_policy_apiversion_options _apiversion_options;
az_identity_access_token _token;
az_identity_access_token_context _token_context;

az_span uri;
az_http_pipeline pipeline;
az_keyvault_keys_client_options retry_options;
az_identity_access_token _token;
az_identity_access_token_context _token_context;
} az_keyvault_keys_client;

extern az_keyvault_keys_client_options const AZ_KEYVAULT_CLIENT_DEFAULT_OPTIONS;
Expand Down Expand Up @@ -64,6 +70,10 @@ AZ_NODISCARD AZ_INLINE az_result az_keyvault_keys_client_init(
AZ_CONTRACT_ARG_NOT_NULL(self);

*self = (az_keyvault_keys_client){
._apiversion_options
= (_az_http_policy_apiversion_options){ .add_as_header = false,
.name = AZ_HTTP_HEADER_API_VERSION,
.version = AZ_KEYVAULT_API_VERSION },
.uri = uri,
.pipeline = { 0 },
.retry_options = options == NULL ? AZ_KEYVAULT_CLIENT_DEFAULT_OPTIONS : *options,
Expand All @@ -80,6 +90,7 @@ AZ_NODISCARD AZ_INLINE az_result az_keyvault_keys_client_init(

self->pipeline = (az_http_pipeline){
.policies = {
{ .pfnc_process = az_http_pipeline_policy_apiversion, .data = &self->_apiversion_options },
{ .pfnc_process = az_http_pipeline_policy_uniquerequestid, .data = NULL },
{ .pfnc_process = az_http_pipeline_policy_retry, .data = &(self->retry_options.retry) },
{ .pfnc_process = az_http_pipeline_policy_authentication, .data = &(self->_token_context) },
Expand Down
16 changes: 0 additions & 16 deletions sdk/keyvault/keyvault/src/az_keyvault_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,8 @@ AZ_NODISCARD az_result az_keyvault_keys_key_create(
AZ_RETURN_IF_FAILED(
az_http_request_builder_append_path(&hrb, az_keyvault_client_constant_for_keys()));

// add version to request
AZ_RETURN_IF_FAILED(az_http_request_builder_set_query_parameter(
&hrb, AZ_STR("api-version"), AZ_KEYVAULT_API_VERSION));

AZ_RETURN_IF_FAILED(az_http_request_builder_append_path(&hrb, key_name));

// add extra header just for testing append_path after another query
AZ_RETURN_IF_FAILED(
az_http_request_builder_set_query_parameter(&hrb, AZ_STR("ignore"), AZ_KEYVAULT_API_VERSION));

AZ_RETURN_IF_FAILED(
az_http_request_builder_append_path(&hrb, az_keyvault_client_constant_for_create()));

Expand Down Expand Up @@ -189,10 +181,6 @@ AZ_NODISCARD az_result az_keyvault_keys_key_get(
AZ_RETURN_IF_FAILED(
az_http_request_builder_append_path(&hrb, az_keyvault_client_constant_for_keys()));

// add version to request as query parameter
AZ_RETURN_IF_FAILED(az_http_request_builder_set_query_parameter(
&hrb, AZ_STR("api-version"), AZ_KEYVAULT_API_VERSION));

// Add path to request after adding query parameter
AZ_RETURN_IF_FAILED(az_http_request_builder_append_path(&hrb, key_name));

Expand Down Expand Up @@ -223,10 +211,6 @@ AZ_NODISCARD az_result az_keyvault_keys_key_delete(
client->uri,
az_span_empty()));

// add version to request
AZ_RETURN_IF_FAILED(az_http_request_builder_set_query_parameter(
&hrb, AZ_STR("api-version"), AZ_KEYVAULT_API_VERSION));

// Add path to request
AZ_RETURN_IF_FAILED(
az_http_request_builder_append_path(&hrb, az_keyvault_client_constant_for_keys()));
Expand Down
14 changes: 11 additions & 3 deletions sdk/storage/blobs/inc/az_storage_blobs.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
#ifndef _az_STORAGE_BLOBS_H
#define _az_STORAGE_BLOBS_H

#include <az_contract.h>
#include <az_http_header_internal.h>
#include <az_http_pipeline.h>
#include <az_http_policy.h>
#include <az_http_response.h>
#include <az_contract.h>
#include <az_identity_access_token.h>
#include <az_identity_access_token_context.h>
#include <az_result.h>
Expand Down Expand Up @@ -38,12 +39,14 @@ typedef struct {
} az_storage_blobs_blob_download_options;

typedef struct {
_az_http_policy_apiversion_options _apiversion_options;
az_identity_access_token _token;
az_identity_access_token_context _token_context;

az_span uri;
az_span blob_type;
az_http_pipeline pipeline;
az_storage_blobs_blob_client_options client_options;
az_identity_access_token _token;
az_identity_access_token_context _token_context;
} az_storage_blobs_blob_client;

extern az_storage_blobs_blob_client_options const AZ_STORAGE_BLOBS_BLOB_CLIENT_DEFAULT_OPTIONS;
Expand Down Expand Up @@ -74,6 +77,10 @@ AZ_NODISCARD AZ_INLINE az_result az_storage_blobs_blob_client_init(
.uri = uri,
.pipeline = { 0 },
.client_options = options == NULL ? AZ_STORAGE_BLOBS_BLOB_CLIENT_DEFAULT_OPTIONS : *options,
._apiversion_options
= (_az_http_policy_apiversion_options){ .add_as_header = true,
.name = AZ_HTTP_HEADER_X_MS_VERSION,
.version = AZ_STORAGE_BLOBS_BLOB_API_VERSION },
._token = { 0 },
._token_context = { 0 },
};
Expand All @@ -87,6 +94,7 @@ AZ_NODISCARD AZ_INLINE az_result az_storage_blobs_blob_client_init(

client->pipeline = (az_http_pipeline){
.policies = {
{ .pfnc_process = az_http_pipeline_policy_apiversion, .data = &client->_apiversion_options },
{ .pfnc_process = az_http_pipeline_policy_uniquerequestid, .data = NULL },
{ .pfnc_process = az_http_pipeline_policy_retry, .data = &client->client_options.retry },
{ .pfnc_process = az_http_pipeline_policy_authentication, .data = &(client->_token_context) },
Expand Down

0 comments on commit c90c1a2

Please sign in to comment.