Limit calls to only working days and specific hours in day

Not applicable

Hey! Is it possible to setup policy to restrict calls only to specific days of a week? And the same with specific hours. For example: Five days per week (not Saturday and Sunday) from 7 a.m. to 7 p.m. Cheers, Tomas

Solved Solved
1 8 507
2 ACCEPTED SOLUTIONS

Dear @Tomasz Korecki ,

Welcome to Apigee Community 🙂

Yes, It's possible. You can able to achieve same using Apigee Javascript Policy with minimal code.

  • Read present date & time using javascript
var d = new Date();

var n = d.getDay();

var n = d.getHours();
  • Compare with your conditions, set a variable based on result
  • Use raise fault policy to restrict to API based on above result to avoid hitting to backend api.

Cheers,

Anil Sagar

View solution in original post

Not applicable

I am really amazed by trace and Api console tools.

I've made few changes:

  1. Remove fault rules from proxy endpoint configuration.
  2. Moved condition checking to preflow of proxy endpoint configuration, so it looks like this:
<PreFlow name="PreFlow">
        <Request>
            <Step>
                <Name>JavascriptDatetimeParametersPolicy</Name>
            </Step>
            <Step>
                <Name>WorkingDaysAndHoursRaiseFault</Name>
                <Condition>(requestReject is true)</Condition>
            </Step>
        </Request>
        <Response/>
</PreFlow>

And it works as I want to. Thank you guys!

View solution in original post

8 REPLIES 8

Dear @Tomasz Korecki ,

Welcome to Apigee Community 🙂

Yes, It's possible. You can able to achieve same using Apigee Javascript Policy with minimal code.

  • Read present date & time using javascript
var d = new Date();

var n = d.getDay();

var n = d.getHours();
  • Compare with your conditions, set a variable based on result
  • Use raise fault policy to restrict to API based on above result to avoid hitting to backend api.

Cheers,

Anil Sagar

Not applicable

@Anil Sagar

Due to your advises, I've made several steps:

1) I've added new Javascript file in Scripts:

var day = new Date();
var dayOfTheWeek = day.getDay();
var hour = day.getHours();

var requestReject = (dayOfTheWeek >= 1 && dayOfTheWeek <= 5) && (hour >= 12 && hour <= 17); 

context.setVariable("requestReject", requestReject);

2) I've added new Javascript Policy:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Javascript async="false" continueOnError="false" enabled="true" timeLimit="200" name="JavascriptDatetimeParametersPolicy">
    <DisplayName>JavascriptDatetimeParametersPolicy</DisplayName>
    <Properties/>
    <ResourceURL>jsc://DatetimeParameters.js</ResourceURL>
</Javascript>
3) I've added policy to one of my flow, in the request node:
<Request>
	<Step>
		<Name>JavascriptDatetimeParametersPolicy</Name>
	</Step>
</Request>

4) I've added new RaiseFault policy:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RaiseFault async="false" continueOnError="false" enabled="true" name="WorkingDaysAndHoursRaiseFault">
    <DisplayName>WorkingDaysAndHoursRaiseFault</DisplayName>
    <Properties/>
    <FaultResponse>
        <Set>
            <Headers/>
            <Payload contentType="text/plain"/>
            <StatusCode>503</StatusCode>
            <ReasonPhrase>Service is available only in working days since 12 a.m. to 5 p. m.</ReasonPhrase>
        </Set>
    </FaultResponse>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</RaiseFault>

5) Finally, I've added FaultRules section to my ProxyEndpoint configuration:

<FaultRules>
        <FaultRule name="NotSupportedDayAndTime">
            <Step>
                <Name>WorkingDaysAndHoursRaiseFault</Name>
            </Step>
            <Condition>(context.requestReject = true")</Condition>
        </FaultRule>
</FaultRules>

It checks if the context variable which was setup in javascript file is true. And if it is, it raises the RaiseFault condition.

But this solution do not works for me. Service works normally, like there would not be any kind of fault rules or conditions.

Any ideas what I did wrong?

Cheers, Tomas

@Tomasz Korecki , condition variable should be just "requestReject" , use trace tool in Apigee Edge to debug same.

@Anil Sagar, That did not worked for me. I've also cannot debug with trace tool, because I'am using Basic Authentication in my flow. In trace tool I did not find options to setup username and password (I can do this in API console). Anyway, can I raise this fault rule before Basic Authentication policy? It should resolve the authentication problem.

Find out more about trace tool here. You can make call using API Console, it will be recorded in trace tool & helps you debug.

Hi @Tomasz Korecki, yes in trace session we cannot do that. you can use Postman or Api Console for Basic Auth Setting. So, start the Trace Session and then using API console, pass the Basic Auth and then go back to your trace session to debug the problem.

Not applicable

I am really amazed by trace and Api console tools.

I've made few changes:

  1. Remove fault rules from proxy endpoint configuration.
  2. Moved condition checking to preflow of proxy endpoint configuration, so it looks like this:
<PreFlow name="PreFlow">
        <Request>
            <Step>
                <Name>JavascriptDatetimeParametersPolicy</Name>
            </Step>
            <Step>
                <Name>WorkingDaysAndHoursRaiseFault</Name>
                <Condition>(requestReject is true)</Condition>
            </Step>
        </Request>
        <Response/>
</PreFlow>

And it works as I want to. Thank you guys!

Awesome @Tomasz Korecki , Glad your query is resolved 🙂