public void TestCreateAdUnits() {
      // Create ad unit 1.
      AdUnit localAdUnit1 = new AdUnit();
      localAdUnit1.name = string.Format("Ad_Unit_{0}", new TestUtils().GetTimeStamp());
      localAdUnit1.parentId = adUnit1.id;

      Size size1 = new Size();
      size1.width = 300;
      size1.height = 250;

      AdUnitSize adUnitSize1 = new AdUnitSize();
      adUnitSize1.size = size1;
      adUnitSize1.environmentType = EnvironmentType.BROWSER;

      localAdUnit1.adUnitSizes = new AdUnitSize[] {adUnitSize1};

      // Create ad unit 2.
      AdUnit localAdUnit2 = new AdUnit();
      localAdUnit2.name = string.Format("Ad_Unit_{0}", new TestUtils().GetTimeStamp());
      localAdUnit2.parentId = adUnit1.id;

      Size size2 = new Size();
      size2.width = 300;
      size2.height = 250;

      AdUnitSize adUnitSize2 = new AdUnitSize();
      adUnitSize2.size = size2;
      adUnitSize2.environmentType = EnvironmentType.BROWSER;

      localAdUnit2.adUnitSizes = new AdUnitSize[] {adUnitSize2};

      AdUnit[] newAdUnits = null;

      Assert.DoesNotThrow(delegate() {
        newAdUnits = inventoryService.createAdUnits(new AdUnit[] {localAdUnit1, localAdUnit2});
      });

      Assert.NotNull(newAdUnits);
      Assert.AreEqual(newAdUnits.Length, 2);

      Assert.AreEqual(newAdUnits[0].name, localAdUnit1.name);
      Assert.AreEqual(newAdUnits[0].parentId, localAdUnit1.parentId);
      Assert.AreEqual(newAdUnits[0].parentId, adUnit1.id);
      Assert.AreEqual(newAdUnits[0].status, localAdUnit1.status);
      Assert.AreEqual(newAdUnits[0].targetWindow, localAdUnit1.targetWindow);

      Assert.AreEqual(newAdUnits[1].name, localAdUnit2.name);
      Assert.AreEqual(newAdUnits[1].parentId, localAdUnit2.parentId);
      Assert.AreEqual(newAdUnits[1].parentId, adUnit1.id);
      Assert.AreEqual(newAdUnits[1].status, localAdUnit2.status);
      Assert.AreEqual(newAdUnits[1].targetWindow, localAdUnit2.targetWindow);
    }
    /// <summary>
    /// Run the code example.
    /// </summary>
    /// <param name="user">The DFP user object running the code example.</param>
    public override void Run(DfpUser user) {
      // Get the InventoryService.
      InventoryService inventoryService =
          (InventoryService) user.GetService(DfpService.v201502.InventoryService);

      // Get the NetworkService.
      NetworkService networkService =
          (NetworkService) user.GetService(DfpService.v201502.NetworkService);

      string effectiveRootAdUnitId = networkService.getCurrentNetwork().effectiveRootAdUnitId;

      // Create an array to store local ad unit objects.
      AdUnit[] adUnits = new AdUnit[5];

      for (int i = 0; i < 5; i++) {
        AdUnit adUnit = new AdUnit();
        adUnit.name = string.Format("Ad_Unit_{0}", i);
        adUnit.parentId = effectiveRootAdUnitId;

        adUnit.description = "Ad unit description.";
        adUnit.targetWindow = AdUnitTargetWindow.BLANK;

        // Set the size of possible creatives that can match this ad unit.
        Size size = new Size();
        size.width = 300;
        size.height = 250;

        // Create ad unit size.
        AdUnitSize adUnitSize = new AdUnitSize();
        adUnitSize.size = size;
        adUnitSize.environmentType = EnvironmentType.BROWSER;

        adUnit.adUnitSizes = new AdUnitSize[] {adUnitSize};
        adUnits[i] = adUnit;
      }

      try {
        // Create the ad units on the server.
        adUnits = inventoryService.createAdUnits(adUnits);

        if (adUnits != null) {
          foreach (AdUnit adUnit in adUnits) {
            Console.WriteLine("An ad unit with ID = '{0}' was created under parent with " +
                "ID = '{1}'.", adUnit.id, adUnit.parentId);
          }
        } else {
          Console.WriteLine("No ad units created.");
        }
      } catch (Exception ex) {
        Console.WriteLine("Failed to create ad units. Exception says \"{0}\"", ex.Message);
      }
    }
    /// <summary>
    /// Run the code example.
    /// </summary>
    /// <param name="user">The DFP user object running the code example.</param>
    public override void Run(DfpUser user) {
      // Get the InventoryService.
      InventoryService inventoryService =
          (InventoryService) user.GetService(DfpService.v201502.InventoryService);

      // Get the NetworkService.
      NetworkService networkService =
          (NetworkService) user.GetService(DfpService.v201502.NetworkService);

      // Set the parent ad unit's ID for all ad units to be created under.
      String effectiveRootAdUnitId = networkService.getCurrentNetwork().effectiveRootAdUnitId;

      // Create local ad unit object.
      AdUnit adUnit = new AdUnit();
      adUnit.name = "Mobile_Ad_Unit";
      adUnit.parentId = effectiveRootAdUnitId;
      adUnit.description = "Ad unit description.";
      adUnit.targetWindow = AdUnitTargetWindow.BLANK;
      adUnit.targetPlatform = TargetPlatform.MOBILE;

      // Create ad unit size.
      AdUnitSize adUnitSize = new AdUnitSize();
      Size size = new Size();
      size.width = 400;
      size.height = 300;
      size.isAspectRatio = false;
      adUnitSize.size = size;
      adUnitSize.environmentType = EnvironmentType.BROWSER;

      // Set the size of possible creatives that can match this ad unit.
      adUnit.adUnitSizes = new AdUnitSize[] {adUnitSize};

      try {
        // Create the ad unit on the server.
        AdUnit[] createdAdUnits = inventoryService.createAdUnits(new AdUnit[] {adUnit});

        foreach (AdUnit createdAdunit in createdAdUnits) {
          Console.WriteLine("An ad unit with ID \"{0}\" was created under parent with ID " +
              "\"{1}\".", createdAdunit.id, createdAdunit.parentId);
        }
      } catch (Exception e) {
        Console.WriteLine("Failed to create ad units. Exception says \"{0}\"", e.Message);
      }
    }
예제 #4
0
        public AdUnit CreateAdUnit(DfpUser user)
        {
            InventoryService inventoryService =
              (InventoryService) user.GetService(DfpService.v201502.InventoryService);

              AdUnit adUnit = new AdUnit();
              adUnit.name = string.Format("Ad_Unit_{0}", GetTimeStamp());
              adUnit.parentId = FindRootAdUnit(user).id;

              // Set the size of possible creatives that can match this ad unit.
              Size size = new Size();
              size.width = 300;
              size.height = 250;

              // Create ad unit size.
              AdUnitSize adUnitSize = new AdUnitSize();
              adUnitSize.size = size;
              adUnitSize.environmentType = EnvironmentType.BROWSER;

              adUnit.adUnitSizes = new AdUnitSize[] {adUnitSize};
              return inventoryService.createAdUnits(new AdUnit[] {adUnit})[0];
        }
    /// <summary>
    /// Run the code example.
    /// </summary>
    /// <param name="user">The DFP user object running the code example.</param>
    public override void Run(DfpUser user) {
      // Get the InventoryService.
      InventoryService inventoryService =
          (InventoryService) user.GetService(DfpService.v201502.InventoryService);

      // Get the NetworkService.
      NetworkService networkService =
          (NetworkService) user.GetService(DfpService.v201502.NetworkService);

      // Set the parent ad unit's ID for all ad units to be created under.
      String effectiveRootAdUnitId = networkService.getCurrentNetwork().effectiveRootAdUnitId;

      // Create local ad unit object.
      AdUnit adUnit = new AdUnit();
      adUnit.name = "Video_Ad_Unit";
      adUnit.parentId = effectiveRootAdUnitId;
      adUnit.description = "Ad unit description.";
      adUnit.targetWindow = AdUnitTargetWindow.BLANK;
      adUnit.explicitlyTargeted = true;
      adUnit.targetPlatform = TargetPlatform.WEB;

      // Create master ad unit size.
      AdUnitSize masterAdUnitSize = new AdUnitSize();
      Size size1 = new Size();
      size1.width = 400;
      size1.height = 300;
      size1.isAspectRatio = false;
      masterAdUnitSize.size = size1;
      masterAdUnitSize.environmentType = EnvironmentType.VIDEO_PLAYER;

      // Create companion sizes.
      AdUnitSize companionAdUnitSize1 = new AdUnitSize();
      Size size2 = new Size();
      size2.width = 300;
      size2.height = 250;
      size2.isAspectRatio = false;
      companionAdUnitSize1.size = size2;
      companionAdUnitSize1.environmentType = EnvironmentType.BROWSER;

      AdUnitSize companionAdUnitSize2 = new AdUnitSize();
      Size size3 = new Size();
      size3.width = 728;
      size3.height = 90;
      size3.isAspectRatio = false;
      companionAdUnitSize2.size = size3;
      companionAdUnitSize2.environmentType = EnvironmentType.BROWSER;

      // Add companions to master ad unit size.
      masterAdUnitSize.companions = new AdUnitSize[] {companionAdUnitSize1, companionAdUnitSize2};

      // Set the size of possible creatives that can match this ad unit.
      adUnit.adUnitSizes = new AdUnitSize[] {masterAdUnitSize};

      try {
        // Create the ad unit on the server.
        AdUnit[] createdAdUnits = inventoryService.createAdUnits(new AdUnit[] {adUnit});

        foreach (AdUnit createdAdUnit in createdAdUnits) {
          Console.WriteLine("A video ad unit with ID \"{0}\" was created under parent with ID " +
              "\"{1}\".", createdAdUnit.id, createdAdUnit.parentId);
        }
      } catch (Exception e) {
        Console.WriteLine("Failed to create video ad units. Exception says \"{0}\"", e.Message);
      }
    }
    public void TestUpdateAdUnits() {
      List<AdUnitSize> adUnitSizes = null;
      Size size = null;

      // Modify ad unit 1.
      adUnitSizes = new List<AdUnitSize>(adUnit1.adUnitSizes);
      size = new Size();
      size.width = 728;
      size.height = 90;

      // Create ad unit size.
      AdUnitSize adUnitSize = new AdUnitSize();
      adUnitSize.size = size;
      adUnitSize.environmentType = EnvironmentType.BROWSER;

      adUnitSizes.Add(adUnitSize);
      adUnit1.adUnitSizes = adUnitSizes.ToArray();

      // Modify ad unit 2.
      adUnitSizes = new List<AdUnitSize>(adUnit2.adUnitSizes);
      size = new Size();
      size.width = 728;
      size.height = 90;

      // Create ad unit size.
      adUnitSize = new AdUnitSize();
      adUnitSize.size = size;
      adUnitSize.environmentType = EnvironmentType.BROWSER;

      adUnitSizes.Add(adUnitSize);
      adUnit2.adUnitSizes = adUnitSizes.ToArray();

      AdUnit[] newAdUnits = null;
      Assert.DoesNotThrow(delegate() {
        newAdUnits = inventoryService.updateAdUnits(new AdUnit[] {adUnit1, adUnit2});
      });

      Assert.NotNull(newAdUnits);
      Assert.AreEqual(newAdUnits.Length, 2);

      Assert.AreEqual(newAdUnits[0].name, adUnit1.name);
      Assert.AreEqual(newAdUnits[0].parentId, adUnit1.parentId);
      Assert.AreEqual(newAdUnits[0].id, adUnit1.id);
      Assert.AreEqual(newAdUnits[0].status, adUnit1.status);
      Assert.AreEqual(newAdUnits[0].targetWindow, adUnit1.targetWindow);
      Assert.AreEqual(newAdUnits[0].adUnitSizes.Length, adUnit1.adUnitSizes.Length);

      Assert.AreEqual(newAdUnits[1].name, adUnit2.name);
      Assert.AreEqual(newAdUnits[1].parentId, adUnit2.parentId);
      Assert.AreEqual(newAdUnits[1].id, adUnit2.id);
      Assert.AreEqual(newAdUnits[1].status, adUnit2.status);
      Assert.AreEqual(newAdUnits[1].targetWindow, adUnit2.targetWindow);
      Assert.AreEqual(newAdUnits[1].adUnitSizes.Length, adUnit2.adUnitSizes.Length);
    }