MYOBCommunity
Partner Zone
|
|
Partner Zone

Bad Request calling ItemService.GetRangeAsync

This thread is now closed to new comments.
Some of the links provided in this thread may no longer be available or relevant.
If you have a question please start a new post.
2 REPLIES 2
Former Staff ShaunWilde
62 Posts
Former Staff
Australia
ShaunWilde
Former Staff

62Posts

3Kudos

22Solutions

Accepted Solution Solved Solution

Re: Bad Request calling ItemService.GetRangeAsync

The issue is that you are sending "$orderby=Date desc".

 

Since Item does not have a Date field then you can't order by it. Try ordering by a field the entity does have.

Regards

Shaun Wilde
Experienced Partner kirsten
94 Posts
Experienced Partner
Australia
kirsten
Experienced Partner

94Posts

2Kudos

3Solutions

Re: Bad Request calling ItemService.GetRangeAsync

The problem was  my invalid orderby clause in the pageFilter

Highlighted
Experienced Partner kirsten
94 Posts
Experienced Partner
Australia
kirsten
Experienced Partner

94Posts

2Kudos

3Solutions

Solved: Go to Solution

Bad Request calling ItemService.GetRangeAsync

I have crossposted this problem to http://stackoverflow.com/questions/31554701/refactoring-api-calls-to-use-await

 

Maybe I just need an example to follow?

 

I want to modify MYOBAPI.CSharp  to retreive all the inventory items.

 

Thus I have altered CompanyFilesForm to call my code after the user logs in to the local file.

 

        private void DgvCompanyFilesCellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //Get the selected company file
            CompanyFile companyFile = ((CompanyFile[]) dgvCompanyFiles.DataSource)[e.RowIndex];

            //Get configuration that matches server where file is
            IApiConfiguration config = companyFile.Uri.GetLeftPart(UriPartial.Path).ToLower().Contains(LocalApiUrl)
                                           ? _configurationLocal
                                           : _configurationCloud;

            //Get the credentials for the database
            var frmLogin = new LoginForm();
            frmLogin.ShowDialog(this);
            if (frmLogin.Username.Length > 0)
            {
                ICompanyFileCredentials credentials = new CompanyFileCredentials(frmLogin.Username, frmLogin.Password);

	            var o = new SBDMethods();
		    o.FetchItems(companyFile,config,credentials); // my method
            }
        }

If I break at this point  and type ? config  in the immediate window I get 

 

{MYOB.AccountRight.SDK.ApiConfiguration}
    [MYOB.AccountRight.SDK.ApiConfiguration]: {MYOB.AccountRight.SDK.ApiConfiguration}
    ApiBaseUrl: "http://localhost:8080/accountright"
    ClientId: null
    ClientSecret: null
    GenerateUris: true
    RedirectUrl: null
    RequestCachePolicy: null

I login to the local file with credentials Administrator and blank password

The code to SBDMethods is

 

	internal class SBDMethods
	{
		private const double PageSize = 400;
		protected CancellationTokenSource MyCancellationTokenSource;
		protected CompanyFile MyCompanyFile;
		protected IApiConfiguration MyConfiguration;
		protected ICompanyFileCredentials MyCredentials;

		protected ItemService MyItemService;
		protected IOAuthKeyService MyOAuthKeyService;

		private int _currentPage = 1;
		private int _totalPages;

		public void FetchItems(CompanyFile companyFile, IApiConfiguration configuration, ICompanyFileCredentials credentials)
		{
	 		MyCompanyFile = companyFile;
			MyConfiguration = configuration;
			MyCredentials = credentials;
			MyCancellationTokenSource = new CancellationTokenSource();
			MyItemService = new ItemService(MyConfiguration, null, MyOAuthKeyService);
			FetchAllItemsAsync();
		}

		private async void FetchAllItemsAsync()
		{
			try
			{
				var items = new List<Item>();
				int totalPages = 0;
				do
				{
					string pageFilter = string.Format("$top={0}&$skip={1}&$orderby=Date desc", PageSize, PageSize * (_currentPage - 1));
					CancellationToken ct = MyCancellationTokenSource.Token;
					Log("About to Await GetRange");

					Task<PagedCollection<Item>> tpc = MyItemService.GetRangeAsync(MyCompanyFile, pageFilter, MyCredentials, ct, null);
					Log("About to Await GetRange B");

					PagedCollection<Item> newItems = await tpc;  // fails here

					Log("Page {0} retrieved {1} items", _currentPage, newItems.Count);
					if (totalPages == 0)
					{
						totalPages = (int)Math.Ceiling((items.Count / PageSize));
					}
					items.AddRange(newItems.Items.ToArray());
					_currentPage++;
				}
				while (_currentPage < totalPages);
				MessageBox.Show(string.Format("Fetched {0} items", items.Count));
			}
catch (ApiCommunicationException ex)
{
Log(ex.ToString()); // gets here
throw;
} catch (Exception exception) { Log(exception.ToString()); throw; } }

The error is

 

{"Encountered a validation error (http://localhost:8080/AccountRight/ab5c1f96-7663-4052-8360-81004cfe8598/Inventory/Item/?$top=400&$skip=0&$orderby=Date desc)"}
    [MYOB.AccountRight.SDK.ApiValidationException]: {"Encountered a validation error (http://localhost:8080/AccountRight/ab5c1f96-7663-4052-8360-81004cfe8598/Inventory/Item/?$top=400&$skip=0&$orderby=Date desc)"}
    base: {"Encountered a validation error (http://localhost:8080/AccountRight/ab5c1f96-7663-4052-8360-81004cfe8598/Inventory/Item/?$top=400&$skip=0&$orderby=Date desc)"}
    ErrorInformation: "Warning, error messages have not been finalised in this release and may change"
    Errors: Count = 1
    RequestId: "e573dfed-ec68-4aff-ac5e-3ffde1c2f943"
    StatusCode: BadRequest
    URI: {http://localhost:8080/AccountRight/ab5c1f96-7663-4052-8360-81004cfe8598/Inventory/Item/?$top=400&$skip=0&$orderby=Date desc}

 

Didn't find your answer here?
Try using advanced search to find a post more easily Advanced Search
or
Get the conversation started and make a new post Start a Post